Section 5.3. Moving Files Around


5.3. Moving Files Around

Being able to display your files is useful, but it's only half of what the Finder can do. The other half, of course, is to move your filesputting them in a new folder, deleting them, and so on. With AppleScript, you can automate these actions and more.

5.3.1. Transferring Items from One Folder to Another

The simplest action you can perform on a file is dragging it from one folder (or disk) to another. With AppleScript, it's also one of the simplest actions you can control in the Finder.

The key to this trick is the move command. It's part of the Finder's Standard Suite (Section 3.2.3), so it's a pretty common command. And, as the Finder's dictionary explains, the move command follows this simple structure:

tell application "Finder"     move someItem to somePlace end tell

In this example, someItem can be either a single item or a list of items. That makes the move command perfect for transferring whole clusters of files (or folders) in a single step. (See Chapter 6 for the lowdown on lists.)

And also, if you use the move command to transfer files from one disk to another, AppleScript (like the Finder) assumes that you mean to copy the files. If you want, you can then use the delete command (Section 5.5) to erase the files from the original disk.

Now, if you're the sort of person who always saves downloads and email attachments to your desktop, you've probably noticed your desktop getting pretty full. You might have so many icons that you've had to shrink them down (View Show View Options). Or perhaps your desktop is so cluttered that icons have started overlapping each other, obscuring all the important stuff you keep there.

No matter what the issue, AppleScript can help you clean up your desktop. With one fell swoop, a script can sweep up all the files and folders there, and stash them somewhere less intrusive.

You can use the move command for any number of other jobs, too: transferring sounds from an old folder to the Mac OS X-standard Music folder; moving downloaded files to a special Just Downloaded folder; or even sending files to another computer on your network (Chapter 9).

5.3.1.1 Creating the destination folder

Right out of the box, your computer comes with one Desktop folderthe one you see right now. That won't help much, though, when you want to clear your desktop and sweep all those files into another folder.

Thankfully, you can create a new folder to hold everything from your old desktop. (In fact, if you're feeling especially creative, you could even name the folder Old Desktop.) Here's how:

tell application "Finder"     make new folder at home with properties {name:"Old Desktop"} end tell

As you've seen on Section 4.5.1, the make command lets you create a new item (such as a document or folder) straight from AppleScript. The at option lets you specify where to create it (in this case, your Home folder). Plus, when you add the with properties option, you can specify various extra settings for the new item you create. (In this case, the name property gives the new folder a name, which is Old Desktop.)

After the with properties option, the settings you specify have to follow a special structure: they have to be surrounded by curly brackets, and each setting's name has to be followed by a colon and the value you want to use for it. For instance, to create a folder with the name Old Desktop, you have to use the property {name: "Old Desktop"}.

You can specify as many settings as you want inside the brackets. A more involved make command, for instance, could go something like this:

make new folder at home with properties ¬ {name:"Old Desktop", comment:"Old Desktop files and folders"}

That creates an Old Desktop folder in your Home folder, but it also adds a comment to the folder. Then, at some point in the future, you could see your comment by selecting the folder in the Finder and choosing File Get Info (-I).

5.3.1.2 Eliminating the "already an item with that name" error

When you first run your script, it'll silently create a new folder named Old Desktop in your Home folder. The trouble is, if you already have an Old Desktop folder when you run the script, you'll see the error message shown in Figure 5-4.

The key to avoiding this problem is placing an if statement around the folder-creating command. That way, if the script discovers that there's already an Old Desktop folder, AppleScript just skips over the command for creating the folder and move on to the next command.

To add the if statement, just modify the script by adding the lines you see here in bold:

tell application "Finder"     if not (the folder "Old Desktop" of home exists) then         make new folder at home with properties {name:"Old Desktop"}     end if end tell

Figure 5-4. AppleScript isn't very graceful about handling errors with the make command: it stops your entire script and presents a dialog box. Luckily, you can spare it the trouble by enclosing your make command in an if statement.


The new if statement looks in your Home folder to see whether you have an Old Desktop folder there already (the exists part). If you don't have an Old Desktop folder, the script runs the next command (make new folder) and creates one. Now you'll never get that annoying dialog box when you run your script in the future.

5.3.1.3 Moving the files and folders

Now that your script can tell whether or not there's an Old Desktop folder, you can get down to business: moving the files and folders from your desktop into your Old Desktop folder. Luckily, this is just a matter of adding two move commands to your script:

tell application "Finder"     if not (the folder "Old Desktop" of home exists) then         make new folder at home with properties {name:"Old Desktop"}     end if     move every file of the desktop to the folder "Old Desktop" of home     move every folder of the desktop to the folder "Old Desktop" of home end tell

These move commands, as you could probably guess, take all the files and folders that sit on your desktop and deposit them into your Home Old Desktop folder. In a single click of the Run button, your desktop's clean.

For the ultimate in convenience, add this script to your Script Menu (Section 1.1.16.1). Then you can run it from any program, whenever you want. This can come in really handy when, for example, you're snapping a bunch of screenshots (with either Shift--3 for a partial-screen picture or Shift--4 for a full-screen picture) and you want to quickly move the images off your desktop (which is where Mac OS X saves them by default). It's a great trick for Mac book authors, especially.



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