5 Replies Latest reply on Mar 11, 2014 1:51 AM by Apod42

    The order of running scripts in .jsx file

    Apod42

      Hello all,

       

      I have a .jsx file which includes multiple scripts. It looks like this:

       

      Script 1 - open file 1.psd

      Script 2 - find and replace text in all opened documents

      Script 3 - open file 2.psd and 3.psd

      Script 4 - find and replace text in all opened documents

      Script 5 - open files 4.psd, 5. psd, 6.psd, 7.psd

       

      Script 2 and Script 4 are not identical, I need to run them both.

       

      My question: why do Script 2 and Script 4 also run for files  4.psd, 5. psd, 6.psd, 7.psd ? These files are opened after the scripts... How can I stop this from happening?

       

      Thank you!!

        • 1. Re: The order of running scripts in .jsx file
          c.pfaffenbichler Level 9

          My question: why do Script 2 and Script 4 also run for files  4.psd, 5. psd, 6.psd, 7.psd ?

          How can we tell if you don’t post the Scripts?

          • 2. Re: The order of running scripts in .jsx file
            Apod42 Level 1

            Sorry

             

            // 1. Open file

             

             

            var inFolder = new Folder("C:/Directory")

            if(inFolder != null){

            var fileList = inFolder.getFiles("1.psd");

            }

            for(var a = 0 ;a < fileList.length; a++)

            {

            var docRef = open(fileList[a]);

            }

             

             

            // 2. Replace text

             

             

            var file = new File ("C:/text-strings.txt");     

            file.open("r");

            var readhere = file.read();

            var replacehere = String(readhere).split("\\n").join("\r");

            var id3 = stringIDToTypeID( "replace" );

            var desc2 = new ActionDescriptor();

            var id4 = charIDToTypeID( "null" );

            var ref1 = new ActionReference();

            var id5 = charIDToTypeID( "Prpr" );

            var id6 = stringIDToTypeID( "replace" );

            ref1.putProperty( id5, id6 );

            var id7 = charIDToTypeID( "TxLr" );

            var id8 = charIDToTypeID( "Ordn" );

            var id9 = charIDToTypeID( "Al  " );

            ref1.putEnumerated( id7, id8, id9 );

            desc2.putReference( id4, ref1 );

            var id10 = charIDToTypeID( "Usng" );

            var desc3 = new ActionDescriptor();

            var id11 = stringIDToTypeID( "find" );

            desc3.putString( id11, "mytext" );

            var id12 = stringIDToTypeID( "replace" );

            desc3.putString( id12, replacehere );

            var id13 = stringIDToTypeID( "checkAll" );

            desc3.putBoolean( id13, true );

            var id14 = charIDToTypeID( "Fwd " );

            desc3.putBoolean( id14, false );

            var id15 = stringIDToTypeID( "caseSensitive" );

            desc3.putBoolean( id15, true );

            var id16 = stringIDToTypeID( "wholeWord" );

            desc3.putBoolean( id16, false );

            var id17 = stringIDToTypeID( "findReplace" );

            desc2.putObject( id10, id17, desc3 );

            executeAction( id3, desc2, DialogModes.NO );

             

             

            // 3. Open files

             

             

            var inFolder = new Folder("C:/Directory")

            if(inFolder != null){

            var fileList = inFolder.getFiles("2.psd"|"3.psd");

            }

            for(var a = 0 ;a < fileList.length; a++)

            {

            var docRef = open(fileList[a]);

            }

             

             

            // 4. Replace Text

             

             

            #target photoshop

            if (app.documents.length > 0) {

            for (var n = 0; n < app.documents.length; n++) {

            app.activeDocument = app.documents[n];

            app.activeDocument.suspendHistory("replace text", "main()")

            }

            };

            function main () {

            var myDocument = app.activeDocument;

            var theTexts = readPref ("C:/text-strings2.txt");

            var theArray1 = theTexts[0];

            var theArray2 = theTexts[1];

            for (var b = 0; b < theArray1.length; b++) {

            replaceText (theArray1[b], theArray2[b])

            };

            };

            function replaceText (replaceThis, replaceWith) {

            var idreplace = stringIDToTypeID( "replace" );

            var desc22 = new ActionDescriptor();

            var idnull = charIDToTypeID( "null" );

            var ref3 = new ActionReference();

            var idPrpr = charIDToTypeID( "Prpr" );

            var idreplace = stringIDToTypeID( "replace" );

            ref3.putProperty( idPrpr, idreplace );

            var idTxLr = charIDToTypeID( "TxLr" );

            var idOrdn = charIDToTypeID( "Ordn" );

            var idAl = charIDToTypeID( "Al  " );

            ref3.putEnumerated( idTxLr, idOrdn, idAl );

            desc22.putReference( idnull, ref3 );

            var idUsng = charIDToTypeID( "Usng" );

            var desc23 = new ActionDescriptor();

            var idfind = stringIDToTypeID( "find" );

            desc23.putString( idfind, replaceThis );

            var idreplace = stringIDToTypeID( "replace" );

            desc23.putString( idreplace, replaceWith );

            var idcheckAll = stringIDToTypeID( "checkAll" );

            desc23.putBoolean( idcheckAll, true );

            var idFwd = charIDToTypeID( "Fwd " );

            desc23.putBoolean( idFwd, false );

            var idcaseSensitive = stringIDToTypeID( "caseSensitive" );

            desc23.putBoolean( idcaseSensitive, true );

            var idwholeWord = stringIDToTypeID( "wholeWord" );

            desc23.putBoolean( idwholeWord, false );

            var idignoreAccents = stringIDToTypeID( "ignoreAccents" );

            desc23.putBoolean( idignoreAccents, false );

            var idfindReplace = stringIDToTypeID( "findReplace" );

            desc22.putObject( idUsng, idfindReplace, desc23 );

            executeAction( idreplace, desc22, DialogModes.NO );

            };

            function readPref (thePath) {

            if (File(thePath).exists == true) {

            var file = File(thePath);

            file.open("r");

            var textArray = [];

            textArray.push(file.readln().split("|"));

            textArray.push(file.readln().split("|"));

            file.close();

            return textArray;

            }

            };

             

             

            // 5. Open files

             

             

            var inFolder = new Folder("C:/Directory")

            if(inFolder != null){

            var fileList = inFolder.getFiles("4.psd"|"5.psd"|"6.psd"|"7.psd");

            }

            for(var a = 0 ;a < fileList.length; a++)

            {

            var docRef = open(fileList[a]);

            }

            • 3. Re: The order of running scripts in .jsx file
              Apod42 Level 1

              What I really don't get is why script 5 triggers before the text replace scripts... Javascripts are not supposed to run in order?

              • 4. Re: The order of running scripts in .jsx file
                c.pfaffenbichler Level 9

                A Script can perform many tasks in sequence, but if its one jsx-file it’s one Script.

                 

                And indeed JavaScript should perform in sequence.

                To observe what happen when you could include alerts or $-writeln (to get messages in the JavaScript Console) in the Script.

                • 5. Re: The order of running scripts in .jsx file
                  Apod42 Level 1

                  As in the past, thank you again! Will get on to that!