user3260288 user3260288 - 1 year ago 160
AppleScript Question

Replace text in an applescript using an applescript

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 & ""
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.

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download