Hi.
I was working in a crazy thing here. I will explain below for the curious ones, but my question is: my script add a line to a Text Frame in Indesign, and later, another line. Can I swap positions of these two lines? I mean, making the first one I created the second one?
Maybe this is simple, but I’m really not finding a way. My script is below too.
Thank you,
Luiz
— Now explaining –
- I have some workbooks in Indesign. They have like 50 pages, and about 40 questions. Each question has some answer options. Maybe it’s just two (yes, no), or maybe more (yes, no, maybe, some other thing etc.).
- I need to make some “answer sheets” that shows the Chapter Title (that appears in some pages only), in the next line the Page Number, and in the next lineS (plural) each question number followed by how many checkboxes exists for that question (maybe two, maybe more). It is always 9 checkboxes, and the ones not used by the question are greyed out (I mean, if the question has only YES/NO as options, two checkboxes are black, the other 7 are 20% red). Like this:
Chapter 1
Page 2
- OOOOOOOOOOOO
- OOOOOOOOOOOO
- OOOOOOOOOOOO
Page 4
4. OOOOOOOOOOOO
5. OOOOOOOOOOOO
6. OOOOOOOOOOOO
Etc.
- I exported all styles with each page number and each text to an Excel spreadsheet.
- My script read row by row, check based on the style name “oh, it’s a chapter title! Let’s add it to the Text Frame and paint it Red”.
- And "oh, it is a question with 2 options, so let’s add a line with 9 checkboxes and paint just the first 2.
- To make it happen, it make a variable = 0 when it sees a Question, and add +1 each time it sees an answer.
- Next time it sees a question, it add the checkboxes and paint.
- It works, BUT when I have a new Chapter, it put the previous answer checkboxes AFTER it (because of the way I wrote the script).
- What I want to do it: “okay, you put it after, but new let’s swap the two lines and things will be in the right place”. And I can’t do that.
Any ideas? Script is below.
tell application "Numbers"
activate
set thisDocument to document 1
tell table 1 of active sheet of document 1
set linhaInicial to 2 -- set the first row to row 2
set myRowNumber to first row's address of last row
set finalText to ""
set qNumber to 0
set pageList to {}
repeat myRowNumber - 1 times
set pageV to round (((value of cell ("A" & linhaInicial)) + 1)) -- take the value of column A, that is the Page Number
set styleV to value of cell ("C" & linhaInicial) -- take the value of column C, that is the Style
set textV to value of cell ("D" & linhaInicial) -- take the value of column D, that is the Text
if styleV starts with "Page Heading" then -- if the style is Page Heading, it is a title, so we need the text to be there
tell application "Adobe InDesign 2024"
activate
tell page 1 of document 1
tell parent story of text frame "MainTextBox"
set contents of insertion point -1 to textV & return -- add the title text (style is below)
set totalPara to count of paragraphs of it
tell every line to set applied paragraph style of paragraph totalPara of parent story of it to "Chapter"
end tell
end tell
end tell
end if
if styleV starts with "Question numbers" then -- if the style is Question Number, it means it is a Question. We dont need it, just its page number
tell application "Adobe InDesign 2024"
activate
tell page 1 of document 1
tell parent story of text frame "MainTextBox"
set contents of insertion point -1 to "" & return -- this adds 9 characters that in the style make them checkboxes (see below how they are used)
set totalPara to count of paragraphs of it
tell every line to set applied paragraph style of paragraph totalPara of parent story of it to "Items" -- apply the style
set tWords to count characters in it
if qNumber is not 0 then set fill color of characters tWords thru (tWords - 9 + qNumber) to "Light red" -- from the 9 characters, I need all of them to be "grayed out", but the first ones according to how many answer options we have.
if pageList contains pageV then -- if we already have the page number printed before, we don't need it again
else
set contents of insertion point -1 to "Page " & (pageV) & return
set pageList to pageList & pageV
set totalPara to count of paragraphs of it
tell every line to set applied paragraph style of paragraph totalPara of parent story of it to "Page"
end if
end tell
end tell
end tell
set qNumber to "0" -- put the counter of answer options back to zero
end if
if styleV starts with "Question indents" then -- if the style is Question indents, it is an answer option, so we need count how many of them we have
set qNumber to (qNumber + 1)
end if
set linhaInicial to linhaInicial + 1 -- go to the next row
end repeat
end tell
end tell