Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Remove text from object name or string.

Contributor ,
Feb 07, 2012 Feb 07, 2012

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

TOPICS
Actions and scripting
1.6K
Translate
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

Valorous Hero , Feb 07, 2012 Feb 07, 2012

You can remove the trim - for the active layer....

activeDocument.activeLayer.name =  activeDocument.activeLayer.name.replace(/trim -/i,'');

Translate
Adobe
Valorous Hero ,
Feb 07, 2012 Feb 07, 2012

You can remove the trim - for the active layer....

activeDocument.activeLayer.name =  activeDocument.activeLayer.name.replace(/trim -/i,'');

Translate
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
Contributor ,
Feb 07, 2012 Feb 07, 2012

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.

Translate
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
Valorous Hero ,
Feb 07, 2012 Feb 07, 2012

Something like this....

var str = "FilesName";
if(!str.match(/.jpg$/)) str = str + ".jpg";

alert(str);

Translate
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
Contributor ,
Feb 07, 2012 Feb 07, 2012

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.

Translate
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
Valorous Hero ,
Feb 07, 2012 Feb 07, 2012

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...

http://www.regular-expressions.info/

Translate
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
Contributor ,
Feb 07, 2012 Feb 07, 2012
LATEST

Thanks a ton!  Adding that site to my (currently short) list of scripting info sites.

Translate
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