Re: ASLG page 326 - Script Objects
Re: ASLG page 326 - Script Objects
- Subject: Re: ASLG page 326 - Script Objects
- From: Paul Skinner <email@hidden>
- Date: Tue, 23 Apr 2002 09:11:19 -0400
On Tuesday, April 23, 2002, at 08:48 AM, nicolas descombes wrote:
Hello,
In "AppleScript Language Guide" page 326, it's written :
"A handler within a script object definition follows the same syntax
rules
as a subroutine definition. Unlike a subroutine definition, however,
you can
group a handler within a script object definition with properties whose
values are related to the handler's actions."
I don't understand the difference. Could someone give me a concrete
example
showing "a handler within a script object definition with properties
whose
values are related to the handler's actions".
Thanks
Nicolas
Jungian Synchronicity!
I usually don't like the form required for calls to handlers inside
Script Objects and avoid using them. Thus, I have lots of individual
handlers and no libraries.
The other night, while trying to make a nice pair of modular
script-timer handlers share some data using properties, I realized that
the call methods for S.O.' s handlers are sometimes nicer than straight
handler calls. So now I'm rethinking libraries for some purposes.
By creating a S.O. wrapper for a pair of handlers, you can define
properties within the S.O. for easy data sharing between the handlers.
This solved my issues and led to more readable handler calls and a
cleaner, simpler design. I love the OO AppleScripty goodness of
'startTimer(1) of Stopwatch'!
script Stopwatch
--Stopwatch.
--Paul Skinner April 17, 2002
--Set up to 32 individual timers and get their elapsed times at
multiple points.
--Overhead is .0009 seconds per call to 'Stopwatch's
elapsedTime(timerId)'. (400Mhz G3. OS version 10.1.2. Applescript verion
1.8.2b3)
--CAUTION: No error checking on timerId validity. This saves .0001
second/call and 12 lines of code.
property startTimes : {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
on startTimer(timerId) --Returns the timer ID for reference.
set currentTime to (time of (current date))
repeat with thisTimerId in timerId as list
set item thisTimerId of startTimes to currentTime
end repeat
return timerId
end startTimer
on elapsedTime(timerId) --Returns the current elapsed time of the
specified timer.
set currentTime to (time of (current date))
set thisTimersElapsedTime to {}
repeat with thisTimerId in timerId as list
set thisTimersElapsedTime to thisTimersElapsedTime &
currentTime - (item thisTimerId of startTimes)
end repeat
try
return thisTimersElapsedTime as integer
on error
return thisTimersElapsedTime
end try
end elapsedTime
end script
--Begin DEMO script
startTimer({1, 2, 3}) of Stopwatch --You can start multiple timers
simultaneously.
Stopwatch's elapsedTime({1, 2, 3}) --You can read multiple timers
simultaneously.
startTimer(1) of Stopwatch --You can reset a running timer.
repeat 10000 times
Stopwatch's elapsedTime(1)
end repeat
display dialog (Stopwatch's elapsedTime(1) as text) & " seconds elapsed
time for 10000 calls to 'Stopwatch's elapsedTime(1)'."
startTimer(2) of Stopwatch
repeat 10000 times
set var to 1
end repeat
display dialog (Stopwatch's elapsedTime(2) as text) & " seconds elapsed
time for 10000 'set var to 1's."
display dialog (Stopwatch's elapsedTime(1) as text) & " seconds elapsed
time overall." --You can read a timer as often as you want.
--End DEMO script
--
Paul Skinner
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.