Select objects by name

Explorer ,
Jan 20, 2021

Copy link to clipboard

Copied

I once again throw myself at teh feet fo teh scriptors and beg for one more script. I am hoping to be able to select objects by partial name, like "Votoms" or "Expanse" so that I can apply a style to them. The object names tend to be more complicated than just the one word, with a 8 digit number and more refrence text. Is this possible? I've dug through and tried everything list here and elsewhere and cannot find a script that works.
Help me oh-be script-kenonbi, you are my last hope.

TOPICS
Scripting

Views

104

Likes

Translate

Translate

Report

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

1 Correct Answer

Engaged , Jan 20, 2021
Inventsable Engaged , Jan 20, 2021
 You can use very basic RegExp to match character patterns inside any string no matter their location.var ignoreCase = false; // Should it match both "hello" and "Hello" var textToFind = prompt("Write the partial name to find") // Create new RegExp and clear selection var RX = new RegExp(textToFind, ignoreCase ? "i" : "") app.selection = null; // Iterate through pageItems for (var i = 0; i < app.activeDocument.pageItems.length; i++) { // If the RegExp test on name is true, then select it ...

Likes

Translate

Translate
Jump to answer Jump to answer
Engaged ,
Jan 20, 2021

Copy link to clipboard

Copied

 

You can use very basic RegExp to match character patterns inside any string no matter their location.

var ignoreCase = false; // Should it match both "hello" and "Hello"
var textToFind = prompt("Write the partial name to find")

// Create new RegExp and clear selection
var RX = new RegExp(textToFind, ignoreCase ? "i" : "")
app.selection = null;

// Iterate through pageItems
for (var i = 0; i < app.activeDocument.pageItems.length; i++) {
    // If the RegExp test on name is true, then select it
    if (RX.test(app.activeDocument.pageItems[i].name))
        app.activeDocument.pageItems[i].selected = true;
}

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
kylwell AUTHOR
Explorer ,
Jan 20, 2021

Copy link to clipboard

Copied

Beautiful! Thank you so very much!

 

 

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Enthusiast ,
Jan 20, 2021

Copy link to clipboard

Copied

For simpler cases, you can use one of many string functions, e.g.

  • slice(from, to_not_including)
  • substring(from, to_not_including)
  • substr(from, length)
var name1 = "VotomsXXXXXXXX";
if (name1.slice(0, 6) == "Votoms") {
    alert(true);
}

 

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Engaged ,
Jan 20, 2021

Copy link to clipboard

Copied

Should note that explicit string matching via sub and slice is only simpler depending on use case; if these partial matches are a guaranteed prefix and case sensitive; otherwise you'd be better off using indexOf:

 

alert(string[i].slice(0, 6) == "Votoms")
"VotomsXXXXXXXX",   // true
"XXVotomsXXXXXXXX", // false
"votomsXXXXXXXX"    // false

alert(string[i].indexOf("Votoms") >= 0)
"VotomsXXXXXXXX",   // true
"XXVotomsXXXXXXXX", // true
"votomsXXXXXXXX"    // false

alert(
        new RegExp("votoms", "i").test(string[i])
)
"VotomsXXXXXXXX",   // true
"XXVotomsXXXXXXXX", // true
"votomsXXXXXXXX"    // true

alert(
        string[i].toLowerCase().indexOf("Votoms".toLowerCase()) >= 0
)
"VotomsXXXXXXXX",   // true
"XXVotomsXXXXXXXX", // true
"votomsXXXXXXXX"    // true

 

 

Likes

Translate

Translate

Report

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