• 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: delay doesn't work when applet is not frontmost
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: delay doesn't work when applet is not frontmost


  • Subject: Re: delay doesn't work when applet is not frontmost
  • From: Shane Stanley <email@hidden>
  • Date: Fri, 06 Mar 2015 10:56:49 +1100

On 6 Mar 2015, at 10:17 am, Stockly, Ed <email@hidden> wrote:

I could, but it's so much simpler to use a repeat/delay for the one
situation and idle for the other.

We shouldn't be having this conversation: delay should just work. And presumably it will be fixed at some stage. However, it's a command with serious form: it's had bugs on and off over many OS releases, so another problem shouldn't come as a total surprise. On that basis, I think trying to avoid it where possible, without being ridiculous about it, is a good exercise in future-proofing.

As well as the idle handler, with Yosemite you can also use timers in stay-open scripts. You just schedule a timer, and it will call a specified handler in your script after a specified delay. It's mostly fairly simple:

use framework "Foundation"
current application's NSTimer's scheduledTimerWithTimeInterval:2.0 target:me selector:"timerFired:" userInfo:(missing value) repeats:true

The time interval is obvious, the target is the script containing the handler, selector is the name of the handler, which must take a single argument of the timer itself, and repeats specifies whether it should run once or repeatedly until invalidated. So with the above, you might have a handler like:

on timerFired:theTimer
set theResult to display dialog "The timer fired" buttons {"Enough Already", "Keep it Up"} default button 2 giving up after 1
if button returned of theResult is "Enough Already" then theTimer's invalidate()
end timerFired:

That should be pretty obvious. And the beauty of this approach, like that of using idle handlers, is that no resources are being consumed during the delay.

But the catch -- and isn't there always one -- is that the script needs to be running in the foreground. That's always the case in applets, but it's rarely the case in script editors. In Script Editor, you can force the issue using control-command-R, or holding the control key so that Run in the Script menu changes to Run in Foreground. In ASObjC Explorer, you check the Run in foreground checkbox.

To avoid accidentally running it not in the foreground, precede the timer by this code:

if not (current application's NSThread's isMainThread()) as boolean then
display alert "This script must be run from the main thread." buttons {"Cancel"} as critical
error number -128
end if

And yes, it can be done via a script library in Mavericks. It's a bit more complicated, but there's a sample you can download from the ASObjC Explorer Web page.

-- 
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>

 _______________________________________________
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

References: 
 >delay doesn't work when applet is not frontmost (From: "Stockly, Ed" <email@hidden>)
 >Re: delay doesn't work when applet is not frontmost (From: Shane Stanley <email@hidden>)
 >Re: delay doesn't work when applet is not frontmost (From: "Stockly, Ed" <email@hidden>)
 >Re: delay doesn't work when applet is not frontmost (From: Shane Stanley <email@hidden>)
 >Re: delay doesn't work when applet is not frontmost (From: Shane Stanley <email@hidden>)
 >Re: delay doesn't work when applet is not frontmost (From: "Stockly, Ed" <email@hidden>)
 >Re: delay doesn't work when applet is not frontmost (From: Shane Stanley <email@hidden>)
 >Re: delay doesn't work when applet is not frontmost (From: "Stockly, Ed" <email@hidden>)

  • Prev by Date: Re: delay doesn't work when applet is not frontmost
  • Next by Date: Re: Recurring Reminders/Calendar Events?
  • Previous by thread: Re: delay doesn't work when applet is not frontmost
  • Next by thread: Re: delay doesn't work when applet is not frontmost
  • Index(es):
    • Date
    • Thread