Empty the trash (again)

The subject is definitely not new; however, I have incorporated several suggestions from various folk as follows:


on emptytheTrash()
	tell application "Finder"
		if length of (items in the trash as string) is 0 then return
		
		if not (folder "Empty Trash Temp" exists) then
			set theFold to make folder with properties {name:"Empty Trash Temp"}
		else
			set theFold to folder "Empty Trash Temp"
		end if
		
		(*
		CanNOT use this because the Finder can't see this folder - it's invisible.
		
		set theFold to POSIX path of (path to temporary items folder)

		As a result, AppleScript generates an error = "Can't get every item of trash":
		*)
		
		move every item in the trash to theFold
		
		set locked of every item in theFold to false
		
		delete theFold
		
		empty trash
		
		-- The following repeat loop delays the rest of the Script until the Trash is empty.
		-- This repeat loop, based on time, prevents an endless loop if there is a
		-- significant amount in the Trash.
		-- suggested by Jon Pugh
		
		set nbrTrashItems to length of (items in the trash as string)
		
		set trash_loop to 0
		repeat while (nbrTrashItems > 0)
			set trash_loop to trash_loop + 1
			if (trash_loop = 15 * minutes) then exit repeat
			
			set nbrTrashItems to length of (items in the trash as string)
		end repeat
	end tell
	
end emptytheTrash

Boy, do I wish I could use the TemporaryItems folder because the System will automatically delete its contents upon reboot; so I’m forced to create a new “Empty Trash Temp” folder as was suggested on this Forum.

The ??? centers on the fact that every once in awhile (not all the time, by any means), the Finder will display a window that states something to the effect that a specific file is “busy”.

What’s interesting is that, for example, if this warning comes half way through the repeat loop, for example, I hit “Continue” and it does continue and does leave the one bothersome file in the trash. So far so good; but then I hit CMD-shift-delete and the bothersome file does get removed from the trash. Why, then, does the Finder produce this warning if all I have to do is hit CMD-shift-delete ??

The only way around this “busy” warning is to use “Secure Empty Trash”.

Is there another approach?

Thanks in advance

Hi John,

why not much simpler

on emptytheTrash()
	tell application "Finder"
		if (count items of trash) > 0 then
			set locked of items of trash to false
			empty trash
			repeat until (count items of trash) = 0
				delay 1
			end repeat
		end if
	end tell
end emptytheTrash

Hi Stefan

Thanks bunches for your simplification, especially in the repeat loop for the count of items.

I noticed that you unlocked items while in the Trash. I have not personally validated the issues on unlocking locked items while in the Trash; but I accepted the discussions within:

http://bbs.applescript.net/viewtopic.php?id=699

John

O.k. I see.
What’s about these quick solutions

do shell script "rm -rf ~/.Trash/*" user name "user" password "¢¢¢¢" with administrator privileges
activate application "Finder"
tell application "System Events" to tell process "Finder"
	key code 51 using {option down, shift down, command down}
end tell

btw. you can access the folder of temporary items easily with

path to temporary items --> alias MacHD:private:var:tmp:folders.501:TemporaryItems:

I think I found the cause of the “feature”?

The problem is not with my emptytheTrash() routine, but rather with my quitAllApps() which quits all applications that use Cache folders, such as SeaMonkey, Mail, iPhoto etc. before I start to empty these Cache folders, finishing with emptying the trash.

The quitAllApps() routine(shown way below) quits all these apps. Then I call my emptyCacheFolder() routine which first unlocks each file of each Cache folder and then deletes each Cache folder’s contents. Then I call emptytheTrash(). Then, I call ClearRecentItems() which turns on Universal Access(if not already on), then calls:


		tell menu item "Clear Menu" of ¬
			menu "Recent Items" of ¬
			menu item "Recent Items" of ¬
			menu "Apple" of ¬
			menu bar item "Apple" of ¬
			menu bar 1 of ¬
			(first process whose frontmost is true)
			click

The “feature” does not occur UNLESS I then, at the very end, either shut down or restart. With the latter, I sometimes get the warning: “Cannot empty the trash because file ‘whatever’ is busy”.

Here’s quitAllApps():


set appNames to {"Explorer", "Fetch", "Firefox", "iPhoto", "Mail", "Mozilla", "Navigator", "Opera", "Safari", "SeaMonkey"}
on quitAllApps(appNames)
	tell application "Finder"
		repeat with eachAppName in appNames
			try			
				tell application "System Events"
					set thisProcess to ¬
						(first application process whose name contains eachAppName)
				end tell
				
				set processName to name of thisProcess
				run script "tell application \"" & processName & "\" to quit"
				
				set allGone to false
				repeat until allGone
					tell application "System Events" to ¬
						set allProcessNames to names of application processes
					set allGone to allProcessNames does not contain eachAppName
				end repeat
			end try
		end repeat
	end tell
end quitAllApps

The I think I found the cause of the “feature”?

The problem is not with my emptytheTrash() routine, but rather with my quitAllApps().

The “feature” disappears IF I add

delay 10

AFTER quitAllApps().

Here’s my total guess work; namely, that somehow it’s related to Universal Access and ClearRecentItems(). I don’t have a clue why … it just seems to work so far.

The real mystery is that my

repeat until allGone

loop within quitAllApps() should NOT require this blanket delay 10 addition ???

Hi John,

a suggestion for a “thinner” quitAllApps() routine


on quitAllApps(appNames)
	repeat with eachAppName in appNames
		tell application "System Events" to exists process eachAppName
		if result then
			quit application eachAppName
			tell application "System Events"
				repeat while exists process eachAppName
					delay 1
				end repeat
			end tell
		end if
	end repeat
end quitAllApps