My First AppleScript Part 2: The AppleScript Dictionary

After reading part 1 of this little series called My First AppleScript, you probably felt fairly comfortable fiddling around with it yourself and have probably tried to write your own AppleScripts. Hopefully your own AppleScripts turned out great and worked like a charm. In my case, however, my AppleScripts crashed and burned. Why? Because I didn’t know the language.

“But I thought you said I didn’t need to learn some complicated language!” you’re probably screaming. “You promised!”

Like I said before, AppleScript is fairly easy to understand and one of the programming languages closest to “normal” English. However, the broken and incomprehensible English of my 2-year-old daughters is not the same English I use – even though we may be using some of the same words. In order to bring everyone up to the same playing field, AppleScript requires that anyone using AppleScript know the basics of the language. And where does any good linguist turn to learn a new vocabulary? The dictionary of course!

AppleScript itself, along with every Application that takes advantage of AppleScript, has its own dictionary. This dictionary outlines the possible commands that can be executed as well as the types of objects that can be manipulated. For example, the dictionary for the Finder lists commands you may not have known about such as: clean up, eject, update, reveal, and empty. The dictionary also lists the attributes of objects such as size, color, or location.

As you can start to see, the dictionary serves as your definitive reference guide. If you have some sort of process that you’d like to automate using AppleScript – but you just can’t figure out what the command would be called – the dictionary is the place to look. In fact, the dictionary is the first place I went after my first AppleScript. It’s useful, easy to navigate, and even contains search functions to help you find the commands for doing what you have planned.

How to Use the Dictionary
To access the dictionary simply choose File > Open Dictionary from within the Script Editor. From there simply choose the application that you are working with. The first app that I used to help develop my AppleScript skills was Finder. Finder is something everyone uses – and knowing how to manipulate it can be a great benefit.

When first experimenting with AppleScript the first thing I decided to do was to change the way the Finder looks. Every time I logged on it took me several minutes to get Finder to look the way I wanted it to. And in this example I’ll show you what I did to get Finder to open automatically every time I logon – using nothing but the Script Editor’s Dictionary.

The first step is getting the Finder to open showing the contents that I want to see first – in this case my ‘home’ folder. This can be done with the line:
tell application "Finder" to open home
This simply tells Finder to open a Finder window to your home folder. This seems pretty simple, so let’s also open the Applications folder in a separate window by using:
tell application "Finder" to open folder "Home:Applications"

We’ve now got the windows we want opened, but they’re stacked on top of each other and not in the view that we would like – so let’s go ahead and change that. Now, it might take a little searching in the dictionary to find exactly what you want, but you can also use the search function to find what you are looking for faster. In this case, a simple search for “view” gives us what we want – the property “current view.”

We’re now going to add the following lines to our Script Editor to change the default Finder view:
tell application "Finder" to set current view of Finder window 1 to icon view and
tell application "Finder" to set current view of Finder window 2 to list view

Since the Home:Applications folder is opened last, it is opened on top, giving it the “index” value of 1 (something we won’t go into here). However, on your own machine you can use the actual name of the folders or windows you are working with by using ...Finder window "Jason" or something similar.

So now that we have the Finder windows looking perfect – for our demonstration at least – and now we can work on the windows’ positions. The easiest way to do this is to arrange the finder windows exactly how you want, find out what the position coordinates are, and then use AppleScript to set them that way every time.

After positioning and sizing the windows on your screen run this line of script:
tell application "Finder" to get the bounds of Finder window 1 to find out where the window is. You can then simply change “window 1” to “window 2” to find out the bounds of the second window. However, if you’ve been clicking on the Finder windows, the indexes might have changed, so for this portion of the exercise you might want to try using the window names like:
…get bounds of Finder window “Jason” (which is my home folder) or window “Applications”

Once you have the coordinates of the windows, simple change the code from “get” to “set” in order to change where the widows are positioned. However, not only will the “bounds” property tell you where the window is located (also done by the “position” property) but bounds defines where all four corners are located – effectively resizing the windows.

Congratulations! You’ve now built all the pieces to your first multi-line AppleScript. Now all we have to do is combine the lines into one giant tell statement like so:

And be sure to finish the tell statement with an “end tell” or else the script will not work.

Running the AppleScript at Startup
Once your AppleScript is complete, you’ll need to save it as an application so that we can run it at startup. To do that, simply choose File > Save As… and choose “Application” as the file format. Also, be sure that “startup screen” is unchecked or else you will be prompted to run it each time (we want it to run automatically).

Now that your AppleScript is in the form of an application, simply add it to the list of items to run at startup. If you’ve never done this before simply go to the “Accounts” pane under System Preferences. Choose Login Items at the top where you can then click the ‘+’ button and add your new AppleScript to the list of applications that are run at startup.

Now, every time you login your windows will be configured exactly the way you want (see above). And each time you see those windows you can say to yourself, “I did that myself using AppleScript!”

You're subscribed! If you like, you can update your settings


Comments have been disabled for this post