Applescript and shell move files script
Do you ever need to move a bunch of files to a specific location? Or do you want to backup certain files and need a nice method for moving them to a second drive or location? This following script can be helpful. You can re-write it for your particular purpose. I originally wrote it to move postscript files to a RIP while they were being created. That way I could write out postscript to a local drive, but at the same time move them to the rip as they were being created. But this could easily be used to backup or just orgainize files you want to organize. This is an applescript, that you can attach to a folder. So it is written with an on adding folder items to handler. By doing that you can attach it to any folder and it will launch as items are added to the folder. In the loop, I switch from using Applescript directly, to using a shell script to move the files. Unix shell scripts are just plain nice for moving and copying files. Apple has provided the ditto command for the purpose of moving files, and the -rsrc flag is just a safety device telling the system to keep the resource fork with the file if there is one. Some carbon apps still write out resource forks into files.
on adding folder items to this_folder after receiving these_items
tell application "Finder"
set the destination_folder to folder "Firewire 58600:movedTestFiles:" as alias
set the destination_directory to POSIX path of the destination_folder
end tell
repeat with i from 1 to number of items in these_items
set this_item to item i of these_items
set the item_info to info for this_item
if this_item is not the destination_folder then
set the item_path to the quoted form of the POSIX path of this_item
set the destination_path to the quoted form of ¬
(destination_directory & (name of the item_info))
do shell script ("/usr/bin/ditto -rsrc " & item_path & " " & destination_path)
--comment out if you don't want to delete files
do shell script ("rm " & item_path)
end if
end repeat
end adding folder items to
I added the do shell script to remove the file after it has been moved to the destination folder. If you want to have two copies of the file, you can remove that line or comment it out. After you have this script written in your Applescript editor, save it to your /Library/Scripts/FolderActionScripts. Then in the finder, select the folder you want to use as the one you are going to place items to move. Then right click or control click the folder and select “Attach A Folder Action” from the contextual menu. Navigate to your script and attach it. That’s it. If any part of this isn’t clear, give me a comment and I’ll try to explain it better.