Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Applescript Empty Trash

Applescript Empty Trash
Thread Tools
oscar
Grizzled Veteran
Join Date: Oct 1999
Location: Minneapolis
Status: Offline
Reply With Quote
Jul 9, 2005, 05:01 AM
 
Hi, i wrote a script that will display a dialog with various trash information, here is the script, please post mods to this thread. works as expected now, but still get random errors (divide by zero errors)
Code:
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 ≠ 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 desktop) & "TrashData.txt" as string set ref_num to open for access file tftw with write permission set theof to get eof ref_num try 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 on error close access ref_num end try 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
     
Diggory Laycock
Professional Poster
Join Date: Oct 2001
Location: London
Status: Offline
Reply With Quote
Jul 9, 2005, 06:31 AM
 
damn - I would have done "tell app finder to empty trash"

It doesn't appear to work here - it claims my trash is empty. (there is some stuff in the trash in my home dir on the local volume 'wideboy' and in the remote volume 'diggory'.)

Perhaps the trash isn't always kept in a folder called 'Trashes' - e.g. the volume 'diggory' appears to keep its trash in a dir called '.Trash'

Code:
-rw-r--r-- 1 diggory diggory 15364 Jul 3 22:15 .DS_Store drwx------ 13 diggory diggory 398 Jul 8 15:07 .Trash -rw------- 1 diggory diggory 0 Apr 28 23:49 .Xauthority
[php]
tell current application
current date
date "Saturday, July 9, 2005 11:36:31"
path to home folder
alias "WideBoy:Users:diggory:"
list disks
{"WideBoy", "diggory", "Network", "G4160"}
(*checking for trash in WideBoy*)
do shell script "echo $UID"
"501"
(*Error: -43. File WideBoy:.Trashes:501 wasn't found.*)
(*checking for trash in diggory*)
do shell script "echo $UID"
"501"
(*Error: -43. File diggory:.Trashes:501 wasn't found.*)
(*checking for trash in Network*)
do shell script "echo $UID"
"501"
(*Error: -43. File Network:.Trashes:501 wasn't found.*)
(*checking for trash in G4160*)
do shell script "echo $UID"
"501"
(*GetTrashContents, G4160:.Trashes:501:*)
list folder alias "G4160:.Trashes:501:"
{}
(*found 0 files in this trash*)
display dialog "Trash currently contains no items" buttons {"Ok"} default button 1
{button returned:"Ok"}
end tell
[/php]
( Last edited by Diggory Laycock; Jul 9, 2005 at 06:41 AM. )
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 06:09 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,