An AppleScript to empty one user's trash on one drive

Mar 13, '07 07:30:04AM

Contributed by: CajunLuke

I have several external hard drives connected to my iBook. One of them I use as scratch space, so I throw away lots of files on there. Since Mac OS X doesn't automatically empty the trash, the disk can get full quite fast. The problem is, I don't want to empty the trash on all of my disks - just that one. I've been cding into the trashes folder on the drive to empty it, but that's too time-consuming for general use. The command I've been using is this one:

rm -r /Volumes/Vingilot/.Trashes/510/*
Where Vingilot is the drive name, and 510 is your user number. (Every user account has a number associated with it - root is 0, and the account you make when you first start Mac OS X is 501. You can see all the numbers for your accounts in Terminal by typing ls -ln /Users.)

To automate this task, I wrote an AppleScript droplet. Drop a single drive on the file, and it will empty the trash on that drive. Here's the code:

on open dropped_item
  tell application "Finder"
    set drivepath to POSIX path of dropped_item
  end tell
  if drivepath is "/" then
    display dialog "This doesn't work on your boot volume."
  else
    set uid to the third word of (do shell script "id")
    set trashpath to drivepath & ".Trashes/" & uid & "/"
    beep
    set confirm to display dialog ¬
     "Empty the trash on " & drivepath & ¬
     "?" buttons {"No", "Yes"} default button "No"
    if button returned of confirm is "Yes" then
      display dialog "The deleted items on " & drivepath & ¬
       " were deleted."
      do shell script "rm -r " & trashpath & "*"
    end if
  end if
end open
Paste this into Script Editor and save it as an application. Put it in you sidebar or somewhere convenient, then drag and drop a volume onto the application to use it. The advantages of this method are: The code also asks for confirmation, so an accidental drag won't delete anything.

[robg adds: I tried this script, and it worked as described.]

Comments (8)


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