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.
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;
}
Copy link to clipboard
Copied
Beautiful! Thank you so very much!
Copy link to clipboard
Copied
For simpler cases, you can use one of many string functions, e.g.
var name1 = "VotomsXXXXXXXX";
if (name1.slice(0, 6) == "Votoms") {
alert(true);
}
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