Easily empty the contents of stacks

I am in love with stacks in Mac OS X 10.5 Leopard. They are very convenient for storing my temporary download files, document drafts, code snippets, image clips or PDF files.

But at times I just want to flush my stacks, delete all the files and folders therein to make place for new items. To simplify this stack-cleaning process for you and me I wrote a little AppleScript: *stackflush!

*stackflush deletes every file and folder found in the stack where the script file resides. Of course, the script file itself is not deleted.

System Requirements
Mac OS X 10.5 (or later)

Installation
Just open the scriplet below in Script Editor, save it as an Application bundle and then place it in a stack folder of your choice. Whenever you want to flush this specific stack, simply start the script by (double-)clicking its icon. Please note, that you need to place a copy of the script in every stack where you want this functionality.

IMPORTANT: Don’t use aliases of the script. They will flush the folder where the original script resides! This can result in Ouch!

Tip
If you open the script with the Script Editor on your Mac, you will immediately discover a property named «filesnfolders». If you set this property to «false» and save the script, the script will only delete files, but not folders. This comes in handy, if you want to get rid of ZIP/DMG/RAR/etc. files in your Downloads-stack only, but keep the extracted folders.

How does it work?
The script detects its parent folder and then deletes every file and/or folder found therein, except itself. Therefor it can also be used with other folders, it is not a stack-only script. But it was written with stacks in mind. Nevertheless you will need a copy of the script for every stack/folder where you want this functionality.

Note
The script does not really delete the files and folders, it moves them to the trash.


-- created: 15.11.2007
-- modified:
-- ¢ 03.12.2007
-- version: 1.0.1
-- history:
-- ¢ v1.0.1 (03.12.2007)
--   + script has now been saved as an application bundle
--   + in order to reduce the launch time on Intel-based Macs
--   +  http://www.macosxhints.com/article.php?story=2006080109510225
-- ¢ v1.0 (15.11.2007):
--   + first public release

-- *********************************************
-- * This script detects its parent folder and then deletes
-- * all files and folders therein, except the script itself.
-- * It can be used to flush the contents of a stack,
-- * e.g. the 'Downloads'-stack
--  *********************************************

property scriptname : "*stackflush"
-- if you set the filesnfolders value to false,
-- then the script will only delete files, but not folders
-- this can be used to get rid of ZIP/DMG/RAR/etc. files only
property filesnfolders : true

on run
	try
		-- getting some knowledge about myself
		set mypath to ((path to me) as alias)
		set myinfo to info for mypath
		set myname to name of myinfo
		-- telling the Finder to flush the parent directory of the script
		tell application "Finder"
			set parentdir to container of mypath
			set diritems to every item in parentdir
		end tell
		repeat with diritem in diritems
			set iteminfo to info for (diritem as alias)
			-- avoiding friendly fire! (we don't want the script itself to be deleted)
			if name of iteminfo is not equal to myname then
				if filesnfolders is true then
					tell application "Finder"
						delete diritem
					end tell
				else
					if folder of iteminfo is false then
						tell application "Finder"
							delete diritem
						end tell
					end if
				end if
			end if
		end repeat
		-- catching unexpected errors
	on error errmsg number errnum
		tell me
			activate
			display dialog "Sorry, an error occured:" & return & return & (errmsg & " (" & errnum & ")") buttons {"OK"} default button 1 with title scriptname with icon caution
		end tell
	end try
end run