Search code examples
applescriptadobe-indesign

How can I select a text frame by script label using applescript in InDesign?


I'm trying use AppleScript to select pre-existing text frames in an InDesign document by using pre-existing unique labels, and then modify the fill colours and sizes of those text frames. (This is part of a larger workflow that adjusts these properties for about ~100 such frames, according to values in a list. The labels of the text frames are also in a list, and the position of the labels and values is the same in their respective lists, with an eye to putting this into a loop.)

In my example code below, "Fred" is the label of my text frame, "Secondary 1" is the name of the colour swatch I'm trying to set as the fill color of the text frame, and I'm trying to specify the width of the text frame to be 107 mm without changing any other geometric bounds.

set labelList to {"Fred","Alice","Bob"}
set valueList to {107,42,42}

tell application "Adobe InDesign 2020"
    set myDocument to active document -- presumes you've already done a data merge.
    tell myDocument
        set testFrame to every text frame whose label is item 1 of labelList
        copy geometric bounds of testFrame to {x0, y0, x1, y1}
        set geometric bounds of testFrame to {x0, y0, (x0 + item 1 of valueList), y1}
        set fill color of testFrame to {"Secondary 1"}
    end tell
    
end tell

This fails with the following error:

"Can’t get geometric bounds of {text frame id 6546 of spread id 5872 of document id 2 of application "Adobe InDesign 2020"}." number -1728 from «class gbnd» of {«class txtf» id 6546 of «class sprd» id 5872 of document id 2}

The fact that the error gives me a text frame id number makes me think that I'm succeeding in selecting the text frame with the label "Fred", but that I'm not adequately specifying something in the following lines. As a result the the script can't correctly read or set the properties of that text frame.

I've come across lots of examples of script to create text frames and set their properties, and there's a similar question on the site for setting the properties of text within a text frame - but the answer in that case is that the properties of text are held by a different object, so not much help there.

Any advice would be much appreciated!


Solution

  • The statement

    set testFrame to every text frame whose label is item 1 of labelList
    

    returns a list. It would be less confusing if you named that testFramesToProcess and then

    set testFrame to item 1 of testFramesToProcess