15 Comments

Summary:

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, […]

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!”

  1. Nice tutorial. I’d be good to have a screenshot of the way your desktop looks at startup now.

    Share
  2. Great idea – thanks! I went ahead and added a screenshot of my desktop after I login.

    Share
  3. Thank you Jason for an interesting article. Here’s a question. Can Applescript do this? I have a Pop account in Mail but with multiple computers. I’ve set Mail to delete from the Server as soon as email is checked. However, every once in a while, there is an email I want to put back on the server so that it can be accessed from another computer. Is there an Applescript that can be written so that it tells the computer than when I mark an email as “unread” it goes back to the server as unread (so that next time someone checks email, including from another computer, the email will be downloaded for them as “new”). I hope this makes sense. Thanks.

    Share
  4. Interesting question AS. I’ve never done it before, but I don’t see why it couldn’t be done. I’ll look into it.

    In the meantime, have you looked into using IMAP instead of POP? IMAP enables you to do exactly what you’re talking about. And from within Mail you can even download and ‘synch’ a local copy of your IMAP folders so that just in case your IMAP server fails, you still have a local copy.

    Share
  5. Are there any simple scripts that does back ups to a specific folders? I’d like to toy around with that after my backup software eats some of my addresses.

    Thanks

    Share
  6. Hi,

    I copied the script but always get an error saying:

    Error in Finder: impossible to obtain folder “Home:Applications”.

    I tried other folders inside home but to no avail.

    Any help would be appreciated

    Using Script Editor 2.1.1. / 10.4.7.

    Share
  7. @Jean

    Try -

    open folder “Applications” of startup disk

    instead of

    open folder “Home:Applications”

    Share
  8.  i believe “Home:Applications” would indicate an Applications folder inside the Home directory. this would be in addition to the root level Applications folder.

    however, i cannot get an Applescript to function using this : filepath notation. i continually get “Finder got an error: Can’t get folder” no matter what i try.  

    Share
  9. This worked for me:

    open folder “macintosh hd:applications”

    My Applications folder is not in the home folder, but in Macintosh HD (name of drive).

    Share
  10. Thanks all for the replies.

    It’s working now :)

    J

    Share

Comments have been disabled for this post