Re: Targeting Script Objects
Re: Targeting Script Objects
- Subject: Re: Targeting Script Objects
- From: Bill Cheeseman <email@hidden>
- Date: Fri, 26 Oct 2001 16:53:25 -0400
on 10/26/01 3:36 PM, Bourque, Jason at email@hidden wrote:
>
Why won't the below compile? If I comment out the line "tell beeper to
>
beepTheBeeper" it will
>
>
>
>
tell me to beepTheScript()
>
>
on beepTheScript()
>
>
tell beeper to beepTheBeeper
>
>
my beepTheBeeper()
>
>
script beeper
>
on beepTheBeeper()
>
beep 4
>
end beepTheBeeper
>
end script
>
>
end beepTheScript
You can make it compile by adding "my" to "tell beeper...", so that the line
reads "tell my beeper to beepTheBeeper".
But it won't run, first, because you didn't put a pair of parentheses around
the first call to beepTheBeeper, and second, when you add the parentheses,
because it won't recognize the beepTheBeeper() command.
The reason it won't recognize the script object's beepTheBeeper() handler is
that a script declared inside a handler doesn't exist as a script object
until the handler is run. You put the script definition at the end of the
handler, so it doesn't exist yet when you try to call its beepTheBeeper()
handler.
AppleScript doesn't "instantiate" script objects when they're defined, but
only when they're run. A script defined in an implicit run handler at the
top level of a script will be instantiated when the script is run. A script
defined in a handler will be instantiated when the handler is run (i.e.,
when it is called).
So you can make your script work by moving the script definition to the top
of the handler, where it will run first and be instantiated before you call
its beepTheBeeper() handler. You won't need the "my" then, either. (But
you'll have to comment out the second attempt to call beepTheBeeper(),
because the statement "my beepTheBeeper()" assumes that there is a
beepTheBeeper() handler at the top level of the script, which there isn't.)
So, this version of your script works:
tell me to beepTheScript()
on beepTheScript()
script beeper
on beepTheBeeper()
beep 4
end beepTheBeeper
end script
tell beeper to beepTheBeeper()
--my beepTheBeeper()
end beepTheScript
The more traditional way to do what I think you're up to is as follows. Note
that the makeBeeperScript handler implicitly returns the script object as
its result.
set beeperScript to makeBeeperScript()
tell beeperScript to beepTheBeeper()
on makeBeeperScript()
script beeper
on beepTheBeeper()
beep 4
end beepTheBeeper
end script
end makeBeeperScript
You'll find a mini-tutorial on the basic methods for creating script objects
in the Script Object Timer script in the "Scripts" chapter of The
AppleScript Sourcebook, here:
<
http://www.applescriptsourcebook.com/scripts/scriptobjecttimer/description.
html>. The comments to "METHOD 4" in the script explain why your script
doesn't work and why the makeBeeperScript() technique does work, with
references to the relevant portions of the AppleScript Language Guide where
all this is laid out.
--
Bill Cheeseman - email@hidden
Quechee Software, Quechee, Vermont, USA
http://www.quecheesoftware.com
The AppleScript Sourcebook -
http://www.AppleScriptSourcebook.com
Vermont Recipes -
http://www.stepwise.com/Articles/VermontRecipes
Croquet Club of Vermont -
http://members.valley.net/croquetvermont