skip to Main Content

I have several hundred lengthy applescripts to edit where I need to find and replace the following code snippet in various places in each script.

tell application "Adobe Photoshop CC 2015.5"
set myLayer to current layer of current document
if last character of mySport is "s" then
set contents of text object of myLayer to mySport & ""
else
set contents of text object of myLayer to mySport & "'s"
end if
end tell

I want to replace it with

tell application "Adobe Photoshop CC 2015.5"
set myLayer to current layer of current document
set contents of text object of myLayer to mySport & "'s"
end tell

Is there a way to write an applescript to find and replace several lines?

code screen grab

The second problem is how do I deal with the apostrophe contained inside the quotes?

You can probably tell that I’m an artist and not a developer or scripter! I tried to get an answer a while back but unsuccessfully and the problem is now become critical.

Many thanks in anticipation of an answer.

3

Answers


  1. Chosen as BEST ANSWER

    I purchased Script Debugger from Late Night Software and it enables the script to access pieces of code and replace them. Mark Alldritt was amazing in the support he offered and the software is now my "first use" destination.


  2. The best would have been to set this subroutine as a separate script library and call it it in each of your scripts. Doing so, only one change would be enough. I advice you to do this way for next time.

    I dig to find a way to make change in a script, but that’s not that easy. Script Editor as very limited capability for scripting. the work around is to use the GUI scripting, which means that any changes made by Apple in future versions may no longer work.

    The script bellow simulate your keyboard action to search & replace CurString to NewString :

    set myScript to "Users:imac27:Desktop:Testscript.scpt" -- path to your script
    
    set CurString to "Set B to 2"
    set NewString to "Set X to 5"
    
    tell application "Script Editor"
    open myScript
    activate myScript
    delay 2
    tell application "System Events"
        keystroke "f" using {option down, command down} --mode search & replace
        keystroke tab using {shift down} -- got to search area
        keystroke CurString -- set the search target
        keystroke tab -- goto replace area
        keystroke NewString -- set replace value
        -- click on menu "Replace all " which is the 7th item of "Search" menu item (=item 14th of menu "Edit")
        tell process "Script Editor" to click menu item 7 of menu of menu item 14 of menu 4 of menu bar 1
    end tell
    compile front document
    save front document
    close front document
    end tell
    

    This script opens the script, it does the search, replaces, clicks on “replace” menu, then it compiles new version, saves it and closes it. If you have many scripts, you must run it through a loop for each script.

    I tested it OK with simple line : replace “Set B to 2” by new line “Set X to 5”.

    However, your issue is more complex because you want to replace several lines, not only 1. I did not found a way to set the search area with multiple lines. I tried with CR (13) or LF (10), but it does not work. May be someone has an idea for that part ?

    Also, if you want to add a ” in your search or replace patterns, you can use the following :

    set Guil to ASCII character 34
    Set CurString to "this is a " & Guil & "s" & Guil & " between quotes"
    

    In this case, the CurString value will be : this is a “s” between quotes

    Login or Signup to reply.
  3. You are sure of your original script and the final script? In this case no hesitation to use xxd and sed below in hexadecimal script which you wrote you can test this script, no danger for your script. Naturally, you change your path and names at your convenience.

        set thePath to POSIX path of (choose file)
    
            tell application "Script Editor"
                set doc to open thePath
                save doc as "text" in POSIX file "/Users/yourname/Desktop/yourscriptold.txt"
                close thePath
            end tell
    
    
            set scp to do shell script "xxd -p -c 100000  /Users/yourname/Desktop/yourscriptold.txt " & " | sed -e 's#74656c6c206170706c69636174696f6e202241646f62652050686f746f73686f7020434320323031352e35220a736574206d794c6179657220746f2063757272656e74206c61796572206f662063757272656e7420646f63756d656e740a6966206c61737420636861726163746572206f66206d7953706f727420697320227322207468656e0a73657420636f6e74656e7473206f662074657874206f626a656374206f66206d794c6179657220746f206d7953706f727420262022220a656c73650a73657420636f6e74656e7473206f662074657874206f626a656374206f66206d794c6179657220746f206d7953706f7274202620222773220a656e642069660a656e642074656c6c#74656c6c206170706c69636174696f6e202241646f62652050686f746f73686f7020434320323031352e35220a736574206d794c6179657220746f2063757272656e74206c61796572206f662063757272656e7420646f63756d656e740a73657420636f6e74656e7473206f662074657874206f626a656374206f66206d794c6179657220746f206d7953706f7274202620222773220a656e642074656c6c#' > /Users/yourname/Desktop/yourscriptnew.txt"
    
            set scp to do shell script "xxd -r -p /Users/yourname/Desktop/yourscriptnew.txt >/Users/yourname/Desktop/yournewscript.txt"
    
                do shell script "osacompile -o " & "/Users/yourname/Desktop/temporyname.scpt" & " /Users/yourname/Desktop/yournewscript.txt"
    
    do shell script "rm -f /Users/yourname/Desktop/yourscriptold.txt "
    do shell script "rm -f /Users/yourname/Desktop/yourscriptnew.txt "
    do shell script "rm -f /Users/yourname/Desktop/yournewscript.txt "
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search