How to use app.selection [0] for scripts in Adobe InDesign

I would like to run the code by testing only the current selection (not the whole document) and I am having a hard time understanding how the "app.selection" array and its methods work. To start, I use a for loop to loop through each item selected with:

for(loop = 0; loop < app.selection.length; loop++){
    var sel = loop;
}

      

This works fine, but when I want to define what each element is IS, it gets a little weird. For example,

for(txt = 0; txt < app.selection[sel].textFrames.length; txt++){
    // do something to each text frame in the selection here.
}

      

doesn't work as expected, but

for(img = 0; img < app.selection[sel].allGraphics.length; img++){
    // do something to each graphic in the selection here
}

      

seems to work fine regardless of whether this selection is selected not only as a single graphic, but inside or outside the group.

Sometimes it seems like app.selection [0] is the only way to access an element yourself. In other words, if a text frame is selected, app.selection [0] might be the same as app.document.textFrames [0], in which case it would be redundant (and incorrect) to say

app.document.textFrames[0].textFrames[0]

      

And yet the same concept on different pages works like a charm. This is rather perplexing. Moreover, it seems impossible to determine which object is the object. I want to say something like:

if (app.selection[0] == [object TextFrame])

      

but that doesn't seem to work for me. Is there a way to clearly check if the current element is a group, graphics or text frame and do different things depending on the result?

+3


source to share


2 answers


app.selection returns an array of objects, so each element of the array can be of a different type, and the properties and methods available for it will be different. When using the Extendscript Extension Console, you can see which specific element in the array is on the fly by simply typing

app.selection[0]

      

(or as much as you like). The result will be something like [object TextFrame].

While looping through the selection array, you can use app.selection [0] .constructor.name to determine the type of each one. Or, if you are only interested in certain types,

if (app.selection[i] instanceof TextFrame){}

      



At this point, you will learn more about what properties you can get, depending on the type.

To answer the second part of the question, there is no allTextFrames property, but there is an allPageItems property. This returns an array of pageItems (textFrames, groups, etc.) and you can work with it similarly to app.selection. So, if I have three text frames grouped on the first page of my document (and nothing else), I can see that everything is correct:

app.activeDocument.pages[0].textFrames.length == 0;
app.activeDocument.pages[0].allPageItems.length == 4;
app.activeDocument.pages[0].allPageItems[0] instanceof Group;
app.activeDocument.pages[0].allPageItems[1].constructor.name == "TextFrame";

      

So you can probably iterate over this array if it is more useful to you than the textFrames assembly. Just keep in mind that you don't have access to the special properties of the TextFrames collection (like everyItem ()).

+3


source


App.selection is really an array that each element can access by its index:

var sel = app.selection // Can be empty with no documents open! An empty array with no selection with an open document. One to n array of length if selected.

after which you have selected one or more elements, you can reach these objects by its index

sel [0] // Returns the first element of the array. Javascript starts counting from zero.



After you say you have access, say sel [4] and counts less than 5 items or column 5 is empty, you get undefined. Therefore, you need to carefully check the validity of the element before using it and never assume that it will return anything.

NTN,

Loic http://www.ozalto.com

0


source







All Articles