My First AppleScript Part 2: The AppleScript Dictionary

15 Comments

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

15 Comments

Adam Olson

Oh boy completely excited. I just discovered the record button thanks to http://www.vtc.com/products/AppleScript-2006-tutorials.htm much quicker and easier way to record locations and have scripts written for locations in the Finder. Just navigate to where you want in column view and then double click on the folder that you want to be the path. I was able to run the script above first and then run record in a new applescript click on the folders and navigate to their location and then cut and paste the newly generated script to the bottom of the old one and it works. Yeah!

property monitor_width : 1920
property monitor_height : 1200
set the startup_disk to (path to startup disk)
tell application “Finder”
activate
set visible of (every process whose visible is true and frontmost is false) ¬
to false
— BOTTOM WINDOW
set this_window to make new Finder window
set the target of this_window to the startup_disk
set the bounds of this_window to {0, (monitor_height * 0.55) div 1, ¬
monitor_width, monitor_height}
set the current view of this_window to column view

end tell
set display_x_size to 6000

set cur_app to (path to frontmost application as Unicode text)

tell application cur_app
tell front window
set {x1, y1, x2, y2} to (get bounds)
set win_width to (x2 – x1)
set room_right to (display_x_size – x2)
if room_right > win_width then
set x1 to (x1 + win_width)
set x2 to (x2 + win_width)
else
set x2 to display_x_size
set x1 to (display_x_size – win_width)
end if
set bounds to {x1, y1, x2, y2}
end tell
end tell
set the startup_disk to (path to startup disk)
tell application “Finder”
activate
set visible of (every process whose visible is true and frontmost is false) ¬
to false

— TOP WINDOW
set this_window to make new Finder window
set the target of this_window to the startup_disk
set the bounds of this_window to {0, (monitor_height * 0.036) div 1, ¬
monitor_width, (monitor_height * 0.53) div 1}
set the current view of this_window to column view
end tell
set display_x_size to 6000

set cur_app to (path to frontmost application as Unicode text)

tell application cur_app
tell front window
set {x1, y1, x2, y2} to (get bounds)
set win_width to (x2 – x1)
set room_right to (display_x_size – x2)
if room_right > win_width then
set x1 to (x1 + win_width)
set x2 to (x2 + win_width)
else
set x2 to display_x_size
set x1 to (display_x_size – win_width)
end if
set bounds to {x1, y1, x2, y2}
end tell
end tell
tell application “Finder”
activate
select Finder window 1
set target of Finder window 1 to folder “Desktop” of folder “adamwolson” of folder “Users” of startup disk
set current view of Finder window 1 to list view
select Finder window 2
set target of Finder window 1 to folder “Tutorial Sections” of folder “UCD Thesis” of folder “Personal Documents” of folder “Documents” of folder “My Files” of disk “Media 1TB”
set current view of Finder window 1 to list view
end tell

Adam Olson

For example I can get this path to run by itself

tell application “Finder” to open desktop

and the second window I want to open the following path which I can get to run by itself but I want both windows to open in the specified size like the preveous script does.

tell application “Finder” to open folder “Media 1TB:My Files:Documents:Personal Documents:UCD Thesis”

Thanks for any help on the matter!

Adam Olson

Is there a way to modify this script that I have been piecing together to make it open a specific path in the two windows so it will always open two folders I transfer files between regularly?

This script sets up two windows to open and then moves them to my second 23″ monitor.

property monitor_width : 1920
property monitor_height : 1200
set the startup_disk to (path to startup disk)
tell application “Finder”
activate
set visible of (every process whose visible is true and frontmost is false) ¬
to false
— BOTTOM WINDOW
set this_window to make new Finder window
set the target of this_window to the startup_disk
tell application “Finder” to open desktop
set the bounds of this_window to {0, (monitor_height * 0.55) div 1, ¬
monitor_width, monitor_height}
set the current view of this_window to column view

end tell
set display_x_size to 6000

set cur_app to (path to frontmost application as Unicode text)

tell application cur_app
tell front window
set {x1, y1, x2, y2} to (get bounds)
set win_width to (x2 – x1)
set room_right to (display_x_size – x2)
if room_right > win_width then
set x1 to (x1 + win_width)
set x2 to (x2 + win_width)
else
set x2 to display_x_size
set x1 to (display_x_size – win_width)
end if
set bounds to {x1, y1, x2, y2}
end tell
end tell
set the startup_disk to (path to startup disk)
tell application “Finder”
activate
set visible of (every process whose visible is true and frontmost is false) ¬
to false

— TOP WINDOW
set this_window to make new Finder window
set the target of this_window to the startup_disk
set the bounds of this_window to {0, (monitor_height * 0.036) div 1, ¬
monitor_width, (monitor_height * 0.53) div 1}
set the current view of this_window to column view
end tell
set display_x_size to 6000

set cur_app to (path to frontmost application as Unicode text)

tell application cur_app
tell front window
set {x1, y1, x2, y2} to (get bounds)
set win_width to (x2 – x1)
set room_right to (display_x_size – x2)
if room_right > win_width then
set x1 to (x1 + win_width)
set x2 to (x2 + win_width)
else
set x2 to display_x_size
set x1 to (display_x_size – win_width)
end if
set bounds to {x1, y1, x2, y2}
end tell
end tell

Peg

Great tip.

Anyone knows how to write a script to for InDesign to state the x and y coordinates of the top left corner of a frame (both text and picture frames)?

P

Evan

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).

RJ

 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.  

Ken

@Jean

Try –

open folder “Applications” of startup disk

instead of

open folder “Home:Applications”

Jean

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.

Nick

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

Jason Guthrie

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.

AS

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.

Comments are closed.