1. Home
  2. Mac OS X
  3. Introduction to applescript

An Introduction To AppleScript

Apple is not famous for allowing intrusion on their platforms, the carefully guarded iOS environment being one such example. You can customize it, but you have to assume certain risks, such as those involved in jailbreaking. Mac OS X however, is a slightly different scenario. Mac is a sophisticated Unix build working in unison with Apple wizardry. There are custom actions that you can make your Mac do that would render so much of your work absolutely hassle free, all at the click of a button. To help achieve this, Apple created AppleScript back in 1993. Where they let users automate basic functions on a Mac until a certain level of automation set in. If you want to customize your Mac experience through basic scripts, here is how.

AppleScript - Editor

Keep in mind that we can only introduce you to Apple’s automated scripting language and how to go about using it. We cannot make you experts in it. That, you will have to manage on your own, but if you do, your computing world genuinely changes and this isn’t over hype, it literally changes. Imagine the myriad of tasks that you have to do repeatedly, an affliction suffered by all operating systems, gone with a single click. You can do anything from sorting icons on your desktop, to logging hours, making an entry in Excel and emailing it. All at the click of a button. That is power you should definitely learn to wield.

Intro

AppleScript, unlike the complicated Objective C, and the less complicated but still not as easy Swift, is a scripting language that processes regular English. This process will not require a Master’s degree in programming. All you need to understand is how to give instructions to a machine. For example:

tell application "Finder"
display dialog "Hello World"
end tell

AppleScript - HelloWorld

Syntax

With all programming, there is a convention. That is to tell a program when to start, what to start, what to do once it has started and when to end. So the string becomes, [command] [target type] [target name] [action] [end command]. This is an overly simplistic representation, but if you are writing code and something goes wrong, it is safe to assume that the code is missing one or more of the above parameters.

Or, in simpler terms, there is a “tell block”, in which you are “telling” the application what you want from it. Like the above code is telling the Finder to display a dialog window containing the words “Hello World” (or whatever word you decided to test on it). Once you are finished with the command, you write “end tell”. Like we said, it is a machine, so you have to tell it where to end, or it will refuse to compile (compiling is the act of turning code into an executable).

Compiling

 Press compile when you are done writing. If there are any syntax errors (typos mostly), it will tell you what the error is (to the best of its abilities) and if there is no error, your code will automatically format and colorize.

AppleScript - Formatted

Running

After compiling, press run and you will be done. Depending on your follow up action, a log entry will be made. As exampled below for either scenario in our example. “Return” is the act of taking your input to the machine. If you are comfortable with this, let us take it one step ahead and declare variables.

AppleScript - Ok

AppleScript - Canceled

Variables

If you are familiar with programming, even at a cosmetic level, then you are probably familiar with variables. For those of us who are not familiar, Variables are a simple method of compressing lots of information into a single string that is easy to manipulate. How this is done varies from language to language, with AppleScript, you can take the following approach.

set varExample to "Hello World"
tell application "Finder"
display dialog varExample
end tell

AppleScript - VarExample

Here, “varExample” is the name we gave our declared variable. Now, if we need to print Hello World again in this script, we will only need to call varExample and won’t have to write all of that code again. One can assign the result of any code, complicated or otherwise to a variable and call it when needed. As you can see, compiling the above code yielded the same results as the one before it. The new words, “set” and “to” imply that we are assigning values basically set [this] to “[that]”. It is that straight forward. Seasoned programmers will appreciate that there is no need to declare the variable “type”, because AppleScript can manage that on its own. Keep in mind that there cannot be any spaces in variable names, or the compiler will think of it as a command.

Also, if you write anything after a double hyphen/dash “–” the compiler will consider it a remark and will ignore it. This comes in handy because then you can  leave yourself notes on what you are doing. You will need them if your code is going to get complex.

You are free to run your experiments with variables as you please, here is an example you can copy paste (or try your own if you wish).

--Numeric/Integer Variables
set theFirstNumber to 1
set the theSecondNumber to 2
--Variable Operations
set theAnswer to (theFirstNumber + theSecondNumber)
set theAnswer to (theAnswer + 1)
--String Variables
set theString to "3+2+1="
--Display Dialog
tell application "Finder"
display dialog theString & theAnswer
end tell

AppleScript - Compiling

This code accomplishes nothing other than some fancy displays and the most basic arithmetic, it is just an example to help illustrate the things you can achieve with it.

Dictionary

Now, all of the above is just to help you get familiarized with the environment. Once you have done that we will come to the difficult part. It is to familiarize yourself to dictionaries. AppleScript has a range of commands that apply to any program or item within OS X, however, developers of each application are required to add full AppleScript support to their apps. In order to do so, they write simple manuals on how you can communicate with their apps. These manuals are called “Dictionaries”. Go to File then Open Dictionary.  Here, you will see a list of all installed apps (except for any windows apps running through wrappers) that support AppleScript. Click on any app and you will see a list of customization commands and how to use them. We are going to open Evernote as an example. You can see all the commands it supports.

AppleScript - Dictionary

AppleScript - Evernote

Customizing AppleScript Editor

If you look at “AppleScript Editor” in the file menu, and click on Preferences, you will find a customizable list of all possible colors and formatting to your Apple Script. Formatting is just FYI, it is not important to the actual scripting.

AppleScript - FormattingOptions

Conclusion

In order to avoid over-burdening users, we will end this instruction here, so you can get yourself familiarized with the tool and play around with a few options. Keep checking back as we will soon give you the next step on how to create custom scripts. Let us know how your experience went by sounding off in the comments.