Copy link to clipboard
Copied
Hi
I wanna search Layers and Pageitems by name.
The two Layers in my Illustratorfile are labled like "28.041" and "28.104".
When I enter "28.0" as a Searchstring then I get "28.041" selected.
When I enter ".04" as a Searchstring then I get "28.041" and "28.104" selected.
It seems that "." is operating like a wildcard (I hope this is the correct expression for it :-)).
How can I search by name and considering "." as a search Sign and NOT as a wildcard?
Here is my script so far:
//Search by name
var n = prompt( 'Please enter your \'Note\' string…', 'Your Searchstring', 'Layerfinder' );
alert('Searchstring: '+n);
findLayers(n);
function findLayers(n) {
var doc = app.activeDocument;
var allLayers = doc.layers;
for ( var i = 0; i < allLayers.length; i++ ) {
if ( allLayers.name.match(n) && allLayers.pageItems.length>0) {
for ( var k = 0; k < allLayers.pageItems.length; k++ ) {
allLayers.pageItems
.selected = true; };
};
};
};
Kind regards
Alain
1 Correct answer
The issue is that you are using a string input as a regular expression, which can include regular expression-specific characters reserved for them.
The solution is to put a \ before the dot \. to escape it.
A generic escaping function could be useful any time an input string is going to be turned into a regular expression.
regex - Is there a RegExp.escape function in Javascript? - Stack Overflow
This one put the function on the RegExp object- but there are other answers on other similar questions w
...Explore related tutorials & articles
Copy link to clipboard
Copied
The issue is that you are using a string input as a regular expression, which can include regular expression-specific characters reserved for them.
The solution is to put a \ before the dot \. to escape it.
A generic escaping function could be useful any time an input string is going to be turned into a regular expression.
regex - Is there a RegExp.escape function in Javascript? - Stack Overflow
This one put the function on the RegExp object- but there are other answers on other similar questions which make it a prototype of the String object, which is what I actually think makes more sense - but this is the first one I found.
Copy link to clipboard
Copied
Do you have more than one layer with the same name?
If not, there is no need to loop through the layers. Use
doc.layers.getByName("your_String");
instead.
One additional note: Be aware - the layer and all items on it should be unlocked and visible. Otherwise your script snippet will fail sooner or later.
Copy link to clipboard
Copied
It would be nice if you could answer my question:
https://forums.adobe.com/people/pixxxel+schubser schrieb
Do you have more than one layer with the same name?
If not, there is no need to loop through the layers. Use
doc.layers.getByName("your_String");
instead …
Copy link to clipboard
Copied
Yes, I wanna select all Layers that contain a given String in its name. Thus I have to scan all Layers, not only one. But thanks for your hint as well, maybe I can use it in another case.
Copy link to clipboard
Copied
Thx for your answer. It seems that I didn't read your first post exactly enough.
It's clear now.
Copy link to clipboard
Copied
Thanks alot for that hint.
It works now and the code looks like this:
//Search by name
var n = regExpEscape(prompt( 'Please enter your \'Note\' string...', 'Your Searchstring', 'Layerfinder' ));
findLayers(n);
function findLayers(n) {
var doc = app.activeDocument;
var allLayers = doc.layers;
for ( var i = 0; i < allLayers.length; i++ ) {
if ( allLayers.name.match(n) && allLayers.pageItems.length>0) {
for ( var k = 0; k < allLayers.pageItems.length; k++ ) {
allLayers.pageItems
.selected = true; };
};
};
};
function regExpEscape(literal_string) {
return literal_string.replace(/[-[\]{}()*+!<=:?.\/\\^$|#\s,]/g, '\\$&');
};