5.3. Moving Files AroundBeing 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 AnotherThe 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
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 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.
5.3.1.1 Creating the destination folderRight 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"}.
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 5.3.1.2 Eliminating the "already an item with that name" errorWhen 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 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 foldersNow 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
|