- 0 Comments
- 0 Recommendations
Amazing AppleScript! Page 2 of 4
Learn Apple's Simple Scripting Language, and Your Mac Will Be at Your Command
Getting Familiar
In our overview of AppleScript, we’ll examine the properties of a Finder window in detail and learn how to manipulate them with scripts.
The Script Editor To write a script, we’ll use the Script Editor application installed in your system. You’ll find this application in the AppleScript folder located in the Applications folder on your hard drive.
Script Editor displays a multipaned window, known as a script window, with two panes. The top pane contains the description field, which is for optional comments or instructions regarding the use of the script. The bottom pane contains the script field, which is for the text of the script itself. (By default, the script window is rather small, so you may want to expand it.)
Our First Script We’ll begin learning AppleScript by writing a series of simple script commands in the form of tell statements—single-line scripts beginning with the verb tell . This verb is used to direct script actions at a specific application or scriptable object. A tell statement has two parts: a reference to the object to be scripted and the action to be performed. Using this format—or syntax—we can write scripts instructing the Finder to perform whatever actions we desire. To see an example, type the following in the script window (making sure the word Finder is inside straight, not curly, quotation marks):
tell application "Finder" to close every window.
Click on the Check Syntax button in the script window to confirm that the script was written correctly and to prepare it for use. Next, click on the Run button to play the script. The OS will read the script and send the appropriate commands to the Finder, which will then follow those instructions and close any open windows. Congratulations—you’ve written and run your first AppleScript!
Note that
Finder
is enclosed in quotation marks in the script—names and text data are always treated this way in scripts. This punctuation prevents Script Editor from seeing the text as commands or instructions when it checks and prepares the script. Delete the previous script from the script window, and enter, check, and run the following script:
tell application "Finder" to open the startup disk.
A new Finder window will appear on the desktop. Finder windows are different from other windows used by the Finder, in that they have a toolbar and display the contents of disks or folders. We’ll use this newly opened window as we examine the properties of a Finder window.
The Name Property
The first Finder-window property we’ll examine is its name property. A window’s name is the text shown in its title bar. In the case of Finder windows, the text is the name of the folder or disk whose contents are displayed within the Finder window.
Name Game To retrieve the value of the window’s name property, we’ll use the get command. The verb get is used when we want to extract information or data from a scriptable element or object. Enter and run the following script: tell application "Finder" to get the name of front window.
To see the result of the request, look in the result window (choose Show Result from the Controls menu in Script Editor, or type Command-L). You’ll see that the title of the open Finder window also happens to be the name of your start-up disk.
For Finder windows, the name property is a reference (or read-only) property—it can be used to refer to a window, but its value cannot be changed by a script. The value of a Finder window’s name property will always be the name of the folder or disk whose contents it displays.
As the following script demonstrates, you can use the name property as a means to refer to a specific window. Delete the previous script from the script window, and then enter, check, and run the following script (replace Macintosh HD with the name of the open Finder window if it’s different):
tell application "Finder" to close window "Macintosh HD". The Macintosh HD window should close. Note that this script is a fully qualified tell statement—it both refers to the object receiving the commands (in this case, the open window) and indicates the desired action to take (closing the window).
The Index Property
If you’re writing a script that will be used on multiple computers, using the name property is not always the best way to locate a specific window. Another, more generic way to refer to an open Finder window is through its index property.
Look Up My Number The value of this reference property is a number corresponding to the window’s numeric position in the stacking order of open Finder windows. For example, the frontmost Finder window will always have an index value of 1, while the last Finder window will always have an index value equal to the total number of open Finder windows. To see how this property can be used, first reopen the previous window, using this script: tell application "Finder" to open the startup disk.
Now run the following script:
tell application "Finder" to get the index of Finder window "Macintosh HD".
The result of this script in the result window will be the number 1 , since only one window is open at this point. Open a second window and run:
tell application "Finder" to open home.
A second window displaying the contents of your Home directory will now appear on the desktop.
Note the use of startup disk and home in the previous scripts. Startup disk and home are special terms reserved by the Finder application to identify important locations. They’re generic, and they’ll work regardless of how drives and folders are named on the computer.
Now you can see whether the addition of the new window has affected the index value of the target window. Run the following:
tell application "Finder" to get the index of Finder window "Macintosh HD". The result of this script is
2,
since the target window is the second window in the stack of open windows on the desktop.
The index property can be used to identify any open window, and since the AppleScript language was designed to be like English and to be written in a conversational manner, there are several ways to reach the same result (for more on this, see “AppleScript at a Glance”).
The Target Property
The value of the target property is a reference to the folder or disk whose contents are displayed in the Finder window. Unlike the values in our previous examples, this value can be both read and changed.
Where Am I? With your Home folder at the forefront, use this script to get the value of the target property:
tell application "Finder" to get the target of the front window. This returns folder
username of folder Users of startup disk of application "Finder"
.
As you can see, the result of this script is a reference to the folder whose contents are displayed in the Finder window, in this case your Home directory. This reference describes the target folder in terms of its position in its object hierarchy. The returned object reference clearly shows that the target folder is contained by the Users folder, which is on the start-up disk, which is an element of the Finder application. You’ll use this hierarchical reference structure often in the scripts you write.
Next, we’ll change the targets of the open Finder windows. To change the value of a property, use the verb set. Run the following script:
tell application "Finder" to set the target of the front Finder window to the startup disk. You’ll notice that the frontmost Finder window now displays the contents of the start-up disk. Now try this script:
tell application "Finder" to set the target of the last Finder window to home. The second Finder window now displays the contents of your Home directory.
To summarize, the target property of a Finder window has a value that is a reference to a specific folder or disk whose contents are displayed within the Finder window. This value can be changed by using the verb set in conjunction with an object reference to the new target folder. An object reference describes an object in terms of its position in its object hierarchy, or—in the case of a folder object—where it is on its parent disk.
Now, let’s continue our overview of the properties of a Finder window.
The Current View Property
The next Finder-window property we’ll examine is the current view property. The value of this property is the method used to display the contents of the Finder window. This property has three possible values—icon view, list view, and column view—and can be both read and edited.
Point of View To see your frontmost window’s view status, run the following: tell application "Finder" to get the current view of the front Finder window. This returns one of the following terms: icon view, list view, or column view. In order to change the current view to list view, you can run this script: tell application "Finder" to set the current view of the front Finder window to list view. You can then easily change the view to another by substituting column view or icon view for list view.
The Position Property
The value of the position property determines where a Finder window is placed on the desktop. This value is displayed as a list of two numbers that describe the position of the top left corner of the Finder window in relation to the top and left sides of the desktop display. The value of the position property can be both read and edited, and you’ll use this property in the scripts you write for organizing window placement.
Know Your Place Delete the previous script from the script window, and then enter, check, and run the following script:
tell application "Finder" to get the position of the front Finder window. This returns a bracketed list of two numbers, such as {14, 64}, describing the window’s position relative to the top left point of the desktop display. The first is the horizontal distance in pixels from the left side of the desktop display; the second, the vertical distance in pixels from the top of the desktop display.
Let’s change the value of the position property of the front Finder window, to move the window to the top left of the screen. Delete the previous script from the script window, and then enter, check, and run the following script:
tell application "Finder" to set the position of the front Finder window to {72, 90}. The front Finder window has now been moved so that its top left corner is 72 pixels from the left side of the desktop and 90 pixels from the top of the desktop.
The Bounds Property
If you find yourself always resizing windows, you’ll probably write scripts with the bounds property. The value of this property describes the size and position of the target window. A list of four numbers, a window’s bounds are described by two points: the top left point and the bottom right point of the window. These two coordinates are used to outline the rectangular shape of the window. Like the position property, the value of the bounds property can be read and edited.
The Shape of Things First, let’s get the bounds of the frontmost window:
tell application "Finder" to get the bounds of the front window. The value of the bounds property is returned as a four-item list, such as {14, 64, 512, 481}, and describes the window’s boundaries in relation to the screen.
By changing the value of the bounds property, a Finder window can be resized and positioned anywhere on the desktop. For example, the following script will place the front Finder window at the top left of the desktop (just below the menu bar) and resize the window to 400 pixels in width and 300 pixels in height. Again, the verb set is used in this script: tell application "Finder" to set the bounds of the front window to {0, 44, 500, 344}. The Finder window will now be resized to the top left of the desktop, just beneath the menu bar.
Make a Selection There is one more verb we’ll learn for use with windows— select , to make a window become active or move in front of any other open windows: tell application "Finder" to select the last Finder window. The Finder window that was behind the front Finder window will now be moved to the front of the open windows.
- Recommend? 0 YES 0 NO
- 0 Comments



