Copy link to clipboard
Copied
How does one go about removing certain parts of a word, name, or string? For example; I have a document where there are layers such as "trim - highlight" or "trim - color" etc. These are already in a group called trim, so having the prefix "trim - " is kind of redundant.
I'm basically loading these layers into an array based on their group, then saving them out based on their layer name. This isn't really a problem for new projects, but some of our older ones still use the old naming with the redundancy; so I'd like to build something into our saving script to automatically remove this redundancy before saving the layer. Any help is appreciated.
dgolberg
You can remove the trim - for the active layer....
activeDocument.activeLayer.name = activeDocument.activeLayer.name.replace(/trim -/i,'');
Copy link to clipboard
Copied
You can remove the trim - for the active layer....
activeDocument.activeLayer.name = activeDocument.activeLayer.name.replace(/trim -/i,'');
Copy link to clipboard
Copied
That worked great Paul! Thanks a ton!
There is one other thing, though, that I can see being handy in the near future. Is there way to get it to check if that exists before changing it? For example, having it check a string to see if it ends in .jpg or not, and if it doesn't, adding that in.
Copy link to clipboard
Copied
Something like this....
var str = "FilesName";
if(!str.match(/.jpg$/)) str = str + ".jpg";alert(str);
Copy link to clipboard
Copied
Awesome! Thanks again Paul! One last thing (simple question), what does the $ represent in that last script? Does it tell it to check only the end of the string?
Sorry, I'm still more of a script hacker than writer at this point... so trying to learn what all the various symbols do.
Copy link to clipboard
Copied
You've got it in one.
if(!str.match(/^.jpg/)) str = str + ".jpg";
This checks if .jpg is at the front of the string.
This site is a good starting point for regular expressions...
Copy link to clipboard
Copied
Thanks a ton! Adding that site to my (currently short) list of scripting info sites.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more