Finding and replacing in multiple applescripts?

Is there a way to find and replace across multiple Applescripts? For instance, I have a lot of scripts that load my AS classes. If I were to move these scripts, is there a way to change all of the references at once or would I have to manually find each and every applescript with a “load” statement (and Spotlight does not appear to work for this), and then change them all manually?

Hi,

use relative paths (e.g. path to me refers to the script itself) or create a library script containing all paths.
Your scripts could then read the paths and initialize their properties

I did not know you could do that. Very handy. But let’s say you’ve already hardcoded links to specific file paths in a number of files, and you need to change them to something like “path to me”. Is there a way to do some sort of multifile find and replace? Do I need to try and write a script to do this?

Thanks,
Rebecca

It’s good programming habit to avoid hard-coded paths if possible
As compiled script aren’t just plain text files, I guess it’s faster to change the paths manually.

For relative paths read the Standard Additions dictionary path to (folder)

Wow this sounds awesome could you elaborate a little how you would have one script that has all the paths and then reference them from another script? I reference a lot of scripts via a hard-coded paths or relative paths and this has always freaked me out. I have tried to use alias because that sort of follows the file around, but always have felt there’s a better way.

That’s too bad there is no application that can search for text in multiple scripts? I too am trying to find code found in multiple scripts. I tried “Easy Find” and even that didn’t do the trick, I guess because it is not plain text.

You have one script that just has properties set. In your other scripts, you load that script so you can retrieve the property values.

set savedValues to load script file ((path to documents folder as string) & "SavedValues.scpt")
set theLocalVariable to savedValues's variableFromScript

As to the original question, AppleScript Editor is itself scriptable. You might be able to script opening a batch of scripts and make the changes. Check out the scripting library for AppleScript Editor. This is a script I wrote (droplet) to save run-only copies of my scripts before distributing them to the department:

on open these_items
	set PathToDesktop to path to the desktop as text
	tell application "AppleScript Editor"
		launch
	end tell
	tell application "Finder"
		activate
		if not (exists folder "Scripts RUN ONLY Copy") then
			make new folder at desktop with properties {name:"Scripts RUN ONLY Copy"}
		end if
	end tell
	
	repeat with i from 1 to the count of these_items
		set this_item to (item i of these_items)
		set the item_info to info for this_item
		tell application "System Events"
			if ((kind of item_info is "script") or (kind of item_info starts with "Application")) then
				if (kind of item_info starts with "Application") then
					set ScriptKind to "application bundle"
				else --Must be "script"
					set ScriptKind to "script"
				end if
			else --Not an AppleScript
				tell me
					activate
					display dialog "This is not an AppleScript." buttons "Skip"
				end tell
				return
			end if
		end tell
		
		tell application "AppleScript Editor"
			activate
			try
				open this_item
			on error
				display dialog "Could not process. This may not be an AppleScript. Skipping this file."
			end try
			set ScriptName to name of front document
			save front document in (PathToDesktop & "Scripts RUN ONLY Copy:" & ScriptName) as ScriptKind with run only without startup screen and stay open
			close front document
		end tell
	end repeat
	
	tell application "Finder"
		activate
		try
			open folder "Scripts RUN ONLY Copy"
		end try
	end tell
	
end open

Another thought, you could save your common paths in a central location in an XML file. Then use System Events to read the XML file.

Great stuff, thanks for the help and tips Matt. I found just found out ScriptDebugger will make your files searchable by Spotlight.

http://blog.latenightsw.com/?p=41