Monday, July 25, 2011

A Simple AppleScripting Tutorial



Among other things, I enjoy AppleScripting. AppleScript is a coding language built into Apple operating systems since OS 7 in 1993. It is used to control and exchange data with applications. Although AppleScript has limited processing power, its main job is to interact with already-built applications to increase efficiency by performing repetitive
tasks. And no, you PC users can't use it.
So, what can you do with AppleScripts? Well, open up AppleScript Editor (Script Editor on OS X systems before Snow Leopard 10.6). It's in Applications>AppleScript>. Here's a very simple example of AppleScripting.
display dialog "Hello World!"


This will display a window like this:
To customize the window, you can add buttons:
display dialog "Hello World!" buttons {"Hi!", "Bye"}
This will display this:

Notice that neither of the buttons are highlighted blue. You can fix that by adding this:
display dialog "Hello World!" buttons {"Hi!", "Bye"} default button 2
Now it looks like this:
You can change the default button by changing the default button number. The number go from left to right, with the leftmost button being 1, etc.
Another cool thing you can add to a dialog box is an icon.
display dialog "Hello World!" buttons {"Hi!", "Bye"} default button 2 with icon 1
display dialog "Hello World!" buttons {"Hi!", "Bye"} default button 2 with icon 2
Icon 1:

Icon 2:
I also found another way to add icons to a dialog here. Very cool!
If you want to find out more about AppleScripting, check out the Apple Developer documentation here and here. Macscripter is also another good place to get information. I will probably be posting more stuff on AppleScripting, including some more complex coding.

-- Matthew

3 comments: