Standalone Timer in XCode that Doesnt Interfere with Applescript

Hi Guys,

Does anyone know if its possible/how to create a timer within XCode that I can trigger to count down X amount of minutes and display a visual timer going down (1:52, 1:51, 1:50 etc)
The countdown will trigger another function - So the user would specify “I want to run this function every 15mins” for example

I know its possible in Applescript (which is what my app is running currently)
From what ive read however its a little clunky for doing things like that and it would also mean that the script would be running non stop and looping meaning I prob wouldnt be able to trigger other functions/button within the app. And if I could I imagine the results could be a little unpredictable.

So my question is:
Is it possible to have a standalone script/code that could run without effect my main Applescript code.
That could feed into my MainMenu.XIB with a live countdown of when the function is going to run.

Thanks

Model: Macbook Pro
AppleScript: Xcode 8.2.1
Browser: Firefox 51.0
Operating System: Mac OS X (10.10)

Hi,

Yes, it’s pretty easy

Create two properties for the NSTimer instance and the counter

property timerCounter : 0
property countDownTimer : missing value -- NSTimer

In applicationDidFinishLaunching create the timer and set the counter, the interval is one second, the selector (handler) timerFired: is called when the timer fires

on applicationDidFinishLaunching:aNotification
	set timerCounter to 900 -- 15 minutes
	set countDownTimer to current application's NSTimer's scheduledTimerWithTimeInterval:1 target:me selector:"timerFired:" userInfo:(missing value) repeats:true
end applicationDidFinishLaunching:

In the timerFired: handler the counter is decremented. If the counter reaches 0 another handler is called. In timerFired: you can also update the UI

on timerFired:timer
	set timerCounter to timerCounter - 1
	// update UI
	if timerCounter is 0 then
		countDownTimer's invalidate()
		countDownFinished()
	end if
end timerFired:

on countDownFinished()
	log "Finished: Trigger something"
end countDownFinished

Hi Stefan and thanks for the reply.

That all looks great!
I will give that a try today :slight_smile:

Is the NS Timer completelty separate to Applescript then?
As in: If I trigger the on timerFired function and thats looping through - Can I still trigger applescript handlers to also run at the same time?

Thanks again

Also on the back of that message is it possible to reset the timer back to the start once it hits 0?
Is it just a case of doing this?:


on countDownFinished()
   log "Finished: Trigger something"
if timerCountValue is not 0 then
--timerCountValue is a value taken from a drop down box the user selects
set timerCounter to 900 -- 15 minutes
   set countDownTimer to current application's NSTimer's scheduledTimerWithTimeInterval:1 target:me selector:"timerFired:" userInfo:(missing value) repeats:true
end if
end countDownFinished

Thanks Again

Yes

Yes

Yes, but remove the check ≠0. If the handler is called from timerFired: the value guaranteed to be zero

on countDownFinished()
	log "Finished: Trigger something"
	--timerCountValue is a value taken from a drop down box the user selects
	set timerCounter to 900 -- 15 minutes
	set countDownTimer to current application's NSTimer's scheduledTimerWithTimeInterval:1 target:me selector:"timerFired:" userInfo:(missing value) repeats:true
end countDownFinished

However if you want to start and stop the timer dynamically you need to add startTimer() and stopTimer() handlers which check if the timer is not running / running respectively.