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

Permanently delete files via AppleScript Apps
Ever since switching to OS X from Windows, I've missed the convenience of being able to permanently delete files using Shift-Delete. In order to duplicate that functionality, I've written a short AppleScript that will delete the files selected in Finder instead of placing them in the trash.
set frontAppPath to (path to frontmost application) as string

if the frontAppPath ends with ":Finder.app:" then
  tell application "Finder"
    set selectionList to get selection as list
    set selectedCount to count items in selectionList
    
    if selectedCount > 0 then
      repeat with i from 1 to number of items in the selectionList
        set selectedItem to item i of the selectionList
        set selectedName to the name of selectedItem
        set homePath to (path to home folder) as string
        set trashPath to homePath & ".Trash:"
        set deletePath to trashPath & selectedName & ":"
        
        if not ((kind of selectedItem as string) is equal to "Volume") then
          if (selectedItem as string) contains ".Trash" then
            set posixPath to POSIX path of (selectedItem as string) as string
            do shell script "rm -rf \"" & posixPath & "\""
          else
            try
              deletePath as alias
              display dialog "Error: file named \"" & selectedName & "\" already exists in the Trash."
            on error
              try
                tell application "Finder" to delete selectedItem
                do shell script "rm -rf ~/.Trash/\"" & selectedName & "\""
              end try
            end try
          end if
        end if
      end repeat
    end if
  end tell
end if
The best way to use this script is to assign it to a shortcut key (e.g., shift-backspace) using a program like Spark.

Notes:
  1. The script first moves files to the trash before permanently deleting them. This prevents accidentally deletion of protected or system files.
  2. The script will not delete or remove files that have a duplicate filename already in the trash.
  3. It will not remove files which are not on the local hard drive.
  4. It will only launch if the foremost active window is Finder.
[robg adds: Beyond making sure the code compiles, I have not tested this script. However, if you intend to use this, please do all your testing on a copy of a copy of a non-critical file, and make sure that your backups are current. It should go without saying, but this is a potentially very dangerous hint -- use it incorrectly, and the file(s) you've chosen are gone for good with no recourse for recovery.]
    •    
  • Currently 3.40 / 5
  You rated: 5 / 5 (5 votes cast)
 
[42,333 views]  

Permanently delete files via AppleScript | 7 comments | Create New Account
Click here to return to the 'Permanently delete files via AppleScript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Permanently delete files via AppleScript
Authored by: robdew on Nov 29, '07 07:47:56AM

In briefly looking at the code, this appears to be "immediately" deleting files, not "permanently" deleting files, like you would would with srm.



[ Reply to This | # ]
Permanently delete files via AppleScript
Authored by: gshenaut on Nov 30, '07 10:17:03AM

For a simpler--but equally dangerous--method of removing files, use an Automator Finder plug-in. I have one I call "Expunge", with two actions. The first action is an Automator "Ask for confirmation" screen, and the second one is a simple "Run shell script" that takes the selected files as command line arguments, and applies "rm -rfP" to each one, in a for loop. I save this workflow as a Finder plugin, and voilĂ . When you control-click on one or more objects in the Finder, you just slide down to the "Automator" submenu and select Expunge to delete them immediately and permanently.



[ Reply to This | # ]
Permanently delete files via AppleScript
Authored by: V.K. on Dec 05, '07 04:47:30PM

here is a variation of the above to delete all files in a folder.
Make an automator action consisting of the following simple shell script:

for f in "$@"
do
rm "$f"/*
done



Save the automator action as a finder plug-in and there you have it.
Right-clicking on a folder and choosing your plug-in from the contextual menu will delete all the files in that folder. As Rob says be careful using this!



[ Reply to This | # ]
Permanently delete files via AppleScript
Authored by: ryandesign on Feb 23, '08 04:04:01PM

This script is dangerous because it does not properly quote special characters in the names of the to be deleted items. In particular it will not work correctly, and could potentially cause unwanted data loss, when used on items whose names contain the quotation mark. To correct this, replace

do shell script "rm -rf \"" & posixPath & "\""

with

do shell script "rm -rf " & quoted form of posixPath

and

do shell script "rm -rf ~/.Trash/\"" & selectedName & "\""

with

do shell script "rm -rf ~/.Trash/" & quoted form of selectedName



[ Reply to This | # ]
Permanently delete files via AppleScript
Authored by: lmodern on May 19, '09 03:21:21AM
As this script is very dangerous (I accidentally deleted an important file), I added a "deletion confirmation" dialog box before removing any element.
The new script looks as follows:

set frontAppPath to (path to frontmost application) as string

if the frontAppPath ends with ":Finder.app:" then
	tell application "Finder"
		set selectionList to get selection as list
		set selectedCount to count items in selectionList
		--Added code1
		display dialog "Do you really want to delete this item?" buttons {"OK", "Cancel"} default button 2
		if the button returned of the result is "Cancel" then
			return
		else
                --End of added code1
			if selectedCount > 0 then
				repeat with i from 1 to number of items in the selectionList
					set selectedItem to item i of the selectionList
					set selectedName to the name of selectedItem
					set homePath to (path to home folder) as string
					set trashPath to homePath & ".Trash:"
					set deletePath to trashPath & selectedName & ":"
					
					if not ((kind of selectedItem as string) is equal to "Volume") then
						if (selectedItem as string) contains ".Trash" then
							set posixPath to POSIX path of (selectedItem as string) as string
							do shell script "rm -rf "" & posixPath & """
						else
							try
								deletePath as alias
								display dialog "Error: file named "" & selectedName & "" already exists in the Trash."
							on error
								try
									tell application "Finder" to delete selectedItem
									do shell script "rm -rf ~/.Trash/"" & selectedName & """
								end try
							end try
						end if
					end if
				end repeat
			end if
                 --Added code2		
                 end if
                 --End of added code2
	end tell
end if



[ Reply to This | # ]
Permanently delete files via AppleScript
Authored by: tedw on May 20, '09 12:51:01AM
this code is far too complex; simpler is (almost) always better
If you want to do this with applescript, use the following:
tell application "Finder"
	if frontmost is false then return
	set selectedList to the selection
	set deleteList to {}
	repeat with thisItem in selectedList
		if class of thisItem is not disk then
			set end of deleteList to thisItem
		end if
	end repeat
	delete deleteList
	ignoring application responses -- because secure deletes can take a while
		empty trash with security
	end ignoring
end tell

if you want to do it with shell scripting, use this:
tell application "Finder"
	if frontmost is false then return
	set selectedList to the selection
	set deleteCmdList to {}
	repeat with thisItem in selectedList
		if class of thisItem is not disk then
			set end of deleteCmdList to quoted form of (POSIX path of (thisItem as alias))
		end if
	end repeat
end tell

set {tid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, " "}
set cmd to deleteCmdList as text
set AppleScript's text item delimiters to tid
do shell script cmd

I'd also suggest you do what I do in my own secure delete script: run the delete list through a choose from list alert so that you can check and remove anything you don't want to destroy.

[ Reply to This | # ]
oops - an error
Authored by: tedw on May 20, '09 08:43:56AM
this line in the second script - set deleteCmdList to {} - should actually read like this - set deleteCmdList to {"srm", "-rfmz"}. The script won't do much if you don't specify the unix command. :)

[ Reply to This | # ]