Re: Timers in AppleScript?
Re: Timers in AppleScript?
- Subject: Re: Timers in AppleScript?
- From: Paul Skinner <email@hidden>
- Date: Mon, 15 Jul 2002 12:51:18 -0400
You could simply put the time of the current date into a variable
and then compare it to the current time later on in your script.
set currentTime to (time of (current date))
-->45833
I use this script object.
script Stopwatch
--Paul Skinner April 17, 2002
--Set up to 32 individual timers and get their elapsed times at
multiple points.
--Overhead is .0007 seconds per call to 'Stopwatch's
elapsedTime(timerId)'. (667Mhz PB G4 Ti. OS version 10.1.5. Applescript
verion 1.8.3)
--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}
property
lapTimeList : {{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {},
{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}}
property ms : false --Attempts to use the GetMiliSec OSAX if it is
installed.
on startTimer(timerId) --Returns the timer ID for reference.
try
set currentTime to GetMilliSec
set ms to true
on error
set currentTime to (time of (current date))
end try
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(s). Returned value is seconds as real.
try
set currentTime to GetMilliSec
set ms to true
on error
set currentTime to (time of (current date))
end try
set thisTimersElapsedTime to {}
repeat with thisTimerId in timerId as list
if not ms then
set thisTimersElapsedTime to thisTimersElapsedTime &
currentTime - (item thisTimerId of startTimes)
else
set thisTimersElapsedTime to (thisTimersElapsedTime &
currentTime - (item thisTimerId of startTimes)) / 1000
end if
end repeat
try
return thisTimersElapsedTime as integer
on error
return thisTimersElapsedTime
end try
end elapsedTime
on laptimer(timerId) --sets a laptimer for up to 32 laps
repeat with thisTimerId in timerId as list
set currTime to my elapsedTime(thisTimerId)
set the end of item thisTimerId of lapTimeList to currTime
end repeat
return currTime
end laptimer
on clearLapTimer(timerId) --Resets a lap timer.
repeat with thisTimerId in timerId as list
set item thisTimerId of lapTimeList to {}
end repeat
return timerId
end clearLapTimer
on lapTimesOfTimer(timerId) --Returns the elapsed times of a given
timer.
try
return item timerId of lapTimeList
end try
end lapTimesOfTimer
end script
--begin Stopwatch demo script
startTimer({1, 2, 3}) of Stopwatch --you can start multiple timers
synchronously.
delay 2
display dialog Stopwatch's elapsedTime(1) --Return the current elapsed
time.
laptimer(1) of Stopwatch --Record the current elapsed time.
startTimer(2) of Stopwatch --You can reset a running timer by restarting
it.
delay 1
display dialog Stopwatch's elapsedTime(2)
laptimer(1) of Stopwatch --Ammend another current elapsed time to a
given timer's lap time list.
Stopwatch's lapTimesOfTimer(1) --Return all of the lap times for a given
timer.
-->{3.338, 5.719}
--end Stopwatch demo script
On Monday, July 15, 2002, at 11:13 AM, Albert Atkinson wrote:
How would I go about setting up a timer in an applescript?
Thanks!
--
Can't make "old8tracks" into a record.
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.