AppleScript is a scripting language for Mac users that allows you to automate actions and applications. It is similar to other scripting languages like Perl and PHP but currently only runs on the Mac OS. What makes Applescript especially interesting is it’s ability to control many of the Mac’s applications that include support for AppleScript (eg. PhotoShop, QuickTime, The Finder, etc…). In addition, OS X has introduced Applescript Studio which is an IDE for Applescript and allows one to build fantastic Graphical User Interfaces (GUIs) using another Apple development tool called Interface Builder. Together, these tools allow you to build complete stand-alone applications quickly and easily.
The bad news about AppleScript is that it’s syntax is rather confusing. It was written to read like spoken English and although this may help new users, it can be very difficult to adjust to for a programmer coming from another language. Most other scripting languages have many syntax similarities and are usually based upon C but this is not the case with AppleScript. Using the classic Apple mantra of “think different”, the writers of AppleScript decided to create a completely different syntax that is very arguably easier to learn. For this reason, I will attempt to draw comparisons to Applescript from the viewpoint of a PHP developer. These comparisons should also be useful to developers using other languages (eg. Perl, Python) but I use PHP here because it is most familiar to me.The Basics:
-
Comments
are created by using two dashes (eg. — ) for single line comments or parenthesis and stars (eg. (* comment *) ) for multiple line comments.
-
Variable names
(aka Identifiers ) are NOT case sensitive so the variables named MyName, myname and MYNAME are all the same.
-
AppleScripts are compiled
unlike PHP or Perl which are interpreted languages. AppleScripts need to be compiled before they are run.
-
There are no statement separators
eg. no semicolons marking the end of a statement.
-
Blocks
to a degree, are free typed (no brackets “{}”) but must end with the
endcommand. This is only partially true because AppleScript code, once compiled or checked for syntax, is automatically blocked for you. Code with syntax mistakes can not be compiled but can be saved as text.
Handlers (aka Functions):
Applescript uses the unfortunate term “handler” to refer loosely to a function. One “uses a handler” like one would “call a function” in other languages. They are defined using the “on” and “end” statements (eg. on run ... end run). These handlers come in four flavors:
-
Application Handlers
These “commands” belong to and are stored in an application itself (eg. QuickTime, PhotoShop, etc…) and have no meaning to other applications or scripts outside of the application. These handlers are known as Dictionaries and can be viewed by dropping the application’s icon on the on the Script Editor’s icon, or by opening the application with the Script Editor’s Open Dictionary handler.
-
AppleScript Handlers
These are just built-in handlers no different than built-in functions.
-
Scripting Addition handlers
These are similar to Application handlers except they are not dependant on an application. Like libraries or classes in PHP that you can include in any script, these additional handlers extend (add to) the functionality of Applescript’s regular set of handlers. They are module-like files that, once they are stored in Applescript’s Scripting Additions folder, allow the added handlers to be called from any script.
-
User-Defined Handlers
You make it and you can use it…
Variables:
Variables in AppleScript are dynamically typed just like in PHP. You don’t need to define a type (eg. String, int, float…) when the variable is declared although you can if you wish.
To assign a value to a variable, use the set or copy handlers. Example:
set myName to “Dasspunk”
copy “Dasspunk” to myName
Set MYNAME to “Dasspunk” as string
string using the as command (known in AS as coercion). AppleScript has many of the usual variable types (as well as some unusual ones) including string, text, integer, real, boolean, date and number.
Lists and Records (aka Arrays and Associative Arrays)
Arrays and Associative Arrays (or hashes) in AppleScript are called Lists and Records respectively and behave, pretty much, as you would expect arrays to behave. In addition to the difference in their names, they also have a different syntax for defining or manipulating them. Example:
set myList to {”This”, “is”, “a”, “list”}
— return the string “list”
get item 4 of myList
— return the list {”a”, “list”}
get items 3 thru 4 of myList
— change 4th element of list to “array”
set item 4 to “array”
— returns list {”This”, “is”, “a”, “array”}
get myList
set myRecord to {name: “Dasspunk”, phone: “555-55555″}
– sets the var myName to “Dasspunk”
set myName to name of myRecord

No Comments » RSS feed for comments on this post.
Leave a comment
You must be logged in to post a comment.