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


Rev. 7 | 41 comments | Create New Account
Click here to return to the 'Rev. 7' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Rev. 7
Authored by: identd on Sep 07, '05 02:25:24AM
Added secure file erasing, and it outputs to a text file named Trashdata.txt in the documents folder, enjoy!

on run {}
	-- Initalize variables 
	set pretime to current date
	set trashcontents to {}
	set trashamount to 0
	set itemstoskip to 0
	-- Grab a list of trash contents for each user 
	set thePath to path to current user folder
	try -- See  if the trashfolder exists 
		set theResult to my GetTrashContents((thePath as string) 
			& ":.Trash" as alias, trashamount)
		if (item 1 of theResult is not {}) then
			set end of trashcontents to "** User " & (do shell script "whoami") & "'s Trash Contents **" as string
			set itemstoskip to itemstoskip + 2
			set trashcontents to trashcontents & item 1 of theResult
			set trashamount to (item 2 of theResult) -- supercede new value 
		end if
	end try
	--Grab a list of trash contents for each volume mounted 
	set list_volumes to list disks
	repeat with thevolume in list_volumes
		try
			set theResult to my GetTrashContents(thevolume & 
				":.Trashes:" & (do shell script "echo $UID") as alias, trashamount)
			if ((item 1 of theResult is not {}) and (item 1 of theResult is not {space})) then
				set end of trashcontents to "** Volume " & thevolume & 
					"'s Trash Contents **" as string
				set itemstoskip to itemstoskip + 2
				set trashcontents to trashcontents & item 1 of theResult
				set trashamount to (item 2 of theResult) -- increment trashamount by theamount 
			end if
		end try
	end repeat
	-- Filter the list & format amount 
	if {trashamount = 0} then
		display dialog "Trash currently contains no items" buttons 
			{"Ok"} default button 1
		return false
	end if
	--display dialog trashamount as string
	set amount to my FormatSize(trashamount) -- Format trashamount 
	set filteredlist to my FilterList(trashcontents)
	set filtercount to (count of filteredlist) --Count of items in filteredlist 
	set filecount to ((count of trashcontents) - itemstoskip) --Count of items in thefiles 
	set theButton to ""
	set thesizebytes to trashamount
	-- Show user the information gathered 
	copy trashcontents to temp1
	set end of trashcontents to {"1 Pass Random"}
	set end of trashcontents to {"DOD Spec"}
	set end of trashcontents to {"35 Pass Gutmann algorithm"}
	
	set theResult to choose from list trashcontents with title 
		"Choose a method of Deletion ..." with prompt "There are " & filecount & " files" & " in the trash totaling " & amount & return & return 
		& "Empty Trash?" OK button name "Empty Trash" with empty selection allowed
	if theResult contains " - " then
		set theFile to item 2 of my stringtolist(item 1 of theResult, " - ")
		move ((path to trash & theFile) as string) to (path to desktop)
		set isdone to false
	end if
	if theResult ? false then
		set isdone to false
		set pretime to current date
		if (theResult as string) contains {"1 Pass Random"} then
			do shell script "srm -rs " & (POSIX path of (path to trash)) & ("*")
			tell application "Finder" to empty trash
			set isdone to true
		end if
		if (theResult as string) contains {"DOD Spec"} then
			-- overwrite the file with 7 US DoD compliant passes  (0xF6,  0x00, 0xFF, random, 0x00, 0xFF, random)
			do shell script "srm -rm " & (POSIX path of (path to trash)) & ("*")
			tell application "Finder" to empty trash
			--Slower then -s (simple)
			set isdone to true
		end if
		if (theResult as string) contains {"35 Pass Gutmann algorithm"} then
			-- the 35-pass Gutmann algorithm is used
			do shell script "srm -r " & (POSIX path of (path to trash)) & ("*")
			tell application "Finder" to empty trash
			--Very slow, but more secure
			set isdone to true
		end if
		if isdone is true then
			activate
			set et to ((current date) - pretime)
			set clonet to et
			if et = 0 then
				set et to 1
			end if
			set et to ConvertTimeToString(et)
			set tftw to (path to documents folder) & "TrashData.txt" as string
			set ref_num to open for access file tftw with write permission
			set theof to get eof ref_num
			write "Files Deleted on " & ((current date) as string) & return to ref_num starting at theof
			write my ListToString(temp1, return) to ref_num
			write "Stats: " & amount & " deleted in " & et & " (" & FormatSize(thesizebytes div clonet) & " /sec) using the " & (theResult as string) & " method" to ref_num
			write "

 " to ref_num
			--write (ASCII character 10) to ref_num
			close access ref_num
			display dialog "Secure Delete Complete" & return & amount & " deleted in " & et & return & "(" & FormatSize(thesizebytes div clonet) & " /sec)" giving up after 5
		end if
		if isdone is false then
			tell application "Finder" to empty trash
			set isdone to true
		end if
	end if
end run
on ConvertTimeToString(inTime)
	-- break the time up into hours, minutes, and seconds
	set timeVal to inTime
	set numHours to (timeVal div hours)
	set timeVal to timeVal - (numHours * hours)
	set numMinutes to (timeVal div minutes)
	set numSeconds to timeVal - (numMinutes * minutes)
	
	-- now put together the string in the proper format adding preceding zeros if necessary
	set timeStr to "" as string
	-- hours
	if (numHours < 10) then set timeStr to "0"
	set timeStr to (timeStr & numHours)
	-- minutes
	set timeStr to (timeStr & ":")
	if (numMinutes < 10) then set timeStr to (timeStr & "0")
	set timeStr to (timeStr & numMinutes)
	-- seconds
	set timeStr to (timeStr & ":")
	if (numSeconds < 10) then set timeStr to (timeStr & "0")
	set timeStr to (timeStr & numSeconds)
	
	return (timeStr as string)
end ConvertTimeToString
on FilterList(theList)
	set filteredlist to {} -- Holds the files that with "." 
	-- Build the list of files 
	repeat with theFile in theList
		set olddelim to AppleScript's text item delimiters
		set AppleScript's text item delimiters to " - "
		set theName to every text item of theFile as string
		try
			set theName to text item 2 of theFile
			if (theName begins with ".") then
				set end of filteredlist to theName as string
			end if
		end try
		set AppleScript's text item delimiters to olddelim
	end repeat
	return filteredlist
end FilterList
on FormatSize(the_size)
	set base_file_size to 1024
	set the_size to (the_size * 1024)
	if the_size > (base_file_size ^ 4) then
		set {div_1, the_unit} to {(base_file_size ^ 4), "TB"}
	else if the_size is greater than or equal to (base_file_size ^ 3) then
		set {div_1, the_unit} to {(base_file_size ^ 3), "GB"}
	else if the_size is greater than or equal to (base_file_size ^ 2) then
		set {div_1, the_unit} to {(base_file_size ^ 2), "MB"}
	else if the_size is greater than or equal to base_file_size then
		set {div_1, the_unit} to {base_file_size, "KB"}
	else
		set {div_1, the_unit} to {1, "B"}
	end if
	set the_size to (((the_size div div_1) & "." & ((the_size mod div_1) div (div_1 / 10)) as string) as real) as string
	if the_size ends with ".0" then set the_size to (text 1 thru -3 of the_size)
	return (the_size & " " & the_unit)
end FormatSize
(*
on FormatSize(thesize)
	display dialog thesize
	if (thesize > 1024) then
		set thesize to thesize / 1024 as string
		set thesizebytes to thesize as string
		set olddelim to AppleScript's text item delimiters
		set AppleScript's text item delimiters to "."
		set thesize to every text item of thesize
		set item 2 of thesize to item 1 of item 2 of thesize
		set thesize to thesize as string
		set AppleScript's text item delimiters to olddelim
		set thesize to thesize & " MB" as string
	else
		set thesize to (thesize as integer) & " KB" as string
	end if
	return thesize
end FormatSize
*)
on GetSize(thePath)
	repeat
		try
			tell application "Finder" to set thesize to ((physical size of thePath) / 1024)
			exit repeat
		on error
			delay 1
		end try
	end repeat
	return thesize
end GetSize

on GetTrashContents(thetrashpath, trashamount)
	set filelist to {}
	-- Get the contents of the trash 
	set thefiles to list folder thetrashpath -- Gets the directory contents of of the folder 
	-- Calculate each file size of thetrashpath 
	repeat with i from 1 to (count of thefiles)
		set theAlias to ((thetrashpath as string) & item i of thefiles) as alias 
			--Make the type of file to an alias 
		set theInfo to info for theAlias --Get the info on the alias 
		set theamount to my GetSize(theAlias)
		set trashamount to trashamount + theamount
		--display dialog theamount as string
		set theamount to my FormatSize(theamount)
		set end of filelist to theamount & " - " & name of theInfo as string
	end repeat
	set end of filelist to space
	return {filelist, trashamount}
end GetTrashContents
on ListToString(theList, delim)
	set oldelim to AppleScript's text item delimiters
	set AppleScript's text item delimiters to delim
	set alist to theList as string
	set AppleScript's text item delimiters to oldelim
	return alist
end ListToString
on stringtolist(theString, delim)
	set oldelim to AppleScript's text item delimiters
	set AppleScript's text item delimiters to delim
	set temp to (every text item of theString)
	set AppleScript's text item delimiters to oldelim
	return temp
end stringtolist
[\code]


[ Reply to This | # ]