To change a script - an assistant

Quite possibly the majority of MacScripters do not perceive these 'hurdles' as I do. But I know they exist - the people, and their hurdles. In the same vein I asked about a [url=http://macscripter.net/viewtopic.php?id=39138]routine to convert variables with underscores to camelCase[/url]. Just an awareness-raiser, nothing more.

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

Hello!

It is a great idea, and the thought has been scurrying in my head to have something like that.

I have taken it a bit further. As I was just checking on it. This is something for me :smiley:

Say you mark your code like you do, then afterwards, you save it as .applescript (text) then you open the file in TextWrangler, choose find and mark all from the thin bar longest to the right, and mark your CHANGE comment.

as long as you don’t remove your markers, you will then have a nice overview over the places you want to edit, that does not need to contain the same names or anything, implying you can make several runs with your script should you need to, and edit it in TextWrangler (without syntax highlightning :frowning: ) But still be able to run it :smiley:

I state that I made an RCS script for Tiger here, it is time to port it, so I have to do it! :smiley: