Section 5.5. Deleting Files


5.5. Deleting Files

So far, you've moved and copied files in the Finder from one place to another. There are some times, though, when you just want to get rid of a fileand AppleScript can do that too.

The key here is AppleScript's delete command. It works just like move or duplicate, except you don't have to specify where the deleted files should go (AppleScript automatically knows that deleted files should go in the Trash). Thus, a typical delete command would look something like this:

tell application "Finder"     delete the file "Chihuahuas.doc" of the desktop end tell

When you run this commandsubstituting the name of the actual file you want to delete, of courseyou hear a satisfying clunch as the Finder wads up your file and deposits it in the Trash can. If you don't hear this sound effect, three things could be wrong:

  • You've muted your speakers. The fix: press the volume-up key or increase the volume in your System Preferences Sound Output tab.

  • You've turned off Mac OS X's sound effects. To turn them back on, visit System Preferences Sound Sound Effects tab and turn on "Play user interface sound effects."

  • You don't have a Chihuahuas.doc file on your desktop. Either get one, or replace Chihuahuas.doc of the desktop with the name of the file you want to delete.

5.5.1. An Example: Clearing Out Safari's Icon Cache

If you use Safari for a few weeks, visiting hundreds or thousands of Web sites, you'll probably notice a significant slowdown each time you load a page. That's caused, in part, by Safari's gigantic database of faviconsthose little icons you see in Safari's Address bar (Figure 5-6).

Figure 5-6. The Apple icon, shown here, is the favicon that goes along with Apple's Web site. If you add this site to your bookmarks (Bookmarks Add Bookmark), the specialized icon will show up in the Bookmarks menu too.


If you delete Safari's icon cache (which is stored in your Home Library Safari Icons folder), you can give Safari a significant speed boostand save a few megabytes of space while you're at it. Here's a script to automate the process:

tell application "Finder"     delete folder "Icons" of folder "Safari" of folder "Library" of home     display dialog "Would you like to empty the trash now?"     (* If you click Cancel in the dialog box, the script ends here.         Otherwise, it continues to the next line *)     empty the trash end tell

When you run the script, the Finder drops the Icons folder in the Trash, and then presents the dialog boxes shown in Figure 5-7.

The only other time you'd really use the delete command is when you have a folder that needs to be emptied regularly (like your Decade-old Home Videos folder, for example). Besides that, there's not much use for delete, since you can always achieve one-time erasures by choosing File Move to Trash (or by pressing -Delete) in the Finder.

Figure 5-7. Top: Your script shows you a dialog box asking whether it should continue, thereby emptying the Trash. Click Cancel to stop the script in its tracks. Bottom: If you click OK, the Finder displays a progress bar as it erases everything in the Trash. That includes other things you've recently deletednot just the Icon folderso make sure that's OK with you before you proceed.


Gem in the Rough
The Reveal Command

As you go about your script-writing business, there might come a time when you want to show a file or folder in the Finder. After you've copied a file from one folder to another, for instance, you might want to display the new location for the file.

AppleScript makes this job easy using the reveal command. It works just like the File Show Song File command in iTunesthat is, it shows you the folder that contains a given file in the Finder. Try this to select your copy of TextEdit in the Applications folder:

tell application "Finder"     activate     reveal the file "TextEdit.app" ¬         of the folder "Applications" ¬         of the startup disk end tell

The reveal command is useful for showing folders, too. For example, you could highlight your Home folder (inside the Users folder) with this command:

tell application "Finder"     activate     reveal home end tell




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