Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

An AppleScript to empty one user's trash on one drive Storage Devices
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:
  • You only empty your own trash, not anybody elses' (unless they're on another computer and have the same UID as you)
  • The script doesn't ask for authentication, so it's not likely to bork anything.
  • The script checks to make sure you're not doing this in your startup drive, where stuff is stored in different locations.
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.]
    •    
  • Currently 3.17 / 5
  You rated: 1 / 5 (6 votes cast)
 
[12,119 views]  

An AppleScript to empty one user's trash on one drive | 8 comments | Create New Account
Click here to return to the 'An AppleScript to empty one user's trash on one drive' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
An AppleScript to empty one user's trash on one drive
Authored by: bugmenot on Mar 13, '07 10:58:09AM

I'm a complete stranger to AppleScript -- is there a way to integrate unmounting the disk into this as well? I know I could add a terminal diskutil command, but can this be done only using AppleScript?

thanks



[ Reply to This | # ]
An AppleScript to empty one user's trash on one drive
Authored by: CajunLuke on Mar 13, '07 03:52:57PM

certainly: after the do shell script "rm ..." command, add a do shell script "disktutil unmount " & drivepath line.

[ Reply to This | # ]

An AppleScript to empty one user's trash on one drive
Authored by: jaguarcy on Mar 13, '07 12:21:50PM
Yes, and it's fairly straight forward:

set vol_alias to (dropped_item as alias)
tell application "Finder"
	if (ejectable of vol_alias is true) then
		eject vol_alias
	end if
end tell


[ Reply to This | # ]
An AppleScript to empty one user's trash on one drive
Authored by: michelle_eris on Mar 13, '07 01:37:59PM

If the trash on the volume is empty, there is an error alert "rm: /Volumes/<disk name>/.Trashes/<uid>/*: No such file or directory"

To fix this problem, modify the code to this:

try
do shell script "rm -r " & trashpath & "*"
end try

I'd also append "giving up after 2" to the second dialog. This will automatically dismiss the dialog after two seconds.



[ Reply to This | # ]
An AppleScript to empty one user's trash on one drive
Authored by: kopf on Mar 13, '07 04:50:32PM

Nice idea!

The droplet could use two improvements:

1. Sanity check to see if anything other than a volume has been dropped. Just use it with any folder on the startup volume and you'll see the problem.

2. Instructions when the application is run instead of something dropped on it.



[ Reply to This | # ]
An AppleScript to empty one user's trash on one drive
Authored by: rolf-thomas on Mar 14, '07 02:05:59AM

I had a whitespace in the drives name and the "rm" doesn't work. I inserted a search-replace to mask whitespaces with backslashes.

Works now as expected. Thank you!

So here is my modification (and german localization :-).

on open dropped_item
	tell application "Finder"
		set drivepath to POSIX path of dropped_item
	end tell
	if drivepath is "/" then
		display dialog "Funktioniert nicht auf dem Startlaufwerk." buttons {"Ok"} default button "Ok"
	else
		set uid to the third word of (do shell script "id")
		set trashpath to drivepath & ".Trashes/" & uid & "/"
		beep
		set confirm to display dialog ¬
			"Mülleimer von " & trashpath & ¬
			" leeren?" buttons {"Nein", "Ja"} default button "Nein"
		if button returned of confirm is "Ja" then
			set trashpath to searchReplace(trashpath, " ", "\\ ")
			try
				do shell script "rm -r " & trashpath & "*"
			end try
			display dialog "Der Mülleimer wurde geleert" buttons {"Ok"} default button "Ok" giving up after 2
		end if
	end if
end open

to searchReplace(thisText, searchTerm, replacement)
	set AppleScript's text item delimiters to searchTerm
	set thisText to thisText's text items
	set AppleScript's text item delimiters to replacement
	set thisText to "" & thisText
	set AppleScript's text item delimiters to {""}
	return thisText
end searchReplace[code]


[ Reply to This | # ]
An AppleScript to empty one user's trash on one drive
Authored by: dev_sleidy on Mar 14, '07 06:19:12AM

on open dropped_item
set drivepath to POSIX path of dropped_item
if (not (drivepath is "/")) then
try
do shell script ("rm -r " & (quoted form of (drivepath & ".Trashes/" & (third word of (do shell script "id")) & "/") & "*"))
beep
end try
end if
end open



[ Reply to This | # ]
Quickest shell command I could find to do this
Authored by: lullabud on Mar 14, '07 08:27:16AM
This is the quickest way I could find to do this, automating the detection of the UID for less chance of error:

rm -rf /Volumes/ExternalDisk/.Trashes/`id -u`/*

[ Reply to This | # ]