Re: Script objects on the fly
Re: Script objects on the fly
- Subject: Re: Script objects on the fly
- From: "Arthur J Knapp" <email@hidden>
- Date: Fri, 12 Jan 2001 14:46:48 -0500
>
Date: Fri, 12 Jan 2001 13:26:15 -0400
>
From: John MacDonald <email@hidden>
>
Subject: Script objects on the fly
>
Can anyone think of a way of making a script object 'on the fly?'
>
>
Ideally, I'd like to be able to specify the name of the script and
>
add properties and methods, but being able to make a new script with
>
a given name would be the most handy.
A scripting language like JavaScript is designed to do "on-the-fly"
scripting very well. AppleScript is not. Most of the time, there are
much better and more effiecient ways to use AppleScript by using lists
and records.
Having said this, let me now show you what can be done in the "on
the fly" department. There are any number of things you could be
talking about here. I'll try to cover several of them:
on FlyScript(v1, v2, v3, v4)
script
property name : v1
property Guy : v2
property Dude : v3
property HandlerHold : v4
end script
end FlyScript
on GenericDialog()
display dialog "Whatever..."
end GenericDialog
on GenericBeep(n)
beep n
end GenericBeep
set SO_1 to FlyScript("so1", "Steve", "Jobs", GenericDialog)
set SO_2 to FlyScript("so2", "Will", "Smith", GenericBeep)
tell SO_1 to HandlerHold()
tell SO_2 to HandlerHold(3)
All of the above shows how you can create new "instances" of
script objects by using a constructor function, ("FlyScript").
Any existing property of the script "template" can be preset
by using a constructer in this way. Note that you can even
assign a handler to a script in this way.
However, this does not allow you to add properties to the new
objects that are not already defined. One way you can solve this
is to make use of record concatenation:
on FlyScript()
script
property reco : {_:missing value} -- AS record
on LeftAdd(r)
set reco to r & reco -- r must be a record
end
on RightAdd(r)
set reco to reco & r
end
end script
end FlyScript
set MyObject to FlyScript()
LeftAdd({Guy: "Jobs"}) of MyObject
RightAdd({Dude: "Steve"}) of MyObject
If you are unfamiliar with record concatenation, (it's trickier
than you think), you should read about it from the horse's mouth:
http://developer.apple.com/techpubs/macos8/InterproCom/
AppleScriptScripters/AppleScriptLangGuide/
AppleScript.a9.html#pgfId=3906
Here is an example of the difference in concatenation order:
set r1 to {a:"1", b:"2"}
set r2 to {b:"3", c:"4"}
set t1 to r1 & r2
set t2 to r2 & r1
{b of t1, b of t2} --> {"2", "3"}
The ultimate means of creating AppleScript code at runtime is to
use the "run script" command. However, be aware that it is slow,
ineffeicient, and "out of context", (meaning that variables in your
main script are not accessable to the script being comiled.)
You simply use:
set MyObject to run script "script text"
Adding properties would therefore be a case of string-parsing a
varaible containing the "source" code. Have fun playing with this,
(he said sarcastically).
--
{
Arthur J Knapp, of STELLARViSIONs ;
http://www.STELLARViSIONs.com ;
mailto:email@hidden ;
"...well the rain falls down
without my help, I'm afraid
and my lawn gets wet,
though I withheld my consent..."
}