Re: Script on the fly?
Re: Script on the fly?
- Subject: Re: Script on the fly?
- From: has <email@hidden>
- Date: Mon, 5 Nov 2001 18:39:11 +0000
Serge Belleudy-d'Espinose wrote:
>
Is there any way, vanilla or with osax, to turn a straight text into a
>
compiled script, or at least a script object?
>
>
I'm trying for AppleMods to have a way of download a script as text and
>
turn it into a compiled one. What I've looked into w/o success:
>
- use the result of a 'run script' in any way
>
- imitate the resources of a compiled script
>
- scripting Script Editor (!!) to copy-paste-compile-save script.
>
>
I've the feeling that what I'm trying is _not_ possible in any way. Any idea?
I'm actually doing just this sort of thing at the moment (though for a
rather more complicated task: you can do some _very_ neat things when
you've got a script that can assemble custom script objects
on-the-fly...:).
1. Using run script is the right approach. Try this:
set theScript to "
on beepme(n)
beep n
end beepme
"--i.e. your script as text
run script "script x
" & theScript & "
end script
x"
That'll return a nice tasty script object to play with. It may be that
you've not quite grasped the full juiciness of AS' objects - but just try
stuffing scripts, handlers and other stuff into variables for a bit and
you'll soon get the hang of it.:)
2. I'm not sure what you mean by this. If you mean adding extra stuff -
e.g. property myProp : "hello" - to the script then sure: just bung it in
as a string as well:
set myExtras to "property myProp : \"hello\""
run script "script x
" & myExtras & "
" & theScript & "
end script
x"
If you want to add really complex stuff like lists or records using this
method, you'll need to coerce them to string first whilst preserving all
their braces and stuff. Here's one way if it's static content you want to
add:
script a
property x : {a:1, b:2}
end script
set myExtras to a as string
If it's something that's being assembled on the fly, you'll need to be a
bit more sophisticated. The vanilla approach is to use Richard 23's
ingenious coerceToString handler (I don't have the url, but I'm sure you
can dig it up on macsripter, or someone here is bound to know it).
Alternatively, Smile has a "display" option in its dictionary that will
render anything as a string (of course, that means you've got to include
Smile as part of your process, which might have implications for you if
you're intending to distribute this thing as a standalone tool). If you're
talking about resource forks, see 3. instead.
3. Scripting Script Editor: uh-uh. You need to get yourself a better editor.
For example: Smile is scriptable, attachable, and probably a dozen other
things that also end in -able. And it's free too if cost is a concern. Can
take a little getting used to at first if you're coming to it from plain,
predictable SE, but don't panic: if the Text Windows and all the other
extra widgets make you uncomfortable you can still work in it exactly as
you do in Script Editor - just stick to using Script Windows to author in
and treat the Worksheet as a Results window. (This is what I did, then
gradually picked up on the additional features over time. Now I can't
imagine how I ever survived in SE.<g>)
You've got access to Smiles own dictionary, which includes a "do script"
function that is much faster than using "run script" (its only weakness is
that you can't pass external parameters to it as you can with run script,
but it doesn't sound like you need this option anyway). It's also got a
bona-fide text editor at heart, so you can play with all that fancy "word 1
of paragraph 1" and "tell window 1 to save in filePath" stuff as well. If
you've scripted those before I'm sure you'll be right at home.
HTH
has
(Disclaimer: I don't have any sort of connection with Satimage, so don't
take this as a sales spiel or anything like that. I'm sure the other
3rd-party editors are also very nice, so no need to start an editor war -
really...;)