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

Javascript find and replace text in photoshop

Community Beginner ,
Jan 09, 2020 Jan 09, 2020

Copy link to clipboard

Copied

Hi there,

 

I have been trying to write a script that finds and replaces certain text files in photoshop.

For example: There was a large man called (Name) with a big burly beard.

I have created an action using find and replace text to achieve this, but in the process of trying to automate this process by converting it to javascript using Xtools it no longer works.

Any help pointing me in the right direction would be super useful.

 

Cheers,

 

Marty

 

#target photoshop

//

// Action1.jsx

//

 

//

// Generated Fri Jan 10 2020 12:56:37 GMT+1100

//

 

cTID = function(s) { return app.charIDToTypeID(s); };

sTID = function(s) { return app.stringIDToTypeID(s); };

 

//

//==================== Action 1 ==============

//

function Action1() {

  // Replace All

  function step1(enabled, withDialog) {

    if (enabled != undefined && !enabled)

      return;

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

    var desc1 = new ActionDescriptor();

    var ref1 = new ActionReference();

    ref1.putProperty(cTID('Prpr'), sTID("replace"));

    ref1.putEnumerated(cTID('TxLr'), cTID('Ordn'), cTID('Al  '));

    desc1.putReference(cTID('null'), ref1);

    var desc2 = new ActionDescriptor();

    desc2.putString(cTID('find'), "(Name)");

    desc2.putString(sTID("replace"), "John");

    desc2.putBoolean(sTID("checkAll"), true);

    desc2.putBoolean(cTID('Fwd '), true);

    desc2.putBoolean(sTID("caseSensitive"), false);

    desc2.putBoolean(sTID("wholeWord"), false);

    desc2.putBoolean(sTID("ignoreAccents"), true);

    desc1.putObject(cTID('Usng'), sTID("findReplace"), desc2);

    executeAction(sTID('replace'), desc1, dialogMode);

  };

 

  step1();      // Replace All

};

 

 

 

//=========================================

//                    Action1.main

//=========================================

//

 

Action1.main = function () {

  Action1();

};

 

Action1.main();

 

// EOF

 

"Action1.jsx"

// EOF

 

TOPICS
Actions and scripting

Views

4.1K

Translate

Translate

Report

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
Adobe
Community Expert ,
Jan 19, 2022 Jan 19, 2022

Copy link to clipboard

Copied

@AlexPrint 

 

Let's push the reset button on this...

 

With a filename of:

For sale _ Design 023.psd

 

To remove everything after the first underscore _ character, including the underscore, one possible regular expression would be using a positive lookahead, replacing the unwanted text with nothing to delete:

 

.replace(/(?=\w{1,})_.+/, '')

 

Keep in mind that there is a word space either side of the underscore character. The regex above will leave a trailing white space at the end from before the underscore character. One possible way to remove this is to chain together two regex commands in the JavaScript .replace()

 

.replace(/(?=\w{1,})_.+/, '').replace(/ $/, '');

 

Another way to appraoch this is with a capture group, capturing the wanted text and replacing with only the capture:

 

.replace(/(^.+)(_.+)(\.[^\.]+$)/, '$1').replace(/ $/, '');

 

Again, adding a second regex to account for the trailing word space at the end.

 

You may find the following sites helpful:

www.regex101.com

www.regexr.com

https://www.regular-expressions.info

 

Votes

Translate

Translate

Report

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
Participant ,
Jan 19, 2022 Jan 19, 2022

Copy link to clipboard

Copied

Wow, you are a master!, thank you very much, that is more than I can ask for, thank you very much for your help, sincere blessings trully grateful guy here

Votes

Translate

Translate

Report

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
New Here ,
Feb 18, 2021 Feb 18, 2021

Copy link to clipboard

Copied

Sorry I'm try  to use this script and this is the message: 

The command "Replace All" is not currently available

Any idea how  to  resolve that?? 😞

Votes

Translate

Translate

Report

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 18, 2021 Feb 18, 2021

Copy link to clipboard

Copied

In order for this to work, any text layer must be active (selected). This is a long-standing bug for this command when used in Actions / Scripts.

Votes

Translate

Translate

Report

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