AppleScript command for saving the current script when run in Editor?

For testing my Event Handler scripts while I develop them, I always need to save them and then call the host app’s “reload” command so that it read the modified script file.

Currently, I have to remember to manually issue the Save command before running the script, or otherwise the host app might reload an old version of the script.

So I like to add a command to the run handler that’ll save the script if it’s being editing in either SD or SE. How would I do that other than using UI scripting?

Does the script already have a name and location?

If so…

	tell application "Script Editor" to save document 1

If not, then you’ll need to figure those out but it might look something like this:

my saveMe()

on saveMe()
	set vPath to (path to desktop as text)
	tell application "Script Editor"
		set d1Name to name of document 1 -- or whatever
		set exportText to (vPath & d1Name & ".scpt")
		save document 1 in file exportText
	end tell
end saveMe

I’m assuming that you’re running this from the script menu. Script Debugger’s syntax should be similar except it offers (requires?) as compiled script as an option and you should be able to test for current application so the handler can know how to proceed.

I have a script which I use to save a copy as a text file in a separate folder, which allows me to use spotlight to find any code (that I remember to save) — NB as text in script editor, as text script in debugger.

The only other related difference in this area between the two apps is that if you close the file after saving, script debugger seems to require an explicit without saving.

1 Like

I have a run handler in the edited script, so all I do is hit cmd-R - that’ll tell the scripted app to reload this script.

I could probably check which app is frontmost - and thereby determine whether SE or SD edits the script, and then tell either to save the front document. I just wonder if there isn’t a simple “save” command for the document, without having to provide a path. Or perhaps I can query the current path the script is saved to, and pass that back to the save command.

Script Editor has a path property which always includes the extension. The script needs to have a path (i.e. saved at some point previously) so it should be useful in this case.

set d1Path to path of document 1

That said, you can just save an existing document. This should work in either app.

save document 1

You would still need to tell the correct app to perform the save but you could write the handler to get that information at runtime.

1 Like

The is yours? If it yours, then include at its beginning the script’s saving part. It should check if this particular script is opened in SD or SE currently. Better choice is display dialog “The script is edited currently. Save it to run?”.