4 Replies Latest reply on Jul 5, 2015 7:53 AM by Obi-wan Kenobi

    TextWrangler, AppleScript and Grep search

    Obi-wan Kenobi Adobe Community Professional

      Hi all,

       

      Not really an AppleScript scripter, I would need to automatize (with [AS], no [JS]) grep searches in TextWragler to clean XML files.

       

      My beginning is:

       

      tell application "TextWrangler"

        set result to find "aaa" searching in text window 1 options {search mode:grep}

        if (found of result) then

        set text of (found object of result) to grep substitution of "xxx"

        end if

        set result to find "bbb" searching in text window 1 options {search mode:grep}

        if (found of result) then

        set text of (found object of result) to grep substitution of "yyy"

        end if

        set result to find "ccc" searching in text window 1 options {search mode:grep}

        if (found of result) then

        set text of (found object of result) to grep substitution of "zzz"

        end if

       

      end tell

       

      That doesn't work. I get an error message:

       

      Capture d’écran 2015-07-04 à 13.21.41.png

       

      … when I execute the following script:

       

      Capture d’écran 2015-07-04 à 13.22.08.png

       

      Thanks in advance! 

        • 1. Re: TextWrangler, AppleScript and Grep search
          Obi-wan Kenobi Adobe Community Professional

          … That seems to work better:

           

          tell application "TextWrangler"

            tell document of text window 1

            find "aaa" options {search mode:grep}

            if (found of result) then

            set text of (found object of result) to grep substitution of "xxx"

            end if

            find "bbb" options {search mode:grep}

            if (found of result) then

            set text of (found object of result) to grep substitution of "yyy"

            end if

            end tell

          end tell


          … Now I would need to make it work in batch!  Thanks! 

          • 2. Re: TextWrangler, AppleScript and Grep search
            Obi-wan Kenobi Adobe Community Professional

            … I'm going on:

             

            tell application "TextWrangler"

              tell document of text window 1

                repeat with i in every line of document of text window 1

                 find "aaa" options {search mode:grep}

                 if (found of result) then

                  set text of (found object of result) to grep substitution of "xxx"

                 end if

                end repeat

                repeat with i in every line of document of text window 1

                 find "bbb" options {search mode:grep}

                 if (found of result) then

                  set text of (found object of result) to grep substitution of "yyy"

                 end if

                end repeat

                repeat with i in every line of document of text window 1

                 find "ccc" options {search mode:grep}

                 if (found of result) then

                  set text of (found object of result) to grep substitution of "zzz"

                 end if

                end repeat

              end tell

            end tell


            … But I have to launch the script x times! 

            • 3. Re: TextWrangler, AppleScript and Grep search
              greenrookie Level 2

              Obi-wan, try this.

               

              tell application "TextWrangler"

                  tell text 1 of text window 1

                      replace "aaa" using "xxx" options {search mode:grep, starting at top:true}

                      replace "bbb" using "yyy" options {search mode:grep, starting at top:true}

                      replace "ccc" using "zzz" options {search mode:grep, starting at top:true}

                  end tell

              end tell

               

              Greenrookie

              • 4. Re: TextWrangler, AppleScript and Grep search
                Obi-wan Kenobi Adobe Community Professional

                Hi greenrookie,

                 

                So simple and so clever!

                It works fine! Thanks a lot!    Now I can add all the grep find-replace(s) as I want.