Cannot delete sections of a Page Document

I am trying to make this script run.

The idea is to delete all sections from a document except the one set on the keep variable.

This is ths script

    set keep to 1
    
    tell application "Pages"
    	activate
    	delay 1
    	
    	set myDoc to front document
    	tell myDoc
    		repeat with i from 1 to the count of sections			
    			if i is not equal to keep then
    				delete section i
    			end if
    		end repeat
    	end tell
    end tell

I am getting an error on the delete section i

Yes, ScriptEditor and Automator are authorized in macOS privacy section.

I am using Mojave.

Any ideas?

Hi.

Pages doesn’t seem to like delete-ing things like text and sections. But this seems to work after a fashion:

set keep to 3

tell application "Pages"
	set myDoc to front document
	
	tell myDoc
		-- Reverse repeat so that we're not trying to index sections whose
		-- index numbers have changed as a result of the deletions.
		repeat with i from (count sections) to 1 by -1
			if (i is not keep) then
				-- "Delete" the body text of this section.
				set body text of section i to ""
				-- Then "delete" the currently last instance of a section separator
				-- (apparently character id 4) in the body text of the document.
				set (character -1 of body text where it is (character id 4)) to ""
			end if
		end repeat
	end tell
end tell

SIMPLY
B R I L L I A N T!
thanks!!! :smiley: