Be aware that ‘store script’ is used to store existing script objects to file, rather than to create new ones. If you wish to compile a string and run it as a script, use ‘run script’.
For example:
run script "display dialog "hello world""
To create an entire script using ‘run script’ is slightly more complex (note: you really ought to learn about AS’s script objects if you’re going to be doing this sort of thing). The following example generates very simple custom-assembled scripts and saves them to disk. :
set theName to text returned of (display dialog "Your name:" default answer "")
set theString to "
script
-- This script belongs to " & theName & "
display dialog "Hello " & theName & "!"
end script"
set theScriptObject to run script theString
store script theScriptObject in (new file with prompt "Save script as:")
This approach is useful when you want to generate a complete, bona-fide script on the fly. It’s not without its disadvantages, however. The main one is speed: in order to compile a string using ‘run script’, the AS compiler has to be loaded and unloaded each time. This is very slow; e.g. it takes about a second on my G3/300.
Here’s another approach to the problem which doesn’t need to assemble and compile strings; instead it uses a ‘constructor’ function to create script objects directly:
set theName to text returned of (display dialog "Your name:" default answer "")
set theScriptObject to newScriptObject(theName)
store script theScriptObject in (new file with prompt "Save script as:")
on newScriptObject(theName)
script
property myName : theName
display dialog "Hello " & theName & "!"
end script
end newScriptObject
The catch with this last example is that you can’t open and edit/run it from a script editor because you’ll lose the data that’s stored in property ‘myName’ if you do. It’s only suitable for storing into, and executing from, an empty applet - or for loading into another script using ‘load script’ and calling it from there. In many cases this is all you want anyway, so isn’t an issue, but you should be aware of this all the same.
So which method you use really depends on what your particular requirements are. Both have their uses.
–
BTW, you don’t need to use ‘run script’ to run a compiled script object. It’s quicker and simpler to call the script object’s ‘run’ handler from a tell block, like so:
script theScriptObject
display dialog "Hello Joe!"
end script
tell theScriptObject
run
end tell
–
This is all very powerful, complex stuff so try to spend a bit of time playing about and getting familiar with it. Try to do some background reading too: it’s a lot easier to do (and do well) if you’ve got some decent knowledge of the subject.
Oh yeah, and it also becomes easier to understand this stuff once you realise that all AS scripts are script objects, and that all script objects contain ‘run’ handlers. (This is not immediately obvious, as AS hides this sort of stuff to make things easier for beginners, but it’s there. The AppleScript Language Guide explains it all in more detail.)
HTH
has