Writing a Script, from within another Script

Hi folks. First post here. I’ve searched prior posts in the forum ( and all throughout Google for the past two days but have been unable to come up with a solution to this problem so far ).

What I’m attempting to do is to automatically write a customized script from within another AppleScript ( running X.3.5 and Script Editor v2.0 )

For Example: General Script A receives a series of variables and then automatically writes a more specific and customized Script B, based on the variables it has just received.

I’ve pretty much gotten the whole thing hashed out except for the fact that the scripts which are written by the General Script A don’t appear to be properly compiled ( and compiling them from within Script Editor doesn’t seem to help solve the problem ). What happens is that I’m left with a fully functional script which will only run in Script Editor ( and cannot be run directly from any other programs like a normal script could ). Also, no changes can be saved to it after the fact - if I do, Script Editor returns the error, “Save: Couldn’t Save Document”. I’ve openend known-good and working scripts side by side with my auto-generated scripts as a baseline to compare the two and my auto-generated scripts just don’t seem to be compiled properly ( they appear as plain text within BBEdit as opposed to the normally compiled gibberish that I see within properly generated scripts made directly by Script Editor ). I’ve even tried to make an empty script from within Script Editor and then save the subsequent code into that ‘hollow script shell’ ( just to make sure that it isn’t a Finder error causing the problems when it initially generates the empty .scpt file on the desktop ), but it unfortunately yields the same lackluster results in either case. If anyone can lend me a clue on this one I’d greatly appreciate your insights on the matter - here’s the sample code.

General Script A:

– the variable serverName is automatically received by this script during normal operations
set desktopPath to path to desktop folder as string
set filename to serverName & “.scpt”
set filepath to desktopPath & filename

tell application “Finder” to make new file at desktop with properties {name: filename, creator type:“SCPT”}
set filehandle to open for access file filepath with write permission
write "display dialog “some text here” " to filehandle
close filehandle
end tell

This spawns off a fully functioning script with the name < serverName >.scpt which shows up with the appropriate AppleScript icon on the desktop. It runs, but only in AppleScript - it cannot be called by any other app or it will simply launch Applescript and open the script code instead of running it. If I try to save it within AppleScript ( with or without any changes being made to it ), I receive the aforementioned “Couldn’t save document” error. Looking at both a known-good and my script example in BBEdit seems to indicate that my script isn’t compiled. Attempting to do so yields no errors but also yields no difference in functionality and the “Couldn’t save document” error still persists after trying to compile it.

I know I’m missing something tragically simple here, but don’t know what it is - anybody have any clues that could put me back on the right course ? Thanks in advance for any info that you guys can provide ! :wink:


Here’s an example of creating a compiled script using ‘store script’ found in the standard additions scripting additions dictionary.

set desk_path to (path to desktop) as string
set file_spec to (desk_path & “MyScript”) as file specification
set r to return
set script_text to “script” & r & “display dialog “some text here”” & r & “end script”
set the_script to (run script script_text) – make a script object from text
store script the_script in file_spec replacing yes
tell application “Finder” to activate – show the new script on desktop
activate – bring this script to front
run script file_spec – run the new script


Kel, you absofrigginlutely rock buddy ! Thanks so much - that’s exactly what I needed. :smiley: