InDesign: Inserting text from a list into selected text frames

Hello! I wonder if anybody would be able to help me with a script I’m working on. It seems pretty easy but I can’t seem to get my head around this :expressionless:

Basically, I’d like to make some text frames in InDesign and upon selecting them, insert text (from a list) into each, in the order that the text frames were created.

For example, if I draw two text frames I’d like item 1 of the list to go into the first frame and item 2 of the list to go in the second frame. So far what I’ve come up with is this:

set dialogueList to reverse of {"This is line 1.", "This is line 2.", "This is line 3."}

tell application "Adobe InDesign 2021"
		set activePage to active page of layout window 1
		
		repeat with myFrame in (items of selection)
			set frameIndex to (index of myFrame)
				set currentParagraph to item frameIndex of dialogueList
				set contents of text frame frameIndex of activePage to currentParagraph
		end repeat
		
	end tell

The problem is, if there are three text frames and only two list items, the last two text frames receive the text instead of the first two. The first frame simply stays empty because its index becomes “3”.

Would anybody know if there’s a better way to reference text frames so that the first frame I create receives the first item of a list, even when the list/text frames differ in length? Hope that makes sense

Hi,

You should determine what list from two has less items. I have not “InDesign” at this moment, so I can’t compile and test:


set dialogueList to reverse of {"This is line 1.", "This is line 2.", "This is line 3."}
set theCount to count dialogueList -- dialog items count

tell application "Adobe InDesign 2021"
       set activePage to active page of layout window 1
       set pageItems to (items of selection)
       set pageItemsCount to count pageItems -- page items count
       if theCount > pageItemsCount then set theCount to pageItemsCount -- THAT IS IMPORTANT
       
       repeat with i from 1 to  theCount
           set frameIndex to (index of (item i pageItems))
               set currentParagraph to item i of dialogueList
               set contents of text frame frameIndex of activePage to currentParagraph
       end repeat
       end tell

NOTE: I don’t understand why do you referse the items of first list. Most likely, you should use this list as is.

Thank you so much! I reversed the list probably because I’m not very smart (haha).

Your script worked really well but I needed all of the text frames selected for it to work properly. If possible I was trying to, for example, select only the second text frame and have it still insert the second list item. However, currently if I select the second text frame it always inserts the first list item.

Sorry, I don’t think I explained it very well in my original post :frowning:

If I understood (at least, now :D), you need try block:


set dialogueList to {"This is line 1.", "This is line 2.", "This is line 3."} -- EDITED

tell application "Adobe InDesign 2021"
		
set activePage to active page of layout window 1
		repeat with myFrame in (items of selection)
			set frameIndex to (index of myFrame)
			try -- ADDED
				set currentParagraph to item frameIndex of dialogueList
				set contents of text frame frameIndex of activePage to currentParagraph
     on error -- ADDED
       exit repeat -- ADDED
				end try -- ADDED
		end repeat
		
end tell

Almost perfect, thank you!

The only issue is, the newest text frame in InDesign always receives an index of 1 :frowning:

If you make four text frames, the last frame created becomes #1 and the first text frame created becomes #4. So if there is 3 list items and 4 frames, only the last 3 text frames will receive data. Which I was hoping to avoid.

It might not be possible with the way InDesign works, though.

Hi. You can work around the problem, but it’s advisable to enforce your desired order when you make the document, either by deliberately creating objects in reverse or using script labels; I’ve previously posted how to use labels in this forum, which can be searched. Additionally, using a selection for anything other than a test is undesirable, as it’s really inefficient.

set dialogueList to {"This is line 1.", "This is line 2.", "This is line 3."}
set counter to 1
tell application "Adobe InDesign CS3"'s document 1
	repeat with aFrame in (get page 1's text frames)'s reverse --or whatever page #
		try --overlook list:object length disparity
			set aFrame's text to dialogueList's item counter
		end try
		set counter to counter + 1
	end repeat
end tell

Thank you for the advice, Marc. I really appreciate your help.
I’m gonna look into labels as I think that might be the way to go :slight_smile: