Section 1.2. Working with the Scripts You Have


1.2. Working with the Scripts You Have

As you'll quickly realize from using the Script Menu, your Mac is teeming with dozens of free, built-in scripts. You can run, rename, and organize these scripts, as described on the preceding pages.

But if you're just starting to learn AppleScript, these scripts can be lifesavers. Not only can you examine how the scripts work, but you can also make changes and see how they affect the scripts' behavior. Best of all, if you get stuck when writing a new AppleScript, you can copy some of the code from the Script Menu's scripts and paste that code right into your own script.

1.2.1. Opening a Script

The first step in working with a script, of course, is opening it up. Fortunately, this is an easy process: just double-click the script in the Finder. The script opens in Script Editor, the all-purpose AppleScript program described in Chapter 2.

When you're just learning AppleScript, you might as well start by looking at a simple script. Double-click Library Scripts Navigation Scripts New Application Window.scpt, and Script Editor opens the script file in a new window (Figure 1-9).

Figure 1-10. If you've never seen an AppleScript before, you may be surprised at how simple the code looks. As you can probably guess from the commands in the window, this script simply opens the Applications folder in the Finder.


At this point, you can click the Run button to see what the script actually does (in this case, the script opens a new Finder window and takes you to the Applications folder).

1.2.2. Analyzing How It Works

The next step in working with a script, of course, is understanding how the script actually works. Some commands are self-evident, while others require you to examine a program's dictionaryits master command list (Section 3.2.2)to understand exactly what's going on. Luckily, the commands in the New Applications Window script are all pretty simple:

  • tell application "Finder" instructs Mac OS X that the commands that follow should be run by the Finder.

  • activate brings the Finder to the front, much as you would by clicking its Dock icon. Unlike clicking the Dock icon, however, the activate command doesn't automatically open a new Finder window; for that stunt, you'll need the next command.

  • open folder "Applications" of the startup disk tells the Finder to open a new window, displaying the Applications folder of your main hard drive. (If you already have the Applications folder open, this command simply brings that window forward.)

  • end tell directs the Finder to go about its regular business, ignoring further AppleScript commands.

Power Users' Clinic
Secrets of the Script Menu

The Script Menu, simple as it may seem, has a number of completely hidden tricks up its sleeve. All these shortcuts can save you time, annoyance, and confusion:

  • Click any submenu to jump right to that folder in the Finder. For instance, if you wanted to open up the Mail Scripts folder, you could just click on the Mail Scripts submenu of the Script Menu.

  • Shift-click any script to jump right to it in the Finder. This is a great trick when you want to quickly rename a script: Shift-click its name to switch to the file, start editing its name by pressing Return, and then press Return again when you're done editing. Prestothe new name now shows up in the Script Menu.

  • Option-click any script to open it in Script Editor. Once there, you can examine the script to see how it works, or edit it to change how it works. Either way, Chapter 2 has more details on Script Editor.

  • Add files besides scripts to your Scripts menu. Yes, you read that right. Your Scripts menu is perfectly content to display programs, files, folders, and aliases, and they'll all show up in your Script Menu (shown here). To make your items appear on the bottom of the Script Menu, put them in your Home Library Scripts folder (rather than just Library Scripts).

  • Using this trick, you can turn your Script Menu into a fast, free application and file launcher. There's just one caveat: don't try to add big folders to the Script Menu. If you do, the menu may spend minutes burrowing into your folders, trying to figure out how to display all their items. Instead, add the individual files and programs you'll need and leave the rest of the folder somewhere else.


1.2.3. Changing What It Does

Now that you understand how the script works, you can change it to better suit your needs. To make the script open the Users folder, for example, you'd follow this procedure:

  1. Replace Applications with Users.

    File names have to be surrounded in double quotes, so make sure you don't delete the quotation marks.

  2. Click the Run button to test your script.

    You should see the Finder come forward and display the Users folder.

  3. Choose File Save As (Shift--S).

    Name the file New Users Window.scpt, make sure the File Format is set to Script, and save it in the Library Scripts Navigation Scripts folder.

  4. Open your Script Menu, and run your new script from the Navigation Scripts submenu.

    If you did everything correctly, your new menu item opens the Users folder in the Finder (Figure 1-10).

Figure 1-11. When you run your newly edited script (top left), you see your Users folder (bottom right). The house icon represents the account that's currently logged in.




AppleScript. The Missing Manual
AppleScript: The Missing Manual
ISBN: 0596008503
EAN: 2147483647
Year: 2003
Pages: 150

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net