5 Replies Latest reply on Aug 27, 2008 10:05 PM by Kasyan Servetsky

    Find and Replace scripting

      Hi all,

      I'm new to scripting and am wondering how to create a script that find and replaces different variables for me. I work with a lot of spreadsheets and always put in little symbols like æ, £, §, ¥, µ, etc... so that later I can find that symbol and replace it with a ^n, ^t, or ^p.

      I liked using actions in Photoshop because it was easy to record what I was doing then have a quick action that way.

      Can someone help me create a script that does the following?:

      Find ^p£^t, Replace with ^p
      then
      Find ^tØ^t, Replace with ^>/^>
      end tell

      Thanks,
      tony
        • 1. Re: Find and Replace scripting
          Kasyan Servetsky Level 5
          Hi Tony,

          It’s not tested, but I hope it will work. You can add your find/replace stuff into myTxtReplace array: ["FindWhat", "ChangeTo"]

          Kasyan
          _________________________


          var myDoc = app.activeDocument;
          // put all your find/replace stuff into array ["FindWhat", "ChangeTo"]
          var myTxtReplace =   [
                                  ["^p?^t", "^p"],
                                  ["^tO^t", "^>/^>"],
                                  ]
          replaceTxt();

          function replaceTxt() {
             app.findTextPreferences = NothingEnum.nothing;
             app.changeTextPreferences = NothingEnum.nothing;
             app.findChangeTextOptions.wholeWord = false;
             app.findChangeTextOptions.caseSensitive = true;
             app.findChangeTextOptions.includeMasterPages = false;
             for (i = 0; i <  myTxtReplace.length; i++) {
                var myCurArray = myTxtReplace[i];
                app.findTextPreferences.findWhat = myCurArray[0];
                app.changeTextPreferences.changeTo = myCurArray[1];
                myDoc.changeText();
             }
             app.findTextPreferences = NothingEnum.nothing;
             app.changeTextPreferences = NothingEnum.nothing;
          }
          • 2. Re: Find and Replace scripting
            Kasyan Servetsky Level 5
            P.S. Characters Pound and O with stroke in lines 4 and 5 got corrupted when I posted the code, replace them manually in ESTK2. I forgot to mention that the script was written for CS3.
            • 3. Re: Find and Replace scripting
              Level 1
              Thanks so much for your help. I wouldn't have been able to figure that out by myself. The script worked perfectly.
              • 4. Re: Find and Replace scripting
                Level 1
                how about finding and replacing character/paragraph styles? is that difficult to add?
                • 5. Re: Find and Replace scripting
                  Kasyan Servetsky Level 5
                  var myDoc = app.activeDocument;
                  app.findTextPreferences = NothingEnum.nothing;
                  app.changeTextPreferences = NothingEnum.nothing;
                  // Find&Change Paragraph Styles
                  app.findTextPreferences.appliedParagraphStyle = myDoc.paragraphStyles.item("Paragraph Style 1");
                  app.changeTextPreferences.appliedParagraphStyle = myDoc.paragraphStyles.item("Paragraph Style 2");
                  myDoc.changeText();
                  app.findTextPreferences = NothingEnum.nothing;
                  app.changeTextPreferences = NothingEnum.nothing;
                  // Find&Change Character Styles
                  app.findTextPreferences.appliedCharacterStyle = myDoc.characterStyles.item("Character Style 1");
                  app.changeTextPreferences.appliedCharacterStyle = myDoc.characterStyles.item("Character Style 2");
                  myDoc.changeText();
                  app.findTextPreferences = NothingEnum.nothing;
                  app.changeTextPreferences = NothingEnum.nothing;