Scripting the Script Editor?

Greetings,

First of all, I’m a big-time newbie (just so you know what you’re dealing with). :slight_smile: Having said that, I’d like to know if there’s a way to generate scripts on the fly (i.e. do some sort of processing within one script and then generate a runnable SCPT – Applescript – file). I’ve been trying to script the Script Editor, but it has been of no avail.

I hope my terse description of the problem is clear enough.

Thanks!

Elvis

yes you can generate script from within a script.

But more about your end goal would help to help you. :slight_smile:

Mark,

Duh! Of course! Silly me. Here’s an example of what I’m trying to do (note: does not work):


set commandToSave to "do shell script '/bin/echo Hello World!'"
tell application "Script Editor"
	save commandToSave as "script" in "/auto-script"
end tell

Is it clearer now?

Thanks!

Elvis

Hi Elvis,

you should create a new document, “paste” the text and save this document


set commandToSave to "do shell script \"/bin/echo Hello World!\""
tell application "Script Editor"
	make new document
	set text of document 1 to commandToSave
	save document 1 as "script" in ((path to startup disk as text) & "auto-script.scpt")
end tell

Or

set file_spec to ((path to desktop as string) & "Mystored.scpt") as file specification
store script script_text in file_spec

on script_text()
	
	do shell script "/bin/echo Hello World!"
	
end script_text

StefanK,

Thanks much! I did notice there seems to be a problem when you try to run the script using the Script Editor (the program crashes unexpectedly when it reaches the point to save the file). However, everything works smoothly when you run the script through the command line:


#!/usr/bin/osascript
on run argv
	set commandToSave to "do shell script \"/bin/echo Hello World!\""
	tell application "Script Editor"
		make new document
		set text of document 1 to commandToSave
		save document 1 as "script" in ((path to startup disk as text) & "auto-script.scpt")
		quit
	end tell
end run

I then saved the file (e.g. test) and gave it executable permissions: chmod +x test. Ran it: ./test and voila! I wonder why it keeps crashing through the Script Editor though…

Thanks again!

Elvis

That’s interesting! I didn’t realise you could save the contents of a handler that way. Normally, you’d save the contents of a script object:

set file_spec to ((path to desktop as string) & "Mystored.scpt") as file specification
store script script_text in file_spec

script script_text
	
	do shell script "/bin/echo Hello World!"
	
end script

For Elvis’s purposes, starting from text, it would be something like:

set commandToSave to "do shell script \"/bin/echo Hello World!\""
set compiledScript to (run script ("script" & return & commandToSave & return & "end script"))
store script compiledScript in file ((path to startup disk as text) & "auto-script.scpt")

Yes it is, Especially as I did it by accident, when running a test… :wink: