5 Replies Latest reply on Jun 21, 2017 3:20 PM by Aristarco Palacios

    Select objects by PARTIAL name

    Aristarco Palacios Level 1

      I'd like to know if there's a way to select objects in Illustrator using a partial name.

      I know that

      doc.pageItems.getByName('ObjectName').selected = true;

      selects the object named 'ObjectName', but what if I only know part of the name? Is there a way to specify such a name with wildcards?

      I do such a thing with SQL using

      ... LIKE '*PartialName*'

      but I don't know if a similar instruction exists in Illustrator scripting. Is there?

        • 1. Re: Select objects by PARTIAL name
          Silly-V Adobe Community Professional

          Not that I know of, you just have to make a function to match some strings to bring back your desired items.
          Speaking of which, did you know about how "getByName" is case-insensitive?
          Try doc.pageItems.getByName('ObjectName') and doc.pageItems.getByName('objectname')

           

          Also it will bring back the first item it finds, case-insensitively. So you can have things named with the same exact name or a name differing only in character case, and it will just get the top one in stacking order.

          • 2. Re: Select objects by PARTIAL name
            Aristarco Palacios Level 1

            Thanks. In my case it does not matter since every object has a unique name so if I find it in upper case or lower case, it's ok. It'll be the first and only one in the stack with that name.

            The script I run selects objects whose names are in a text frame. If the name in the frame says 'Krampus', the object named 'Krampus Klaus' should be selected.

            That's why I'd like to have wildcards. In the example above, searching for 'Krampus*' would find the object.

            • 3. Re: Select objects by PARTIAL name
              Silly-V Adobe Community Professional

              You'll have to do something like this:

               

              #target illustrator

              function test(){

               

                function getPartialNamedItems(nameStr){

                  var doc = app.activeDocument, thisItem, arr = [];

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

                    thisItem = doc.pageItems[i];

                    if(thisItem.name.match(nameStr)){

                      arr.push(thisItem);

                    }

                  };

                  return arr;

                }

               

                var allItems = getPartialNamedItems ("Krampus");

                alert(allItems.length);

               

              };

              test();

              1 person found this helpful
              • 4. Re: Select objects by PARTIAL name
                Aristarco Palacios Level 1

                This is exactly what I needed.

                Thank you!

                I'll adapt this to the algorithm I'm using and post it when I make it work in case anyone needs a similar routine.

                • 5. Re: Select objects by PARTIAL name
                  Aristarco Palacios Level 1

                  Ok, here it is what I'm using now. So far so good. Original function to select by text frame is not mine but Qwertyfly...'s

                  I'm using .startsWith and .endsWith definitions from Mathias Bynens (see https://mths.be/startswith )

                   

                  function list_to_selection(){

                      var ObjectFound = false;      // Flag to know if the Object was found, at least once

                      var doc = app.activeDocument;

                      var sel = doc.selection;      // this will be the text frame

                    if(sel.length==0){

                    alert("Select the data source, first!!"); // if there's nothing selected, exit function

                    return;

                    }

                      var str = sel[0].contents; // get contents of text frame

                      sel[0].selected = false; // deselect text frame

                      var names = str.split(/\,|\r\n|\r|\n/g); // split the string

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

                          // remove any white space from start and end of each name

                          names[i] = names[i].replace(/^\s+|\s+$/g,'');

                        

                          // Initialize flag before searching for the name for every name in names list

                          ObjectFound = false;

                        

                          for(var j = 0; j < doc.pageItems.length; j++){

                            // When the name matches, select the object

                            // Ended up using .search instead of .match bc I just need to know if the string exists

                            if(doc.pageItems[j].name.search(names[i])!=-1){

                              doc.pageItems[j].selected = true;

                              ObjectFound=true;

                            } else {

                              if(j == (doc.pageItems.length-1) && !ObjectFound){

                                //If all items were read and there was no name match, warn the user. Maybe it was misspelled.

                                alert("Element '" + names[i] + "' does not exist.");

                              }

                            }

                          }

                      }

                  }

                  list_to_selection();

                   

                  Hope it works for others. Or at least helps as a base to make other functions. Thanks everyone!