Join Scott Simpson for an in-depth discussion in this video Introducing the dictionaries, part of Up and Running with AppleScript.
- View Offline
I mentioned earlier that the way AppleScript interacts with other programs on your system is through the use of dictionaries. Dictionaries are places where AppleScript can look up what other apps say they'll let AppleScript do. An example of this is the Finder Dictionary which is installed by default. If you've been working with Macs for awhile, you probably know this. But Finder is actually a completely separate program from the operating system just like how AppleScript Editor or Keynote or Photoshop are. So even for something we take so for granted as Finder, we have to specifically tell it to do something that it understands.
We need to tell Pages or Keynote or Safari or Photoshop something they understand how to do too. So, let's take a look at the Finder Dictionary,here in Apple Script Editor I'll click on Window and then pull down to Library these are the system dictionaries that are installed by default I'll click on Finder and here's the dictionary window. There's some text and layout controls along the top they'll take this opportunity to increase the text size a little bit so it's more readable on this screen. There's also a Print button up here if you're feeling particularly eco unfriendly. Some of these dictionaries can be pretty long and I wouldn't recommend printing them.
The dictionary documentation is available on most every Mac in the world and you get a handy search box in this interface as well. The rest of the window is split into a column view on the top and a detail window at the bottom. On the left of the column view is the listing of Suites within the dictionary. You can think of these Suites like chapters in a book, organizing the different classes and commands together. The little orange square S icons next to each one tells you that they're a Suite, and if I click on one I can see what's grouped within it. I've clicked on the Standard Suite, which are general things, that as you can see in the description down below, are common terms that most applications should support.
These items have the little blue C icon, and that C stands for Command. To click down here in the Detail window for a moment. When I click on a command here in the top part, the documentation focuses on that item's information down below. I'll pick, activate. Here I can see that activate is a verb, indicated with a V, like in a printed dictionary. And there's that natural language metaphor at work again. I can see that the description is to activate the specified Window or the Finder. So I'll switch over the editor really quick and show you how to put this documentation to use. Alright, tell application and Finder in double quotes and then end tell.
Inside of that I'll write activate. that verb that we found in the dictionary just a minute ago. So the tell specifier tells Apple Script that we're using the finder dictionary in order to tell finder something. In this case it's activate, which since i don't have any Finder windows open will activate or bring into the foreground the Finder. This is kind of like using Command tab to switch to the Finder or like clicking on the Finder icon in the doc. Let's go back to the Dictionary. Next, I'll click here on Finder Items. This Suite has some commands. Eject, Empty, Erase, things like that that you can do to Finder items.
And there's a purple C icon for item. This purple C icon indicates a class or a type of thing, in this case a Finder item. Just like in a printed dictionary, you can see that this item is a noun because it's a thing. And of course, things have properties, as indicated by the square purple P icon in the third column over here. Each of these properties is a value that you can read or set. The properties that are Read-Only are indicated with this r/o signifier. The other text here is the type of data that the property expects if you can set it, or the type that it returns if it's Read-Only.
I'll bring in the Editor window and show you some of these. I'll change activate to set var1 to every item of path to desktop. And I'll click run. And there is a list of all of the items on my desktop. I can switch every item to, the name of path to desktop, and then when I run this, I get back the name of the folder called "Desktop". Don't worry about this path to desktop business right now. I'll show you what that's all about in a little bit. But basically it's shorthand for, wherever the users desktop is.
I'll jump back to the Dictionary. And moving down the Suites, you can see there are other groupings of things, for example, Containers and Folders. These are more specific types of items, and, as you can see here in the description window, Container, Computer Object, Desk, and so on inherent from the item class that we took a look at a little bit ago. Some of them through the Container class. If you're not familiar with it, inheritance is a term in Object Oriented Programming that means an object has all the properties of something else. Another class called the Super Class. For example this Disk Class here has all of the properties of item.
Since it inherits from it through the Container Class. I'll switch back over to the editor, and we'll use a property from the Item Class. I'll change this line to set var1 to the name of Startup Disk. Startup disk is a placeholder that Finder keeps track of that contains the name of the disk where the Operating System booted from. So this is an item of, Type Disk instead of a Type Container. But since it inherits from Item I still have a name property, Macintosh HD. But since this is a Disk, not just an Item, it also has other properties like Capacity and Free Space, things that makes sense for Disks, but not for all Finder items like Files and Folders.
So I'll change name to Capacity. And I'll hit Run. And down here you can see this is how Apple Script represents a one terabyte disk. 9.99 e+11. I'll jump back into the dictionary. Finally these orange square E icons represent elements or things that can be contained within a Container. For example a Container can contain Files. So let me switch back to the Editor here and I'll change set var1 to every file of Startup Disc. I'll Run this and I get an empty list back.
What's going on there? First switch over to Finder and go to the Macintosh HD, you can see in fact there are no files showing here. But if I switch every File to every Folder and hit Run again, now I get something more like what I expect. I see five folders here and then I see five definitions of what those folders are down here. This is pretty verbose and this is the way that AppleScript thinks about things. Folder Applications of Startup disk of Application Finder. Now, I should mention here, there are a few ways of writing this line.
I can say, the folders, and that result is the same. Or, I can just say folders. So, keep an eye out for that when you're writing apps or looking at someone else's code. I encourage you to explore the other Suites in the Finder Dictionary and maybe poke around in some of the other dictionaries as well. For the next few movies, I'll just be using the Finder Dictionary.
- What is AppleScript?
- Using comments, variables, text, and numbers
- Working with files and folders
- Using conditionals and creating loops
- Building functions
- Controlling other programs with AppleScript
- Preventing and catching errors
- Distributing your AppleScript application