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

Selectively shred files with an Applescript droplet Apps
Since my discovery of the wonders of OS X, I have had a gripe about the way the Finder handles files with the Trash. In Windows, I would frequently delete a file permanently without ever putting it in the trash. Occasionally I like to delete files but leave them in the trash for a time until I'm absolutely sure I want to permanently get rid of them. Later, I'll find a file that I want to delete immediately to save disk space without emptying the trash (as the other files I wish to retain are already there).

Take this example. I just emailed a set of pictures to a friend, and then trashed the folder to keep my Desktop organized. But I don't want to empty the trash just yet -- I want to wait until I hear back from my friend to ensure that he/she successfully received the pictures. Later, I finished burning a CD of my favorite Linux distro (Gentoo), and I want to permanently delete the ISO image to save space on my hard drive. Without doing a little fancy Terminal work, there is no easy way to permanently delete the ISO file without emptying my trash and also permanently deleting those pictures.

This is where my script comes into play. This AppleScript iis meant to be used as a dropplet (so save in an Application Bundle from Script Editor). Dragging and dropping files onto it will prompt first and then permanently shred the files (using rm -rfP). Just to add more functionality to the script, I added an option to shred all the files in the trash by opening the application regularly.

Also, here's a link to the entire thing in an application bundle, with icon. As always, suggestions, comments, and modifications are welcome!
    •    
  • Currently 2.60 / 5
  You rated: 3 / 5 (5 votes cast)
 
[13,590 views]  

Selectively shred files with an Applescript droplet | 7 comments | Create New Account
Click here to return to the 'Selectively shred files with an Applescript droplet' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Selectively shred files with an Applescript droplet
Authored by: Krioni on Apr 29, '05 03:12:33PM
There are some areas your script could be improved. For example, in your code you have:

   if n is less than (length of target_files) then
           --set filenames for command and filelist for dialog prompt
        set filenames to filenames & "\"" & POSIX path of n_file & "\"  "
       set filelist to filelist & POSIX path of n_file & {return}
    else
      --set filenames for command and filelist for dialog prompt
        set filenames to filenames & "\"" & POSIX path of n_file & "\""
         set filelist to filelist & POSIX path of n_file
    end if

Rather than putting your own quotes around the posix path, you could use the Apple-supplied method: quoted form of, and end up with:


		if n is less than (length of target_files) then
			--set filenames for command and filelist for dialog prompt
			set filenames to filenames & quoted form of POSIX path of n_file & space
			set filelist to filelist & POSIX path of n_file & {return}
		else
			--set filenames for command and filelist for dialog prompt
			set filenames to filenames & quoted form of POSIX path of n_file
			set filelist to filelist & POSIX path of n_file
		end if

Also, I'm not sure why you used "on open of targetFiles" - "on open targetFiles" is the correct way to use "on open". Another change is that you can loop over the items in a list, without needing an iteration variable. This means you can do:


	repeat with n_file in target_files
...etc...
Something else to keep in mind is that the variable 'filenames' isn't really a bunch of filenames, its a bunch of file paths. Perhaps naming it filePaths would be clearer? So, here's the script, with some modifications:

(*
Author: Jayson Kempinger
E-mail: evilglowingapple@gmail.com
Date created: 23 April 2005

Permantly Shred script
Drop files/folders onto this as an application.  This script will shred the files
using the command specified in property command.

Icon modified from origional e.shred.gif at http://www.lebofsky.com/write/hailstorms.html

-- some modifications by Dan Shockley (http://www.danshockley.com)
*)


--command to shred files.  Use shred if you have it installed, use "rm -rfP " if you do not
property command : "rm -rfP "

--when running the app not as a dropplet, shred the files in the current user's trash
on run
	set x to display dialog "Do you want to permanently shred (delete by overwriting) all the files in your trash?" buttons {"Yes", "No"} default button "No"
	if button returned of x is "Yes" then
		try
			do shell script command & "~/.Trash/*"
		end try
		--update trash icon in the dock
		tell application "Finder" to update trash
	end if
end run

--shred dropped files
on open target_files
	--combine filePaths of all files into one string for subject
	
	set filePaths to "" -- space-separated list of file-paths for the shell command
	set fileVerticalList to "" -- retrn-delimited list of file-paths for dialog prompt
	
	repeat with n_file in target_files
		set n_file to contents of n_file -- sometimes a "reference" is not a good thing, so
		-- let's explicitly set n_file to the 
		
		if length of filePaths is 0 then
			set filePaths to filePaths & quoted form of POSIX path of n_file & space
			set fileVerticalList to fileVerticalList & POSIX path of n_file & return
		else
			set filePaths to filePaths & quoted form of POSIX path of n_file
			set fileVerticalList to fileVerticalList & POSIX path of n_file
		end if
	end repeat
	
	set x to display dialog "Do you want to permanently shred (delete by overwriting):" & return & fileVerticalList & "?" buttons {"Yes", "No"} default button "No"
	if button returned of x is "Yes" then
		try
			do shell script command & filePaths
		end try
		--update trash icon in the dock just in case files were from the trash folder
		tell application "Finder" to update trash
	end if
end open


[ Reply to This | # ]
Selectively shred files with an Applescript droplet
Authored by: DHB on Apr 29, '05 09:07:50PM

Or just buy SafeShred:

http://www.codetek.com/safeshred/

Works fine for me on 10.3.9



[ Reply to This | # ]
The Purpose of Trash.
Authored by: paulrob on May 01, '05 07:57:30AM

An office trashcan doesn't hold filing that people might want to throw away - it hold things that people are absolutely certain they want to throw away.

That is the purpose of the Mac's Trash - to hold things that the user is certain need to be thrown away. Items are only retained so that any error can be rectified i.e recovering an important file accidentally thrown away with obselete files. The only time a user should need to work with individual items in the trash is to retrieve an item accidentally put there.

The original poster would do better to realise that his/her thinking has been skewed by the silly Windows implementation of Trash, and stop trying to outwit what is only there for their benefit.

If it's remotely important - don't put it in the Trash. Trash is not an extra folder for the user, but the last line of defence against error.



[ Reply to This | # ]
Selectively shred files with an Applescript droplet
Authored by: dbingham on Apr 07, '06 05:43:28PM
Re: Krioni's improvements

This isn't quite correct:

		if length of filePaths is 0 then
			set filePaths to filePaths & quoted form of POSIX path of n_file & space
			set fileVerticalList to fileVerticalList & POSIX path of n_file & return
		else
			set filePaths to filePaths & quoted form of POSIX path of n_file
			set fileVerticalList to fileVerticalList & POSIX path of n_file
		end if
If there are more than 2 files, it won't work! Try this instead:

		if n_file is last item of target_files then
			set fileVerticalList to fileVerticalList & POSIX path of n_file
			set filePaths to filePaths & quoted form of POSIX path of n_file
		else
			set fileVerticalList to fileVerticalList & POSIX path of n_file & return
			set filePaths to filePaths & quoted form of POSIX path of n_file & space
		end if
So, the whole script would now be:

(*
Author: Jayson Kempinger
E-mail: evilglowingapple@gmail.com
Date created: 23 April 2005

Permantly Shred script
Drop files/folders onto this as an application.  This script will shred the files
using the command specified in property command.

Icon modified from origional e.shred.gif at http://www.lebofsky.com/write/hailstorms.html

-- some modifications by Dan Shockley (http://www.danshockley.com)
--some more modifications by dbingham
*)


--command to shred files.  Use shred if you have it installed, use "rm -rfP " if you do not
property command : "rm -rfP "

--when running the app not as a dropplet, shred the files in the current user's trash
on run
	set x to display dialog "Do you want to permanently shred (delete by overwriting) all the files in your trash?" buttons {"Yes", "No"} default button "No"
	if button returned of x is "Yes" then
		try
			do shell script command & "~/.Trash/*"
		end try
		--update trash icon in the dock
		tell application "Finder" to update trash
	end if
end run

--shred dropped files
on open target_files
	--combine filePaths of all files into one string for subject
	
	set filePaths to "" -- space-separated list of file-paths for the shell command
	set fileVerticalList to "" -- retrn-delimited list of file-paths for dialog prompt
	
	repeat with n_file in target_files
		set n_file to contents of n_file -- sometimes a "reference" is not a good thing, so
		-- let's explicitly set n_file to the 
		
		if n_file is last item of target_files then
			set fileVerticalList to fileVerticalList & POSIX path of n_file
			set filePaths to filePaths & quoted form of POSIX path of n_file
		else
			set fileVerticalList to fileVerticalList & POSIX path of n_file & return
			set filePaths to filePaths & quoted form of POSIX path of n_file & space
		end if
	end repeat
	
	set x to display dialog "Do you want to permanently shred (delete by overwriting):" & return & fileVerticalList & "?" buttons {"Yes", "No"} default button "No"
	if button returned of x is "Yes" then
		try
			do shell script "echo '" & command & filePaths & "' > ~/test.txt"
		end try
		--update trash icon in the dock just in case files were from the trash folder
		tell application "Finder" to update trash
	end if
end open


[ Reply to This | # ]
Selectively shred files with an Applescript droplet: correction
Authored by: dbingham on Apr 07, '06 05:47:16PM
Oops. Left some debugging code in there. This is correct:


(*
Author: Jayson Kempinger
E-mail: evilglowingapple@gmail.com
Date created: 23 April 2005

Permantly Shred script
Drop files/folders onto this as an application.  This script will shred the files
using the command specified in property command.

Icon modified from origional e.shred.gif at http://www.lebofsky.com/write/hailstorms.html

-- some modifications by Dan Shockley (http://www.danshockley.com)
-- some more modifications by dbingham
*)


--command to shred files.  Use shred if you have it installed, use "rm -rfP " if you do not
property command : "rm -rfP "

--when running the app not as a dropplet, shred the files in the current user's trash
on run
	set x to display dialog "Do you want to permanently shred (delete by overwriting) all the files in your trash?" buttons {"Yes", "No"} default button "No"
	if button returned of x is "Yes" then
		try
			do shell script command & "~/.Trash/*"
		end try
		--update trash icon in the dock
		tell application "Finder" to update trash
	end if
end run

--shred dropped files
on open target_files
	--combine filePaths of all files into one string for subject
	
	set filePaths to "" -- space-separated list of file-paths for the shell command
	set fileVerticalList to "" -- retrn-delimited list of file-paths for dialog prompt
	
	repeat with n_file in target_files
		set n_file to contents of n_file -- sometimes a "reference" is not a good thing, so
		-- let's explicitly set n_file to the 
		
		if n_file is last item of target_files then
			set fileVerticalList to fileVerticalList & POSIX path of n_file
			set filePaths to filePaths & quoted form of POSIX path of n_file
		else
			set fileVerticalList to fileVerticalList & POSIX path of n_file & return
			set filePaths to filePaths & quoted form of POSIX path of n_file & space
		end if
	end repeat
	
	set x to display dialog "Do you want to permanently shred (delete by overwriting):" & return & fileVerticalList & "?" buttons {"Yes", "No"} default button "No"
	if button returned of x is "Yes" then
		try
			do shell script command & filePaths
		end try
		--update trash icon in the dock just in case files were from the trash folder
		tell application "Finder" to update trash
	end if
end open


[ Reply to This | # ]
Pick a file to shred (on run)
Authored by: rshrinivas on Apr 18, '07 11:02:57AM
Using a file selection dialog, ... if some files in the trash cause too much angst, pick them :)
----------
(*
Author: Jayson Kempinger
E-mail: evilglowingapple@gmail.com
Date created: 23 April 2005

Permantly Shred script
Drop files/folders onto this as an application. This script will shred the files
using the command specified in property command.

Icon modified from origional e.shred.gif at http://www.lebofsky.com/write/hailstorms.html

-- some modifications by Dan Shockley (http://www.danshockley.com)
-- some more modifications by dbingham
-- minor addition by rshrinivas
*)


--command to shred files. Use shred if you have it installed, use "rm -rfP " if you do not
property command : "rm -rfP "

--when running the app not as a dropplet, shred a user selected file
on run
set x to choose file with prompt "Pick a file to shred" --default value "~/.Trash/"
set fileName to " \"" & POSIX path of x & "\" "
set x to display dialog "Confirm deleting " & fileName & "?" buttons {"Yes", "No"} default button "No"
if button returned of x is "Yes" then
try
do shell script command & fileName
end try
--update finder
tell application "Finder" to update
end if
end run

--shred dropped files
on open target_files
--combine filePaths of all files into one string for subject

set filePaths to "" -- space-separated list of file-paths for the shell command
set fileVerticalList to "" -- retrn-delimited list of file-paths for dialog prompt

repeat with n_file in target_files
set n_file to contents of n_file -- sometimes a "reference" is not a good thing, so
-- let's explicitly set n_file to the

if n_file is last item of target_files then
set fileVerticalList to fileVerticalList & POSIX path of n_file
set filePaths to filePaths & quoted form of POSIX path of n_file
else
set fileVerticalList to fileVerticalList & POSIX path of n_file & return
set filePaths to filePaths & quoted form of POSIX path of n_file & space
end if
end repeat

set x to display dialog "Do you want to permanently shred (delete by overwriting):" & return & fileVerticalList & "?" buttons {"Yes", "No"} default button "No"
if button returned of x is "Yes" then
try
do shell script command & filePaths
end try
--update trash icon in the dock just in case files were from the trash folder
tell application "Finder" to update trash
end if
end open

[ Reply to This | # ]
Selectively shred files with an Applescript droplet
Authored by: chyna4xena on Apr 19, '07 05:17:23PM
I created an Automator-based Finder plug-in to achieve this sort of functionality. I find it easier to use the contextual menu instead of a droplet.

To create the plug-in:

1) Open Automator, and add two actions: Finder > Get Selected Finder Items, followed by Automator > Run Shell Script

2) In the Shell Script section, set Shell to /bin/bash, set Pass Input to as arguments, and enter the following in the command box

for i; do
intrsh=`echo -n "$i" | grep .Trash`
if [ -n "$intrsh" ]; then
rm -rf "$i"
fi
done
3) Select Save As Plug-in from the File menu, choose a Finder plug-in, and call it something obvious like Delete From Trash.

To use, just select the specific items that you want to delete, control-click to get the contextual menu, and select Automator > Delete From Trash (or whatever you called the plug-in).

For safety, it will only delete items in the trash (with .Trash in their path). Otherwise, if you control-clicked on your hard drive and selected the plug-in it would delete most non-system files!

[ Reply to This | # ]