I have a script that repeats through every page via the page count, determines the applied master page, and adds additional pages if necessary. Problem is because the adding of pages changes the page count the repeat loop stops short of the end of the document. I’ve tried using a repeat loop of “until page count is less than 1” but that doesn’t work because it won’t run through the pages at all.
tell application "Adobe InDesign CS2"
activate
set theFile to active document
tell theFile
set thePages to every page
set w to 1
repeat with w from 1 to count of thePages
try
if name of applied master of page w contains "A-" then
set theNewPage to (make new page with properties {applied master:master spread "C-Spread Master"} at after page w)
---additional code to move items from page w to new page
else
if name of applied master of page w contains "B-" then
repeat 4 times
set theNewPage to (make new page with properties {applied master:master spread "C-Spread Master"} at after page w)
---additional code to move items from page w to new pages
end repeat
end if
end if
end try
end repeat
end tell
end tell
I haven’t tested this, but it could work.
The loop uses a flexible index counter, which will be adjusted after adding the page(s)
tell application "Adobe InDesign CS2"
activate
set theFile to active document
tell theFile
set thePages to count pages
set w to 1
repeat thePages times
try
if name of applied master of page w contains "A-" then
set theNewPage to (make new page with properties {applied master:master spread "C-Spread Master"} at after page w)
set w to w + 1
---additional code to move items from page w to new page
else
if name of applied master of page w contains "B-" then
repeat 4 times
set theNewPage to (make new page with properties {applied master:master spread "C-Spread Master"} at after page w)
---additional code to move items from page w to new pages
end repeat
set w to w + 4
end if
end if
end try
set w to w + 1
end repeat
end tell
end tell
An easier way would be to use the ID of the page or label the page and use the ID or label to address the page.
A few notes:
document offset of the page is the sequential order of the page in the document
Name of the page is the page number as defined by the sections.
The problem with that is the script is adding pages between existing pages, thus increasing the document size as it goes so the page count does not reflect that from the beginning of the repeat statement. The following should work using the ID number of the page to address only those pages which existed at the beginning of the execution of the routine.
tell application "Adobe InDesign CS2"
activate
set theFile to active document
tell theFile
set CMaster to name of every master spread whose name contains "C-"
set thePages to id of every page
repeat with aPage in thePages
set aPage to item 1 of (every page whose id is aPage)
if name of applied master of aPage contains "A-" then
set theNewPage to (make new page at after aPage)
set applied master of theNewPage to master spread CMaster
else if name of applied master of aPage contains "B-" then
repeat 4 times
set theNewPage to (make new page at after aPage)
set applied master of theNewPage to master spread CMaster
end repeat
end if
end repeat
end tell
end tell
Another quick hint, try statements are great to trap errors when a script is running but I never use them when working out a code so that the Script Editor can show me where the problems are. I put try/on error’s in after I have the code working.