Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Repeat AppleScript

Hello, I am creating an AppleScript-based Xcode App.

This is my script:

script AppDelegate

property parent : class "NSObject"

on applicationWillFinishLaunching_(aNotification)

tell application "Finder"

if disk "Mac_Keys" exists then

(*do nothing*)

else

tell application "Finder" to sleep

end if

end tell

end applicationWillFinishLaunching_


on applicationShouldTerminate_(sender)

display dialog "Goodbye"

return current application's NSTerminateNow

end applicationShouldTerminate_

end script


How can I make the italisized part repeat every 5 seconds?

Mac mini, OS X Mountain Lion (10.8.3)

Posted on Aug 25, 2013 4:04 PM

Reply
6 replies

Aug 25, 2013 5:19 PM in response to AppleMan001

I'm not sure what you are doing - putting the computer to sleep will pause the application. You can use a timer to repeatedly call a handler/method or run it after a delay, for example:


setmyTimertocurrent application's NSTimer's timerWithTimeInterval_target_selector_userInfo_repeats_(1.0, me, "someHandler", missing value, true) -- create

current application's NSRunLoop's mainRunLoop's addTimer_forMode_(myTimer, current application's NSDefaultRunLoopMode) -- add

- or -


performSelector_withObject_afterDelay_("someHandler", missing value, 1.0)


Note that you should avoid things like repeat statements unless you are periodically handling system events, otherwise the UI will get blocked.

Aug 25, 2013 7:02 PM in response to AppleMan001

You don't necessarily need to use a repeat statement, you can call a handler that will do something and then sets a timer that calls the handler again, for example:


ondoStuff()


-- do something


performSelector_withObject_afterDelay_("doStuff", missing value, 10.0) -- do it again in ten seconds

enddoStuff

This also has the advantage of not blocking the user interface (buttons and menus still work, etc).


It would also help to know what you wanting to do, since there may be other things that can be used (getting a notification when a disk is mounted or when the system wakes up, for example).

Repeat AppleScript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.