Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
AppleScript
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Handler=== A '''handler''', a variation of the block structure defines a subroutine. ;Function handler <syntaxhighlight lang="AppleScript" line> on myFunction(parameters...) -- subroutine commands end myFunction </syntaxhighlight> ;Folder actions block <syntaxhighlight lang="AppleScript" line> on adding folder items to thisFolder after receiving theseItems -- commands to apply to the folder or items end adding folder items to </syntaxhighlight> ;Run handler <syntaxhighlight lang="AppleScript" line> on run -- commands end run </syntaxhighlight> Handlers can also be defined using "to" in place of "on" and can be written to accept labeled parameters, not enclosed in parens. ;Handler with labeled parameters <syntaxhighlight lang="AppleScript">on rock around the clock display dialog (clock as string) end rock -- called with: rock around the current date </syntaxhighlight> ;Handler using "to" and labeled parameters <syntaxhighlight lang="AppleScript"> to check for yourNumber from bottom thru top if bottom ≤ yourNumber and yourNumber ≤ top then display dialog "Congratulations! You scored." end if end check --called with: check for 8 from 7 thru 10 </syntaxhighlight> There are four types of predefined handlers in AppleScript—run, open, idle, and quit—each of which is created in the same way as the run handler shown above. ;Run handler: Defines the main code of the script, which is called when the script is run. Run handler blocks are optional, unless arguments are being passed to the script. If an explicit run handler block is omitted, then all code that is not contained inside handler blocks is executed as though it were in an implicit run handler. ;Open handler: Defined using "on open theItems". <syntaxhighlight lang="AppleScript">on open theItems repeat with thisItem in theItems tell application "Finder" to update thisItem end repeat end open</syntaxhighlight> When a script containing an "open handler' is saved as an applet, the applet becomes a droplet. A droplet can be identified in the Finder by its icon, which includes an arrow, indicating items can be dropped onto the icon. The droplet's open handler is executed when files or folders are dropped onto droplet's icon. References to the items dropped on the droplet's icon are passed to the droplet's script as the parameter of the open handler. A droplet can also be launched the same way as an ordinary applet, executing its run handler. ;Idle handler: A subroutine that is run periodically by the system when the application is idle. <syntaxhighlight lang="AppleScript">on idle --code to execute when the script's execution has completed return 60 -- number of seconds to pause before executing idle handler again end idle</syntaxhighlight> An idle handler can be used in applets or droplets saved as stay-open applets, and is useful for scripts that watch for particular data or events. The length of the idle time is 30 seconds by default,<ref>{{cite web |url=https://developer.apple.com/library/mac/#documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_about_handlers.html#//apple_ref/doc/uid/TP40000983-CH206-SW14 |title=AppleScript Language Guide: Handlers in Script Applications |website=developer.apple.com |access-date=July 21, 2013}}</ref> but can be changed by including a 'return x' statement at the end of the subroutine, where x is the number of seconds the system should wait before running the handler again. ;Quit handler: A handler that is run when the applet receives a Quit request. This can be used to save data or do other ending tasks before quitting. <syntaxhighlight lang="AppleScript">on quit --commands to execute before the script quits continue quit -- required for the script to actually quit end quit</syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)