Saturday, April 29, 2017

#1 2017-02-03 10:21:40 am

Paralysis
Member
Registered: 2016-12-08
Posts: 26

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)

Offline

 

#2 2017-02-03 10:42:39 am

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11438
Website

Re: Standalone Timer in XCode that Doesnt Interfere with Applescript

Hi,

Yes, it's pretty easy


Create two properties for the NSTimer instance and the counter

Applescript:

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

Applescript:

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

Applescript:

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

Last edited by StefanK (2017-02-03 10:45:08 am)


regards

Stefan

Offline

 

#3 2017-02-06 06:26:36 am

Paralysis
Member
Registered: 2016-12-08
Posts: 26

Re: Standalone Timer in XCode that Doesnt Interfere with Applescript

Hi Stefan and thanks for the reply.

That all looks great!
I will give that a try today 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

Offline

 

#4 2017-02-06 09:13:22 am

Paralysis
Member
Registered: 2016-12-08
Posts: 26

Re: Standalone Timer in XCode that Doesnt Interfere with Applescript

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?:

Applescript:


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

Offline

 

#5 2017-02-06 10:16:52 am

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11438
Website

Re: Standalone Timer in XCode that Doesnt Interfere with Applescript

Paralysis wrote:

Is the NS Timer completelty separate to Applescript then?

Yes

Paralysis wrote:

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?

Yes

Paralysis wrote:

Also on the back of that message is it possible to reset the timer back to the start once it hits 0?

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

Applescript:

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.


regards

Stefan

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.071 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)