Skip to main content
Inspiring
November 27, 2011
Answered

Selecting Symbols on page by name

  • November 27, 2011
  • 1 reply
  • 3641 views

Im struggling with the correct use of symbol / symbol items in applescript. I can successfully list the symbols used on the page, but I cannot select them. Any suggestions on how I can select all the items on the page that are Symbol Items of symbols named "Apples" but not oranges? After that I hope to replace the instances matching the height or width of the items whichever is greater with a 3rd item name. Any suggestions would be great. Javascript is always an option as well but I had the same difficulty.

This topic has been closed for replies.
Correct answer CarlosCanto

here's how to do it with JS

var idoc = app.activeDocument; // get active document

for (i=0; i<idoc.symbolItems.length; i++) // loop thru all symbol items in the active document

          {

                    var symbolitem = idoc.symbolItems; // get each symbol item

                    if (symbolitem.symbol.name == "Apples") // check if it is an instance of "Apples" symbol

                              {

                                        symbolitem.selected = true; // if it is, then select it

                              }

          }

1 reply

CarlosCanto
Community Expert
CarlosCantoCommunity ExpertCorrect answer
Community Expert
November 27, 2011

here's how to do it with JS

var idoc = app.activeDocument; // get active document

for (i=0; i<idoc.symbolItems.length; i++) // loop thru all symbol items in the active document

          {

                    var symbolitem = idoc.symbolItems; // get each symbol item

                    if (symbolitem.symbol.name == "Apples") // check if it is an instance of "Apples" symbol

                              {

                                        symbolitem.selected = true; // if it is, then select it

                              }

          }

Inspiring
June 7, 2012

Hello Everyone,

First let me start by apologizing as I am not sure if its ok to post in this thread with a very similar question that continues on this very same topic? I hope its ok, anyway, this is great to know as I too was wondering this myself just recently, then I saw this thread when it was bumped, so awesome! I am wondering however if it is possible to extend this little script to find items that their names include a certain string with in them? So instead of just selecting items named "Apples" the script would select items that have the desired string term within the name as follows:

123apple5

apple_1, apple_2, apple_3 … apple_45, etc...

apple

APPLE

Apple

appleapple

bigredapple

juicyApple

etc…

Of course these would not work:

app5le

appl_e

a_p_p_l_e

etc..

So finding the string "apple" within the items name on the page, upper or lower case as long as the letters "apple" reside somewhere in the name sequentially. Hope that makes since, I was wondering if something like this would be possible?

Any feedback, advice, scripts, are welcome, thanks everyone for your time and assitance.

hilukasz
Known Participant
June 7, 2012

you would run a match method on it, which returns true if it matches. the if statement checks if they method returns true and tells you what layers contain apples. It uses a regular expression to match it, kind of a shortcut so you don't have to do all the regex mess, starts with / ends with / and the i tells it to ignore case making it case insenstive:

var doc = app.activeDocument,

    layers = doc.layers,

    //string to match, change apples to anything else you want

    stringToMatch = /apple/i;

//iterate through all the layers

for(var i = 0; i < layers.length; i++){

    // if true, continue

    if(layers.name.match(stringToMatch)){

        //return result

        print(layers.name+" contains apples");

    }

}

ran test on all cases you posted. seems to work well.

returns:

bigredapple contains apples

juicyApple contains apples

123apples5 contains apples

appleapples contains apples

APPLES contains apples

apple_1 contains apples

Tastey Apples contains apples

_apples contains apples

apples contains apples

and if you wanted to get user input via a prompt you could use something like this:

Array.prototype.indexOf = Array.prototype.indexOf || function(value, start) {

  for (var i = 0, length = this.length; i < length; i++) {

    if (this == value) {

      return i;

    }

  }

  return -1;

}

var doc = app.activeDocument,

    layers = doc.layers,

    matchString = 'aPpLe';

for(var i = 0; i < layers.length; i++){

    var string = layers.name.toLowerCase();

    if (string.indexOf(matchString.toLowerCase()) != -1){

        print(layers.name+" contains apples");

    }

}

but this should come with understanding that you are extending the array class and doing some hacky things with extendscript....