Skip to main content
November 18, 2014
Question

If word found in document do stuff

  • November 18, 2014
  • 1 reply
  • 329 views

Hello all,

Looking for a Photoshop script that works like this:

1. Search all text layers of opened document for word "Example" - already know how to do this

2. If word "Example" is found in document, do stuff. If word does not exist, do nothing. - help with this part please?

This topic has been closed for replies.

1 reply

c.pfaffenbichler
Community Expert
Community Expert
November 18, 2014

As you already know how to find the word using an if-clause depending on those results should be no problem.

November 18, 2014

c.pfaffenbichler, very glad to see you again!

For searching the word I plan to use a script recorded with the script listner. Example below searches the document for the word "Example".

I just don't know how to write the if clause. I need something like this:

// Run the script below, search for word "Example"

if (word "Example" is found in document) {

do stuff, another PS script goes here

}

else move on with executing the script

Can you please show me how to write in JS: if (word "Example" is found in document) ?

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

var idfindReplace = stringIDToTypeID( "findReplace" );

    var desc2 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref1 = new ActionReference();

        var idPrpr = charIDToTypeID( "Prpr" );

        var idfindReplace = stringIDToTypeID( "findReplace" );

        ref1.putProperty( idPrpr, idfindReplace );

        var idTxLr = charIDToTypeID( "TxLr" );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idAl = charIDToTypeID( "Al  " );

        ref1.putEnumerated( idTxLr, idOrdn, idAl );

    desc2.putReference( idnull, ref1 );

    var idUsng = charIDToTypeID( "Usng" );

        var desc3 = new ActionDescriptor();

        var idfind = stringIDToTypeID( "find" );

        desc3.putString( idfind, """Example""" );

        var idcheckAll = stringIDToTypeID( "checkAll" );

        desc3.putBoolean( idcheckAll, true );

        var idFwd = charIDToTypeID( "Fwd " );

        desc3.putBoolean( idFwd, false );

        var idcaseSensitive = stringIDToTypeID( "caseSensitive" );

        desc3.putBoolean( idcaseSensitive, true );

        var idwholeWord = stringIDToTypeID( "wholeWord" );

        desc3.putBoolean( idwholeWord, false );

        var idignoreAccents = stringIDToTypeID( "ignoreAccents" );

        desc3.putBoolean( idignoreAccents, true );

    var idfindReplace = stringIDToTypeID( "findReplace" );

    desc2.putObject( idUsng, idfindReplace, desc3 );

executeAction( idfindReplace, desc2, DialogModes.NO );

c.pfaffenbichler
Community Expert
Community Expert
November 19, 2014

If you want to use "Find and Replace Text" you could do something like this:

// 2014, use at your own risk;

#target photoshop

if (app.documents.length > 0) {main()};

////// function //////

function main () {

// set to pixels and 72ppi;

var myDocument = app.activeDocument;

var theState = myDocument.activeHistoryState;

var theString = "Example";

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

var idfindReplace = stringIDToTypeID( "findReplace" );

    var desc2 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref1 = new ActionReference();

        var idPrpr = charIDToTypeID( "Prpr" );

        var idfindReplace = stringIDToTypeID( "findReplace" );

        ref1.putProperty( idPrpr, idfindReplace );

        var idTxLr = charIDToTypeID( "TxLr" );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idAl = charIDToTypeID( "Al  " );

        ref1.putEnumerated( idTxLr, idOrdn, idAl );

    desc2.putReference( idnull, ref1 );

    var idUsng = charIDToTypeID( "Usng" );

        var desc3 = new ActionDescriptor();

        var idfind = stringIDToTypeID( "find" );

        desc3.putString( idfind, theString );

        var idcheckAll = stringIDToTypeID( "checkAll" );

        desc3.putBoolean( idcheckAll, true );

        var idFwd = charIDToTypeID( "Fwd " );

        desc3.putBoolean( idFwd, false );

        var idcaseSensitive = stringIDToTypeID( "caseSensitive" );

        desc3.putBoolean( idcaseSensitive, true );

        var idwholeWord = stringIDToTypeID( "wholeWord" );

        desc3.putBoolean( idwholeWord, false );

        var idignoreAccents = stringIDToTypeID( "ignoreAccents" );

        desc3.putBoolean( idignoreAccents, true );

    var idfindReplace = stringIDToTypeID( "findReplace" );

    desc2.putObject( idUsng, idfindReplace, desc3 );

var theVar = executeAction( idfindReplace, desc2, DialogModes.NO );

//

if (myDocument.activeHistoryState == theState) {}

else {

  myDocument.activeHistoryState = theState;

  alert ("do stuff")

  }

};