Re: Seconds until next tock
Re: Seconds until next tock
- Subject: Re: Seconds until next tock
- From: Gil Dawson via AppleScript-Users <email@hidden>
- Date: Mon, 19 Aug 2019 13:22:35 -0700
Thanks for your help, guys. Some interesting variations.
Thanks especially, Shane, for showing us how to display the odd milliseconds.
That will be very useful.
Regarding your comment, "...and you certainly don't want to use delay"...
In a practical app, I had thought I might use the idle handler with a return
value of SecondsUntilNextTock (in my code) or theDiff (in yours). Your
suggestion to use NSTimer instead is very interesting.
--Gil
> On Aug 18, 2019, at 9:17 PM, Shane Stanley <email@hidden> wrote:
>
> On 18 Aug 2019, at 8:45 am, Gil Dawson via AppleScript-Users
> <email@hidden
> <mailto:email@hidden>> wrote:
>>
>> I needed something to execute at a regular interval, on the rounded interval
>> seconds.
>
> The normal way of having a timer fire regularly in AppleScript is to use an
> idle handler. However, the time interval is based on when the idle hander has
> finished doing whatever else it's doing, so the time between calls can vary.
> And you certainly don't want to use delay.
>
> Here's an option using AppleScriptObjC. The catch is that it must be run on
> the main thread. That means it's fine just about everywhere except when run
> from a script editor. You can run it in Script Editor by holding down the
> Control key and choosing Run in Foreground from the Script menu.
>
> use AppleScript version "2.5" -- macOS 10.11 or later
> use framework "Foundation"
> use scripting additions
>
> set theInterval to 10 -- or whatever
> set firstFire to (current date)
> set daySeconds to seconds of firstFire
> set theDiff to daySeconds mod theInterval
> set firstFire to firstFire + theInterval - theDiff
> set theTimer to current application's NSTimer's alloc()'s
> initWithFireDate:firstFire interval:theInterval target:me
> selector:"timerFired:" userInfo:(missing value) repeats:true
> current application's NSRunLoop's mainRunLoop()'s addTimer:theTimer
> forMode:(current application's NSDefaultRunLoopMode)
>
> on timerFired:theTimer
> -- build a date string showing milliseconds so we can see if it's on or
> near correct time
> set theDate to current application's NSDate's new()
> set df to current application's NSDateFormatter's new()
> df's setDateFormat:"yyyy-MM-d hh:mm:ss.SSS"
> set dateString to (df's stringFromDate:theDate) as string
> try
> display dialog dateString
> on error
> theTimer's invalidate()
> end try
> end timerFired:
>
> There are still no guarantees of accuracy
>
> --
> Shane Stanley <email@hidden <mailto:email@hidden>>
> <www.macosxautomation.com/applescript/apps/
> <http://www.macosxautomation.com/applescript/apps/>>, <latenightsw.com
> <http://latenightsw.com/>>
>
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden