• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Seconds until next tock
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Seconds until next tock


  • Subject: Re: Seconds until next tock
  • From: Shane Stanley via AppleScript-Users <email@hidden>
  • Date: Mon, 19 Aug 2019 14:17:04 +1000

On 18 Aug 2019, at 8:45 am, Gil Dawson via AppleScript-Users
<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>
<www.macosxautomation.com/applescript/apps/>, <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

  • Follow-Ups:
    • Which Graphics App?
      • From: Gil Dawson via AppleScript-Users <email@hidden>
    • Re: Seconds until next tock
      • From: Gil Dawson via AppleScript-Users <email@hidden>
References: 
 >Seconds until next tock (From: Gil Dawson via AppleScript-Users <email@hidden>)

  • Prev by Date: Re: Seconds until next tock
  • Next by Date: Re: Seconds until next tock
  • Previous by thread: Re: Seconds until next tock
  • Next by thread: Re: Seconds until next tock
  • Index(es):
    • Date
    • Thread