An AppleScript to trash items on remote volumes

May 31, '05 09:52:00AM

Contributed by: davelentz

A large percentage of AppleScripts are writtten to process files in a programmatic fashion. At the successful conclusion of that processing, I would like to move the original files (that I have carefully preserved to this point, allowing for a graceful back-out should problems arise) to the trash. This is all pretty mundane, until you try to run your script against files on a mounted disk from another system. OS X doesn't like to allow this, I suspect due to sound security concerns. But I did find a graceful way around this, that works whether you are running the script locally or remotely.

I first set up an intermediate folder, let's call it "dumpster," and attach a Folder Action Script that looks like this to it:

on adding folder items to this_folder after receiving these_items
  tell application "Finder"
    move these_items to the trash
  end tell
end adding folder items to
You do this by copying the above text into a script file (or copy an existing Folder Action Script, renaming the copy to "dumpster.scpt," and replace the contents with the code shown above), placing your new Folder Action Script into the /Library/Scripts/Folder Action Scripts folder, and then using the Attach Script to Folder script in the Scripts menu pulldown to associate your new Folder Action Script with your newly created "dumpster" folder. Now anything placed in the "dumpster" folder is immediately moved to the Trash.

Since this happens on the machine whose disk is remotely attached, it requires no explicit action from the remote system to activate it -- just moving files into it will result in those files being moved to the Trash. Now all we need to do in our file-processing AppleScript, when the time comes that we want to move files to the Trash, is to insert the following lines of code:

tell application "Finder"
  set dumpsterFolder to (get folder (POSIX file dumpster as text))
  set sourceFolder to (get folder (POSIX file sourceFolderPath as text))
  move file sourceDoc of sourceFolder to dumpsterFolder
end tell
It may well be possible to simplify the two set statements from the way they are presented above, but it works for me.

So this code fragment will move the sourceDoc file from its enclosing folder into our dumpster folder. Then the Folder Action Script will merrily move it to the trash. This works whether the script that moves the file to the dumpster is executed on the same system as the dumpster folder exists on, or is run from a remote system with the disk mounted to it.

I use it to process stock quotes that arrive at the end of the day via email, constructing a .csv file to be imported into NeoOffice and applied to a spreadsheet, giving me a historical chart of portfolio values. In my case, the quotes for stocks and mutual funds arrive in two separate emails, which I use AppleScript to extract the essentials from and create an import document from. At a successful conclusion, I have no further need of the original files that are the email quotes saved to disk. This technique allows me to process the quotes and clean up afterwards, regardless of whether I am seated in from of the server, or connected via a shared disk and running things via iBook or PowerBook.

Comments (2)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20050517185222756