Applescript Editor is a default app on OS X, located in the Utilities folder in Finder, and is something that can come in very useful. Like all computer languages there is a learning curve, so basic features will be focused on here. Compared to many, Applescript is a relatively simple language and, as you’ll see, isn’t too hard to pick up. This tutorial will focus on dialog boxes: firstly how to create them, and secondly how to assign tasks to them.
To begin, I shall create a simple dialog box. In this case, I want it to say “This is a script”, and I want it to display an OK button. I also want this OK button to be the default button, appearing highlighted when the script is run. This is achievable through one line of code:
There are three simple commands present in this line. “Display dialog”, followed by the desired text in quotation marks, sets the text for the dialog window, and “buttons” defines which buttons will be displayed. The “default button” command, followed by “OK” sets the OK button as default. If you desire more than one button curly brackets are required, but this is mentioned below. If you save the script and click Run, you’ll see your simple dialog box:
Notice that clicking OK causes the dialog box to close. Though in the above example you didn’t tell the OK button to do anything, you can assign buttons to do different things. In this next dialog box, I want two buttons displayed – the first will launch Safari, the second will launch iTunes. Firstly, I’ll set the dialog in this box to say “Which application would you like to launch?”, like so:
Next I’ll create the buttons, the first being Safari, the second being iTunes. I’ll also set Safari as the default button. Notice the buttons are listed in between curly brackets, separated by commas:
Now that the buttons are defined, you need to tell each what to do using a few new commands. In this example, if the “Safari” button is clicked, then Safari will be launched, and if the “iTunes” button is clicked, then iTunes will be launched.
Firstly, I need to use the “if” command to set what happens if the Safari button is clicked, followed by the “then” command to specify what should happen next. Since I want the button to launch the application, I need to use the “tell” command to tell Safari to activate:
Next, I’ll add the lines specifying what should happen if the iTunes button is clicked, also ensuring to close commands:
Each command here serves a specific purpose. The initial “if” command specifies what happens when the Safari button is clicked. The “then” command tells the script what to do if this button is clicked. The “tell” command in this instance tells the application “Safari” to launch, or activate.
The “else if” command specifies what happens if, instead of the Safari button, the iTunes button is clicked. Again, the “then” command tells the script what to do if the button is clicked, and the “tell” command tells iTunes to activate. The “end if” command specifies that these are all the variables required – without this the script wouldn’t run anyway.
Once all this is in place, save the script and click run. If everything is in it’s right place, a dialog box similar to this should be displayed, the Safari button appearing blue as it was set to default:
>
Clicking each button will launch each application. If you’d like, you can also add a Cancel button, adding the option to close the dialog box without launching either application. To do this, simply add “Cancel” to the list of buttons in your script.
Now you’ve got an understanding of the basics of AppleScript, and the key commands needed in most scripts, it’s worth exploring what else is possible. This isn’t so much a practical example as it is a technical one – the key purpose of AppleScript in my eyes is to save time via automation – but this tutorial should provide the basics with which you can explore further.
Add Comment