Re: A reference to
Re: A reference to
- Subject: Re: A reference to
- From: Brennan Young <email@hidden>
- Date: Tue, 05 Jun 2001 08:36:09 +0200
- Organization: Magic Lantern
'as reference' or 'a reference to' was just up on alt.comp.lang.applescript, and
I replied thusly:
>
Well, depending on what you're doing, it might be more or less useful.
>
>
You'll appreciate that one of the most important things you do when
>
Applscripting is telling the right object to do stuff. If the object you are
>
talking to is sufficiently deep in the containment hierarchy, you can end up
>
with quite an elaborate nested tell block. Storing a reference to an object
>
allows you to keep the path to the object later in your script without having to
>
rebuild the tell block every time.
>
>
Here's a little example:
>
>
Let's assume that you don't have any applications running, but that you have
>
BBEdit and Tex-Edit installed.
>
>
tell application "Finder"
>
set RunningApps to a reference to (application processes)
>
end tell
>
>
tell application "BBEdit 6.1"
>
activate
>
end tell
>
>
tell application "Tex-Edit Plus"
>
activate
>
make new document with data (RunningApps as string)
>
end tell
>
>
Notice that at the start of the script, BBEdit is not running, so it would not
>
be present in the Finder's 'Running Apps'.
>
>
However, when the application opens, it will automatically be present in the
>
'runningApps' variable because of the reference form, so when we get to the
>
Tex-Edit code, RunningApps already includes BBEdit, whose name will make it into
>
the Tex-Edit Plus document as proof.
>
>
Here's another example:
>
>
property finderWindows : a reference to (name of (application "Finder"'s windows))
>
>
on run
>
choose from list (finderWindows as list)
>
end run
>
>
Compile the script (this will create the reference), save as an applet, then
>
open and close different windows in the Finder (manually), running the script
>
after each change. Notice how the property is changing dynamically? This is very powerful!
>
>
You can also pass references to objects as parameters, so that a handler can
>
fish out the information it needs.
>
>
Another way to use it, is as a kind of alias - in other words, you can have
>
objects (i.e. variables) with more than one name:
>
>
set a to 1
>
set b to (a reference to a)
>
set a to 99
>
get (b as integer) -- returns 99 because b points to a
>
>
This bare bones example does not hint at the true usefulness of this, but it can
>
be especially handy if you have more than one script object working together,
>
because they can share data without using globals.
>
>
Does this help?
>
--
_____________
Brennan Young
Artist, Composer and Multimedia programmer
mailto:email@hidden
"Supposing we were discussing the art of weaving which might be developed.
Supposing people were at the stage where they could ony tie knots in string,
which gave them pleasure and might be regarded as a foretaste of weaving. If the
people only imitated the knotting phase, and in addition regarded knotting as
the entire art - when would weaving itself come into being, no much how much
pleasure there was attached to it? Certainly, knotting would have a value as
such: but it would also constitute a barrier to going further if the idea of
anything further were 'abolished' by people thinking that knots were as far as
anyone could go in textile work."
-Idries Shah