Select objects by PARTIAL name

Explorer ,
Jun 16, 2017 Jun 16, 2017

Copy link to clipboard

Copied

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?

TOPICS
Scripting

Views

1.0K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Adobe Community Professional , Jun 16, 2017 Jun 16, 2017
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();

Likes

Translate

Translate
Adobe Community Professional ,
Jun 16, 2017 Jun 16, 2017

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 16, 2017 Jun 16, 2017

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 16, 2017 Jun 16, 2017

Copy link to clipboard

Copied

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();

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 19, 2017 Jun 19, 2017

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 21, 2017 Jun 21, 2017

Copy link to clipboard

Copied

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 = names.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.name.search(names)!=-1){

            doc.pageItems.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 + "' does not exist.");

            }

          }

        }

    }

}

list_to_selection();

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 14, 2021 Jan 14, 2021

Copy link to clipboard

Copied

Is necro posting ok here? Ran the script with nothing selected and got a "Select the data Source dirst!!" so I selected everything on a layer and got
Error 21: undefined is not an object.
Line: 21
-> var names = str.split(/\,|\r\n|\r|\n/g); // split the string

Is there an updated version? Am I doing something wrong?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jan 14, 2021 Jan 14, 2021

Copy link to clipboard

Copied

LATEST

Old forum posts had their code snippets mangled up. The common error is [] bracket accessors became removed from for-loops. In this case it looks like:

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

 

Should be actually:

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

 

This is another very small reason to set iterated items to variables like "thisItem = items[i];" - this way "names = thisItem.replace(...)" would stand out even more as an error.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines