When to use delay, pause or return in AppleScript?

Discussion in 'Mac Programming' started by nelly22, Jul 31, 2016.

  1. nelly22 macrumors 6502

    Joined:
    Sep 29, 2009
    #1
    What's the difference between delay, pause and return in idle handler?

    Code:
    on idle
         delay 60
    end idle
    
     
  2. Red Menace macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #2
    Delay is a command that causes the script to wait for the specified number of seconds before continuing. The return statement exits a handler (with an optional value) and returns to the caller. In the case of the idle handler, the number returned becomes the rate (in seconds) that the handler is called (in a stay-open application, the idle handler is called repeatedly while the script is idle).
     
  3. nelly22 thread starter macrumors 6502

    Joined:
    Sep 29, 2009
    #3
    Thanks.

    Years ago in some forum people debated which is best.

    What i should use if i want script to use CPU cycles as little as possible during idle.
     
  4. Red Menace macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #4
    You would want to use some kind of notification or timer so that your application sits idle until the system calls it - you don't want to poll using a repeat loop or a delay, since those will eat up cycles as well as block the user interface. The idle handler basically sets a timer, but you have access to most of the Cocoa API via AppleScriptObjC, so you can also use Cocoa timers and notifications.
     
  5. nelly22, Jul 31, 2016
    Last edited: Jul 31, 2016

    nelly22 thread starter macrumors 6502

    Joined:
    Sep 29, 2009
    #5
    Thanks.

    These timers and notifications sounds cool, but i have no clue how to use these.

    Is there anywhere total newbie friendly step by step instructions how to use these?
     
  6. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #6
    Alternatively, you could use a LaunchAgent/LaunchDaemon. First, make sure your script can be run by the command line using osascript.

    Code:
    osascript /Path/To/Your/Script.scpt
    Next, take a look here at how to execute that command at a particular time/interval.

    http://www.thesafemac.com/scheduling-recurring-tasks/

    Hope that helps!
     
  7. Red Menace macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #7
    Until you get more familiar, I would just make a stay-open application and go with the idle handler. This handler is run after the main run handler completes, and repeats at intervals that you can change by returning a different number (the number of seconds until it fires again). It is basically a timer loop, but without all the Cocoa-y confusion. While your application is running, you can use the Activity Monitor to check out its cpu usage and tweak the timing accordingly.
     

Share This Page