Applescript Indesign - change line position

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 –

  1. 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.).
  2. 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

  1. OOOOOOOOOOOO
  2. OOOOOOOOOOOO
  3. OOOOOOOOOOOO

Page 4
4. OOOOOOOOOOOO
5. OOOOOOOOOOOO
6. OOOOOOOOOOOO

Etc.

  1. I exported all styles with each page number and each text to an Excel spreadsheet.
  2. 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”.
  3. And "oh, it is a question with 2 options, so let’s add a line with 9 checkboxes and paint just the first 2.
  4. To make it happen, it make a variable = 0 when it sees a Question, and add +1 each time it sees an answer.
  5. Next time it sees a question, it add the checkboxes and paint.
  6. 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).
  7. 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

You could use something like this:

tell application id "InDn"
	tell text frame 1 of active document
		if last character is not return then set insertion point -1 to return
		move paragraph -1 to before paragraph -2
	end tell
end tell

But the more efficient way should be to create the paragraphs from the last (in reverse order):

tell application "Numbers"
	--activate -- not sure you need it
	
	tell table 1 of active sheet of document 1
		
		-- get the last row index
		set myRowNumber to first row's address of last row
		
		-- declare initial variables
		set qNumber to 0
		set pageList to {}
		
		-- create paragraphs in reverse order
		repeat with iRow from myRowNumber to 2 by -1
			
			set pageV to round (((value of cell ("A" & iRow)) + 1)) -- take the value of column A, that is the Page Number
			set styleV to value of cell ("C" & iRow) -- take the value of column C, that is the Style
			set textV to value of cell ("D" & iRow) -- take the value of column D, that is the Text
			
			
			tell application "Adobe InDesign 2024"
				activate
				tell page 1 of active document
					tell parent story of text frame "MainTextBox"
						
						-- if the style is Page Heading, we add the title
						if styleV starts with "Page Heading" then
							set contents of insertion point 1 of paragraph 1 to textV & return -- add the title text
							set applied paragraph style of paragraph 1 to "Chapter" -- apply style
						end if
						
						-- if the style is Question Number, we add the page number
						if styleV starts with "Question numbers" then
							
							set contents of insertion point 1 of paragraph 1 to "" & return -- add 9 'checkbox' characters
							set applied paragraph style of paragraph 1 to "Items" -- apply style
							if qNumber is not 0 then set fill color of (characters -9 thru (-9 + qNumber)) of paragraph 1 to "Light red" -- colorize the checkboxes
							
							if pageList does not contain pageV then -- if we already have the page number printed before, we don't need it again
								set contents of insertion point 1 of paragraph 1 to "Page " & (pageV) & return
								set pageList to pageList & pageV
								set applied paragraph style of paragraph 1 to "Page"
							end if
							
						end if
					end tell
				end tell
				
				set qNumber to "0" -- put the counter of answer options back to zero
				
			end tell
			
			-- if the style is Question indents, it is an answer option, so we need count how many of them we have
			if styleV starts with "Question indents" then set qNumber to (qNumber + 1)
			
		end repeat
		
	end tell
end tell

This script is not tested as I dont have the source files.
Let me know if it works.

1 Like

Hi.

Perfect, thank you. Both solutions worked.

Your code works better and it’s much more elegant. I think I got lost and wrote a lot of unnecessary things in mine.

Thanks a lot.
Luiz