This script addresses some hurdles when you need to change specific bits of code to, say, make a script work with a new version of its target application.
First, the AppleScript Editor can search for single words only, and that may not be enough, so you’ll get other lines which do not need changing.
Second, when you have found the 1st occurrence, and changed it, you’ll have to start the search again, and the line you just changed might still match. Finding the first one to change will take more and more time, and more and more clicks.
So, this script. It looks for a line with specific bits of code, and inserts a marking comment in front of each matching line. Now you can look for the markers, and delete each one as you work through the script.
If you can think of similar ways to use scripting, please share.
With this script, it’s about fewer search cycles, and finding only what you need to find.
(And, of course, tell me what I missed!)
(*
This inserts a marker (a comment line) before lines containing specified code
It's easily modified to change a script in various other ways
(including failing in mysterious ones - hah)
*)
set inFile to choose file -- of type (accepts list)
-- read in as text, using osadecompile
set scriptText to paragraphs of (do shell script "/usr/bin/osadecompile " & quoted form of POSIX path of inFile)
set parsedText to {} -- collect modified script, line by line
-- begin modifying code
set marker to "-- MARK" & tab & "------------------------------"
repeat with thisLine in scriptText
if (thisLine contains "set attribute") and ((thisLine contains "colDue") or (thisLine contains "colLastCompleted")) then
set end of parsedText to marker -- when a string it's not a pointer...
end if
set end of parsedText to contents of thisLine -- ... but this is a list item, so it's a pointer
end repeat
-- end modyfing code
-- list to text
set AppleScript's text item delimiters to return
set output to parsedText as text
set AppleScript's text item delimiters to ""
-- create a script object from the text
set compiledScript to (run script ("script" & return & output & return & "end script"))
-- make path for output
set outFile to inFile as text
set AppleScript's text item delimiters to "."
set outFilePath to text 1 thru text item -2 of outFile & " 1.scpt"
set AppleScript's text item delimiters to ""
-- write out as compiled script
store script compiledScript in file outFilePath
-- or overwrite original when cocky