5 Replies Latest reply on Jul 10, 2018 7:05 AM by Ham1000

    FindChangeByList stopped working

    Ham1000 Level 1

      Hi all,

       

      InDesign CC 13.1

      OS 10.13.4

       

      We've been using the FindChangeByList scripts (Javascript and Applescript versions) for some years now. I don't know anything about scripting but had enough commonsense to manage to amend the scripts to make some very simple changes at the push of a button.

       

      We used the Javascript version to search for certain tags in placed text (header levels and bullet point levels) and then change the paragraph styles to commonly named paragraph styles. Unfortunately the Javascript FindChangeByList script started throwing an error (in InDesign CS5, I think) and no longer does what it used to do.

       

      I've tried using the RecordFindChange script that was around (albeit for CS3-CS5). The results that brings up looks OK to me, but if I copy and paste them into the existing .txt files then I just get errors.

       

      As I say, I don't have the scripting knowledge to know where to go with this so any help would be gratefully received.

       

      So, I'm trying to get the script to search text placed into an InDesign doc. It's supposed to look for and change "<<A>>", "<<B>>", "<<b1>>" and "<<b2>>" into "A Header", "B Header", "Body Bullet" and "Body Bullet 2" respectively.

       

      The script is as follows:

       

      //FindChangeByList.jsx

      //An InDesign JavaScript

      /* 

      @@@BUILDINFO@@@ "FindChangeByList.jsx" 3.0.0 15 December 2009

      */

      //Loads a series of tab-delimited strings from a text file, then performs a series

      //of find/change operations based on the strings read from the file.

      //

      //The data file is tab-delimited, with carriage returns separating records.

      //

      //The format of each record in the file is:

      //findType<tab>findProperties<tab>changeProperties<tab>findChangeOptions<tab>description

      //

      //Where:

      //<tab> is a tab character

      //findType is "text", "grep", or "glyph" (this sets the type of find/change operation to use).

      //findProperties is a properties record (as text) of the find preferences.

      //changeProperties is a properties record (as text) of the change preferences.

      //findChangeOptions is a properties record (as text) of the find/change options.

      //description is a description of the find/change operation

      //

      //Very simple example:

      //text    {findWhat:"--"}    {changeTo:"^_"}    {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, wholeWord:false}    Find all double dashes and replace with an em dash.

      //

      //More complex example:

      //text    {findWhat:"^9^9.^9^9"}    {appliedCharacterStyle:"price"}    {include footnotes:true, include master pages:true, include hidden layers:true, whole word:false}    Find $10.00 to $99.99 and apply the character style "price".

      //

      //All InDesign search metacharacters are allowed in the "findWhat" and "changeTo" properties for findTextPreferences and changeTextPreferences.

      //

      //If you enter backslashes in the findWhat property of the findGrepPreferences object, they must be "escaped"

      //as shown in the example below:

      //

      //{findWhat:"\\s+"}

      //

      //For more on InDesign/InCopy scripting see the documentation included in the Scripting SDK

      //available at http://www.adobe.com/devnet/indesign/sdk.html

      //or visit the InDesign Scripting User to User forum at http://www.adobeforums.com

      //

      main();

      function main(){

          var myObject;

          //Make certain that user interaction (display of dialogs, etc.) is turned on.

          app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;

          if(app.documents.length > 0){

              if(app.selection.length > 0){

                  switch(app.selection[0].constructor.name){

                      case "InsertionPoint":

                      case "Character":

                      case "Word":

                      case "TextStyleRange":

                      case "Line":

                      case "Paragraph":

                      case "TextColumn":

                      case "Text":

                      case "Cell":

                      case "Column":

                      case "Row":

                      case "Table":

                          myDisplayDialog();

                          break;

                      default:

                          //Something was selected, but it wasn't a text object, so search the document.

                          myFindChangeByList(app.documents.item(0));

                  }

              }

              else{

                  //Nothing was selected, so simply search the document.

                  myFindChangeByList(app.documents.item(0));

              }

          }

          else{

              alert("No documents are open. Please open a document and try again.");

          }

      }

      function myDisplayDialog(){

          var myObject;

          var myDialog = app.dialogs.add({name:"FindChangeByList"});

          with(myDialog.dialogColumns.add()){

              with(dialogRows.add()){

                  with(dialogColumns.add()){

                      staticTexts.add({staticLabel:"Search Range:"});

                  }

                  var myRangeButtons = radiobuttonGroups.add();

                  with(myRangeButtons){

                      radiobuttonControls.add({staticLabel:"Document", checkedState:true});

                      radiobuttonControls.add({staticLabel:"Selected Story"});

                      if(app.selection[0].contents != ""){

                          radiobuttonControls.add({staticLabel:"Selection", checkedState:true});

                      }

                  }           

              }

          }

          var myResult = myDialog.show();

          if(myResult == true){

              switch(myRangeButtons.selectedButton){

                  case 0:

                      myObject = app.documents.item(0);

                      break;

                  case 1:

                      myObject = app.selection[0].parentStory;

                      break;

                  case 2:

                      myObject = app.selection[0];

                      break;

              }

              myDialog.destroy();

              myFindChangeByList(myObject);

          }

          else{

              myDialog.destroy();

          }

      }

      function myFindChangeByList(myObject){

          var myScriptFileName, myFindChangeFile, myFindChangeFileName, myScriptFile, myResult;

          var myFindChangeArray, myFindPreferences, myChangePreferences, myFindLimit, myStory;

          var myStartCharacter, myEndCharacter;

          var myFindChangeFile = myFindFile("/FindChangeSupport/FindChangeList.txt")

          if(myFindChangeFile != null){

              myFindChangeFile = File(myFindChangeFile);

              var myResult = myFindChangeFile.open("r", undefined, undefined);

              if(myResult == true){

                  //Loop through the find/change operations.

                  do{

                      myLine = myFindChangeFile.readln();

                      //Ignore comment lines and blank lines.

                      if((myLine.substring(0,4)=="text")||(myLine.substring(0,4)=="grep")||(myLine.substring(0, 5)=="glyph")){

                          myFindChangeArray = myLine.split("\t");

                          //The first field in the line is the findType string.

                          myFindType = myFindChangeArray[0];

                          //The second field in the line is the FindPreferences string.

                          myFindPreferences = myFindChangeArray[1];

                          //The second field in the line is the ChangePreferences string.

                          myChangePreferences = myFindChangeArray[2];

                          //The fourth field is the range--used only by text find/change.

                          myFindChangeOptions = myFindChangeArray[3];

                          switch(myFindType){

                              case "text":

                                  myFindText(myObject, myFindPreferences, myChangePreferences, myFindChangeOptions);

                                  break;

                              case "grep":

                                  myFindGrep(myObject, myFindPreferences, myChangePreferences, myFindChangeOptions);

                                  break;

                              case "glyph":

                                  myFindGlyph(myObject, myFindPreferences, myChangePreferences, myFindChangeOptions);

                                  break;

                          }

                      }

                  } while(myFindChangeFile.eof == false);

                  myFindChangeFile.close();

              }

          }

      }

      function myFindText(myObject, myFindPreferences, myChangePreferences, myFindChangeOptions){

          //Reset the find/change preferences before each search.

          app.changeTextPreferences = NothingEnum.nothing;

          app.findTextPreferences = NothingEnum.nothing;

          var myString = "app.findTextPreferences.properties = "+ myFindPreferences + ";";

          myString += "app.changeTextPreferences.properties = " + myChangePreferences + ";";

          myString += "app.findChangeTextOptions.properties = " + myFindChangeOptions + ";";

          app.doScript(myString, ScriptLanguage.javascript);

          myFoundItems = myObject.changeText();

          //Reset the find/change preferences after each search.

          app.changeTextPreferences = NothingEnum.nothing;

          app.findTextPreferences = NothingEnum.nothing;

      }

      function myFindGrep(myObject, myFindPreferences, myChangePreferences, myFindChangeOptions){

          //Reset the find/change grep preferences before each search.

          app.changeGrepPreferences = NothingEnum.nothing;

          app.findGrepPreferences = NothingEnum.nothing;

          var myString = "app.findGrepPreferences.properties = "+ myFindPreferences + ";";

          myString += "app.changeGrepPreferences.properties = " + myChangePreferences + ";";

          myString += "app.findChangeGrepOptions.properties = " + myFindChangeOptions + ";";

          app.doScript(myString, ScriptLanguage.javascript);

          var myFoundItems = myObject.changeGrep();

          //Reset the find/change grep preferences after each search.

          app.changeGrepPreferences = NothingEnum.nothing;

          app.findGrepPreferences = NothingEnum.nothing;

      }

      function myFindGlyph(myObject, myFindPreferences, myChangePreferences, myFindChangeOptions){

          //Reset the find/change glyph preferences before each search.

          app.changeGlyphPreferences = NothingEnum.nothing;

          app.findGlyphPreferences = NothingEnum.nothing;

          var myString = "app.findGlyphPreferences.properties = "+ myFindPreferences + ";";

          myString += "app.changeGlyphPreferences.properties = " + myChangePreferences + ";";

          myString += "app.findChangeGlyphOptions.properties = " + myFindChangeOptions + ";";

          app.doScript(myString, ScriptLanguage.javascript);

          var myFoundItems = myObject.changeGlyph();

          //Reset the find/change glyph preferences after each search.

          app.changeGlyphPreferences = NothingEnum.nothing;

          app.findGlyphPreferences = NothingEnum.nothing;

      }

      function myFindFile(myFilePath){

          var myScriptFile = myGetScriptPath();

          var myScriptFile = File(myScriptFile);

          var myScriptFolder = myScriptFile.path;

          myFilePath = myScriptFolder + myFilePath;

          if(File(myFilePath).exists == false){

              //Display a dialog.

              myFilePath = File.openDialog("Choose the file containing your find/change list");

          }

          return myFilePath;

      }

      function myGetScriptPath(){

          try{

              myFile = app.activeScript;

          }

          catch(myError){

              myFile = myError.fileName;

          }

          return myFile;

      }

       

      AND the associated text file is like this:–

       

      //FindChangeList.txt

      //A support file for the InDesign JavaScript FindChangeByList.jsx

      //

      //This data file is tab-delimited, with carriage returns separating records.

      //

      //The format of each record in the file is:

      //findType<tab>findProperties<tab>changeProperties<tab>findChangeOptions<tab>description

      //

      //Where:

      //<tab> is a tab character

      //findType is "text", "grep", or "glyph" (this sets the type of find/change operation to use).

      //findProperties is a properties record (as text) of the find preferences.

      //changeProperties is a properties record (as text) of the change preferences.

      //findChangeOptions is a properties record (as text) of the find/change options.

      //description is a description of the find/change operation

      //

      //Very simple example:

      //text    {findWhat:"--"}    {changeTo:"^_"}    {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, wholeWord:false}    Find all double dashes and replace with an em dash.

      //

      //More complex example:

      //text    {findWhat:"^9^9.^9^9"}    {appliedCharacterStyle:"price"}    {include footnotes:true, include master pages:true, include hidden layers:true, whole word:false}    Find $10.00 to $99.99 and apply the character style "price".

      //

      //All InDesign search metacharacters are allowed in the "findWhat" and "changeTo" properties for findTextPreferences and changeTextPreferences.

      //

      //If you enter backslashes in the findWhat property of the findGrepPreferences object, they must be "escaped"

      //as shown in the example below:

      //

      //{findWhat:"\\s+"}

      //

      text    {findWhat:"<<A>>"}    {changeTo:"<<A>>”, appliedParagraphStyle:"A Header"}    {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, wholeWord:false}    Find A tag and change paragraph style to A Header.

      text    {findWhat:"<<B>>"}    {changeTo:"<<B>>", appliedParagraphStyle:"B Header"}    {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, wholeWord:false}    Find B tag and change paragraph style to B Header.

      text    {findWhat:"<<b1>>"}    {changeTo:"<<b1>>", appliedParagraphStyle:”Body Bullet“}    {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, wholeWord:false}    Find b1 tag and change paragraph style to Body Bullet.

      text    {findWhat:"<<b2>>"}    {changeTo:"<<b2>>", appliedParagraphStyle:”Body Bullet 2“}    {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, wholeWord:false}    Find b2 tag and change paragraph style to Body Bullet 2.

      text    {findWhat:"<<A>>"}    {changeTo:""}    {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, wholeWord:false}    Find A tag and delete.

      text    {findWhat:"<<B>>"}    {changeTo:""}    {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, wholeWord:false}    Find B tag and delete.

      text    {findWhat:"<<b1>>"}    {changeTo:""}    {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, wholeWord:false}    Find b1 tag and delete.

      text    {findWhat:"<<b2>>"}    {changeTo:""}    {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, wholeWord:false}    Find b2 tag and delete.

       

      AS I SAID BEFORE, this used to work in earlier versions of InDesign and I'm absolutely stumped now! Many thanks.

        • 1. Re: FindChangeByList stopped working
          Ham1000 Level 1

          Sorry, one further thing: this is the error I get in InDesign whenever I try to run the script these days:–

           

          Screen Shot 2018-07-09 at 15.19.29.png

          • 2. Re: FindChangeByList stopped working
            Steve Werner Adobe Community Professional & MVP

            Moving to InDesign Scripting forum

            • 3. Re: FindChangeByList stopped working
              Ham1000 Level 1

              Thank you Steve

              • 4. Re: FindChangeByList stopped working
                S Hopkins Adobe Community Professional

                Just tested script (copied and pasted from above). It works fine. I just tested for the one paragraph style (others should work as well). Here is what was in my FindChangeList.txt plain text file:

                text {appliedParagraphStyle:"<<A>>"} {appliedParagraphStyle:"A Header"} {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, wholeWord:false} Find all text with paragraph style A and replace with paragraph style A Header

                 

                For convenience I did change line 156 of the script to read as follows so I could save the plain text file to my desktop:

                    var myFindChangeFile = myFindFile("~/Desktop/FindChangeList.txt");

                 

                Check your plain text file. All you need is a typo and the script will error on the line you describe above. For instance: Put a space in the paragraph style reference as in: {applied ParagraphStyle:"<<A>>"} or misspell as in: {appliedParagraphStye:"<<A>>"} and it will error.

                • 5. Re: FindChangeByList stopped working
                  Ham1000 Level 1

                  Hi S.

                   

                  Firstly, thanks for taking the time to test the script for me. The fact that the section you copied worked fine for you made me have a closer look at it. Using your example (without any additional instructions in the script), I was able to get the A Header to change correctly, although I slightly altered what you'd written to:

                   

                  text    {findWhat:"<<A>>"}    {appliedParagraphStyle:"A Header"}    {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, wholeWord:false}    Find A tag and change paragraph style to A Header.

                   

                  I think the changeTo:"<<A>>” that I had before the appliedParagraphStyle bit was causing a problem (?) so removed that to match your example and the error stopped.

                   

                  Your advice to check the text file for a typo was great too... At some stage over the last couple of years in my template files I'd changed the name of the bullet point paragraph style from "Body Bullet" to "Bullet List" – so the script was being asked to change the text to a paragraph style that no longer existed!! So I amended the name in the .txt file to match and the script works fine again.

                   

                  Thank you so much again for the help. It's a problem that I've been putting down to completely the work thing for months/years!