The Tell Block
To set the Desktop window display to the arrangement you have set up, the following script performs a series of 12 commands that open and manipulate the two windows:
tell application "Finder" to close every window
tell application "Finder" to open home
tell application "Finder" to set toolbar visible of the front Finder window to true
tell application "Finder" to set the sidebar width of the front Finder window to 135
tell application "Finder" to set the current view of the front Finder window to column view
tell application "Finder" to set the bounds of the front Finder window to {36, 116, 511, 674}
tell application "Finder" to open folder "Documents" of home
tell application "Finder" to set toolbar visible of the front Finder window to false
tell application "Finder" to set statusbar visible of the front Finder window to true
tell application "Finder" to set the current view of the front Finder window to flow view
tell application "Finder" to set the bounds of the front Finder window to {528, 116, 1016, 674}
tell application "Finder" to select the last Finder window
Don’t bother entering this script in Script Editor. As you see, it is rather long, is very dense, and would take some effort to type into Script Editor. There’s an easier way to compose your scripts.
Notice that the script is a series of tell statements targeting the same application, namely, the Finder.
Instead of writing a group of 12 tell statements, each beginning with tell application "Finder" to, we’ll shorten the time it takes to write the script and make it easier to read by enclosing the commands within a single tell block.
A tell block is used to target multiple actions at a single scriptable object, in this case the Finder application, which makes scripts easier to understand. You’ll always want to use a tell block in your scripts when addressing more than one action at one scriptable object.
A tell block begins with the verb tell followed by a reference to a target object. The various actions to be performed by or to that object are then listed, each on its own line. After all the actions have been entered, the tell block ends with the closing statement end tell. All actions inside the tell block target the object referenced in the first line of the tell block.
Enter, compile, and run the following script as shown, except you may optionally substitute the window bounds values you previously wrote down.
tell application "Finder"
close every window
open home
set toolbar visible of the front Finder window to true
set the sidebar width of the front Finder window to 135
set the current view of the front Finder window to column view
set the bounds of the front Finder window to {36, 116, 511, 674}
open folder "Documents" of home
set toolbar visible of the front Finder window to false
set the current view of the front Finder window to flow view
set the bounds of front Finder window to {528, 116, 1016, 674}
select the last Finder window
end tell
You should now see two Finder windows placed side by side on the Desktop, one in column view, the other in flow view, with your home directory the active window and your Documents folder displayed in the other window. Instant Desktop organization!
We’ll do more with this script in a moment, but first let’s return to the subject of tell blocks. A tell block is a much cleaner, more concise, and easier to read than a series of separate tell statements. Compare the previous script to the same script written as a series of tell statements (top of this page).
It is not only easier to write the script using a tell block, but you can clearly see the relationship between scriptable objects and the commands that target them, because of the indentations automatically placed in the script text by Script Editor when you compile the script. It’s very easy to see where a section of a script targeting a specific object begins (tell) and ends (end tell).
In summary, tell blocks are easy to use if you remember these simple rules:
- If you begin a tell block, you must end a tell block with an end tell statement.
- Commands within a tell block target the object addressed in the tell block’s opening statement.
Next we’ll examine to improve and save the Desktop Cleanup script.