Le risposte alle domande sul Mac

AppleScript: basic info and syntax

Main topics: ResourcesMacGuides

Apple topics: Applescript

Author: Marco Balestra

AppleScript is a scripting language that can be used as a kind of "glue" between the system and the Mac applications.

Apple describes it as a tool for repetitive tasks, in fact it a lot more than this: AppleScript is an object oriented language that interacts with all of the objects (and methods) that the system and the applications provide.

Here you will find some basic AppleScript concepts.

This is neither an official guide nor a reference: AppleScript is quite a lot complex and usable for this purpose. Here are only some basic concepts.


In order to start with AppleScript we need to use the Script Editor.
You can launch it on Mac OS X from /Applications/ApleScript/Script Editor

Script Editor

“on” - the events (and the comments)

Everything happens in AppleScript is in response to some event.

The most common event is the script’s run, that will look like this:

on run -- some code end

The handle "run" is invoked when the script starts.

When this handler is onvoked all of the code inside it is executed.

The double hyphen marks a one-line comment, while multi-line comments are marked with (* ... *)

on run (* some code *) end

There are several built-in events, and in addition to those events the user can definre his own events, and pass them parameters:

on run myEvent("ciao") end on myEvent(someText) display dialog (someText) end

built-in basic events

Some of the buil-in events used in AppleScript:

on run -- invoked when the script is launched end on open listVariable (* invoked when the script is opened using Drag&Drop. The name of variable "listVariable" is user defined, and will be a list of references to dragged objects *) end on quit -- invoked before the script quits. -- In order to quit effectively, needs the command: continue quit end on idle (* If the script is saved with "No exit", this handle is invoked once "run" or "open" exit. *) -- If you want to sleep before next idle use: return 10 -- where "10" is the number of seconds end

Type of data, “set” and “copy”

AppleScript provides some basic type of data built-in in the code:

-- integer (integer numbers) set x to 5 -- real (real numbers) set x to 3.57 -- characters (single characters) set x to "f" -- strings set x to "The Hitch-hicker's Guide To The Galaxy" -- lists (array) set x to {"foo",2,3.57}

This introduces the "set" command, that can be used to set the value of some variable to something.

Another way to set the value of a variable is "copy":

copy "Hello world" to x

The main diffence between the two is that "set" creates a reference, while "copy" copies the value of the expression into the given variable.

In case of simple values (like the ones listed above) the actual effect is exactly the same, while for more complex objects there is a severe difference.

Powered by JBLOUD, © 2021 altersoftware.IT