Applescript to find and replace several lines in 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 & ""
	else
		set contents of text object of myLayer to mySport & "'s"
	end if
end tell

to a much simpler version like this.


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?
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 thought I had an answer some while back and got Smile to see if I could edit the scripts more easily. But I’ve not managed to make it work!

Many thanks in anticipation of an answer.

Model: MacPro
Browser: Firefox 47.0
Operating System: Mac OS X (10.10)

I am no expert either, but I would use the following with Text Wrangler

tell application “TextWrangler”
open your file
replace "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
" using “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” searching in text 1 of front document options {starting at top:false, wrap around:true, backwards:true, case sensitive:true, match words:true, extend selection:false}
end tell

This will replace your strings.

Hi Snaplash

Using TextWrangler is fine if the script is saved as a text file (xxx.applescript).
If it’s saved as a script (xxx.scpt) TextWrangler would be of no help.

Maybe this quick and dirty code may be a starting point.

set scriptPath to choose file of type {"com.apple.applescript.script"}

tell application "Script Editor"
	open scriptPath
	delay 0.5
	set origText to contents of document 1
end tell

# Edit the next two instructions to fit your needs.
set theOldText to "set inFiles to (choose file of type {\"pdf\"} with prompt \"Choose your PDF files:\" with multiple selections allowed)
set destPosixPath to POSIX path of (choose file name default name \"Combined.pdf\" with prompt \"Save new PDF to:\")"

set theNewText to "set inFiles to (choose file of type {\"PDF\"} with prompt \"Choose your PDF files:\" with multiple selections allowed)
set destPosixPath to POSIX path of (choose file name default name \"Kombined.PDF\" with prompt \"Save new PDF to:\")"

set theResult to my remplace(origText, theOldText, theNewText)

#=====
(*
replaces every occurences of d1 by d2 in the text t
*)
on remplace(t, d1, d2)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d1}
	set l to text items of t
	set AppleScript's text item delimiters to d2
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end remplace

#=====

Yvan KOENIG running El Capitan 10.11.6 in French (VALLAURIS, France) dimanche 14 aout 2016 22:33:34

Hey eaglen,

That’s quite a big task.

Yes.

Using Smile is probably the simplest way to go.

With Smile it will be very simple to set up a job to find script files in the appropriate path, open them one at a time, do the find/replace, save, and close.

Using the Satimage.osax you could even make use of the Script Editor.app.

The other main alternative would be to use the shell. A basic proof of concept that converts a single file:

[format]
#!/usr/bin/env bash

cd ~/“test_directory/Test_Folder_01/”

Find the scripts to massage “ currently disabled.

fileList=$(find . -iname “.scpt" -o -iname ".scptd” -prune);

FILE=~/“test_directory/Test_Folder_01/test_script_01.scpt”;
scriptText=$(osadecompile “$FILE”);

read -r -d ‘’ replaceStr <<‘EOF’
tell application “BBEdit”
set text of front document to “Some Goofy Text…”
end tell
EOF

export replaceStr;

newScriptText=$(echo “$scriptText” | perl -0777 -ne ‘s!^tell application "BBEdit".+?^end tell!$ENV{replaceStr}!ms; print;’);

echo “$newScriptText”;

osacompile -o test.scpt <<< “$newScriptText”
[/format]

I would want a few sample scripts to see what you’re working with.

I’d also want to know how you’ve organized these hundreds of scripts.

I’ll post more about Smile here in a few minutes.


Chris


{ MacBookPro6,1 · 2.66 GHz Intel Core i7 · 8GB RAM · OSX 10.11.6 }
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

Hey eaglen,

Okay, here’s the basics using Smile.

As you can see it’s a whole lot easier than using the shell.

Operates on the front Finder window (recursively).

I’m not doing any error-checking at all, so be careful and run it only on test scripts “ until you’re sure it works as desired.

-Chris


--------------------------------------------------------------------------------
# Get the front Finder Window
tell application "Finder"
	set srcFolder to insertion location as alias
end tell

set fileList to list files srcFolder of extension {"scpt"} as alias with recursively

set findText to text 2 thru -2 of "
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
"
set replaceText to text 2 thru -2 of "
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
"

tell application "Smile"
	repeat with scriptFile in fileList
		set scriptWindow to open (get contents of scriptFile)
		set scriptText to text of scriptWindow
		set newScriptText to change findText into replaceText in scriptText
		set text of scriptWindow to newScriptText
		close scriptWindow with saving
	end repeat
end tell
--------------------------------------------------------------------------------

After putting the task off for some while I got Script Debugger from Late Night Software. Mark Alldritt sent me a small script that did exactly what I needed. Great people, great software…Happy days!