Scripting the Script Editor and the save command

I have been goofing around with scripting Script Editor, but I cannot get the syntax right to make it save a script. Here is the latest version of my script:

set desk_path to path to desktop folder as string
set save_file to desk_path & "Script001.scpt"
tell application "Script Editor"
	set doc to make new document
	set text of doc to "--This script brought to you by another script!
set this_list to {}
 repeat with j from 1 to 3 
 display dialog  j 
 end
"
	compile doc
	execute doc
	save doc in save_file as script
end tell

I have tried all variations of using either the desktop folder alone, the “script001” without the “.scpt” and a bunch of other things. The dictionary seems pretty clear that you have to point it to a file to save, but I am getting nowhere. Any ideas?

Model: Powermac dual G4
AppleScript: 1.9.3
Browser: Firefox 1.0
Operating System: Mac OS X (10.3.9)

Try specifying the file type as a string: “script”, “application”, “script bundle”, “application bundle” or “text”. So, in place of:

save doc in save_file as script

…use something like:

save doc in file save_file as "script"

kai:

That fix works perfectly; in fact, the most vital fix was the word ‘file’ before the save_path variable. The result is the same when you specify as “script” or just leave it off. It still will not function with the plain as script (without quotes). I will play around with the “application” format sometime.

Thank you for the gentle reminder of using the term file. I often forget that one and the term folder from time to time and it can be infuriating.

Craig Smith

That seems to be the case, Craig. I believe it’s because, in this case, the extension “.scpt” is already appended to the filename - and so this tends to override the as “whatever” statement. Apparently, if you specify a filename without an extension, then the ‘as file type’ parameter kicks in. (And when no filename extension or file type is specified, the default file type is a compiled script.) However, I’m personally a little cautious about saving files without an extension, so I’d probably specify that - and leave out the file type specification.

To do this, simply changing the file’s name extension to “.app” should do the trick:

set file_name to "Script001.app"
set folder_path to path to desktop folder as Unicode text
tell application "Script Editor"
	tell (make new document)
		set its text to "--This script brought to you by another script!
repeat with j from 1 to 3 
display dialog j 
end"
		save in file (folder_path & file_name)
	end tell
	close window 1
end tell

Don’t forget that you can also achieve similar results without Script Editor - by using the Scripting Commands from Standard Additions:

script toBeInstalled
	--This script brought to you by another script!
	repeat with j from 1 to 3
		display dialog j
	end repeat
end script

set file_name to "Script001.app"
set folder_path to path to desktop folder as Unicode text
store script toBeInstalled in file (folder_path & file_name)

Actually, the above scripts seem to work here without the ‘file’ - but a file specification might generally be a little safer than a string. (Incidentally, while talking of safety, it’s usually considered a good idea to coerce aliases to paths with ‘as Unicode text’, rather than ‘as string’ - just in case any ‘exotic’ characters have been used to name files or folders.) :slight_smile:

kai:

Thank you for the explanation. As luck would have it, I never tried running the script without the AS SCRIPT at the end, although I agree with you that putting the file extension on is always best.

I am terribly excited about that last script you described; it appears much cleaner and leaner than using Script Editor. I am a bit disappointed that my Applescript books did not discuss that particular technique. (Or, perhaps I just have not read that far.)

I am not familiar with the command ‘as Unicode text,’ but I have seen it in a few scripts I have downloaded. Initially I was under the impression that it was similar to the POSIX path command (which I have used for running the cp and mv shell commands) so I appreciate the clarification and will use it instead of ‘as string.’ Again, it seems cleaner.

I appreciate all your input and the time you spent explaining your reasoning.

Craig Smith