Empty the trash from any application via AppleScript

Sep 25, '09 07:30:07AM

Contributed by: CaptnEvilStomper

One thing that has always annoyed me in OS X is that the Empty Trash shortcut only works in Finder, so I wrote an AppleScript that lets you empty the trash from any application. First you'll need to download Spark. Make sure the Spark daemon is running and that 'Start Spark Daemon at Login' is checked in the General tab of the Preferences window. Once you have it set up, hit Command-1 to create a new AppleScript shortcut. Copy and paste this code into the window that comes up:

property showWarning : true

on run
  set currentApp to short name of (info for (path to frontmost application))
  
  tell application "Finder"
    set trashCount to count of items in the trash
    if trashCount > 0 then
      if showWarning then
        activate
        try
          display alert "Are you sure you want to remove the items in the Trash permanently?" message ¬
            "You cannot undo this action." buttons {"Cancel", "OK"} cancel button "Cancel" default button "OK"
          empty the trash
        end try
      else
        try
          empty the trash
        end try
      end if
    end if
  end tell
  
  tell application currentApp to activate
end run
The showWarning property tells the script whether or not to show the confirmation dialog when you empty the trash. If it's set to true then you'll get the standard warning, and after you hit OK or Cancel, you'll be taken back to whatever app you were in. If it's false, then the trash will just empty itself in the background.

Once you've pasted the script into the new window, add a shortcut for it in Spark, give it a name, and click the Create button. Now you'll be able to call the script from any application using the shortcut you defined. You could probably do the same thing in Snow Leopard by creating a service and then assigning it a custom shortcut in the Keyboard preference pane, but I haven't upgraded yet so I haven't tried it.

Comments (20)


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