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

Display the size of the trash prior to emptying System
I posted this to this thread on the forums last night, and got a bunch of emails saying how useful it was ... so I figured I'd submit it to the main site. I'll throw in the relevant bits below, but the link probably explains it better than I...

I was kind of bored, so I decided to mess around. I came up with this script, which I then saved as a run-only application, and threw in my dock next to the trash can. I'm sure it can be improved a lot (for instance, this only counts the amount in your user trash (not any other volumes), but it works for me.
set amount to do shell script "du -sk ~/.trash | awk '{print $1}'"
display dialog "There are " & amount & " kb in trash.  Would you like to empty?"
if button returned of result = "OK" then
  tell application "Finder"
    empty trash
  end tell
end if
Maybe I should explain what (I think...) this code does.

The first line uses the 'du' terminal command and then takes the kilobytes from the first column and stores it in a variable. The next line displays the message including the size of the trash. The next line checks to see if you said OK, and if it does, tells the finder to empty the trash. That's it. To change it to megabytes instead of kilobytes you could just insert this between the first and second lines:
set amount to amount / 1024
And change the dialog from kb to mb.

[robg adds: I tested this, and it worked as described...]
    •    
  • Currently 3.60 / 5
  You rated: 4 / 5 (5 votes cast)
 
[15,319 views]  

Display the size of the trash prior to emptying | 41 comments | Create New Account
Click here to return to the 'Display the size of the trash prior to emptying' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Display the size of the trash prior to emptying
Authored by: marcpage on Feb 28, '03 10:29:13AM

This only works for things thrown into the trash on the boot disk. The size of the trash on disk images, network drives and removable media will not be included in the reported size, but will be deleted when the trash is emptied.



[ Reply to This | # ]
...and partitions...
Authored by: mervTormel on Feb 28, '03 03:17:34PM

$ sudo du -sk ~/.trash /Volumes/*/.trashes/ | \
awk "{sum += \$1} END{print sum}" ${1+"$@"}



[ Reply to This | # ]
...and partitions...
Authored by: Accura on Mar 01, '03 03:58:31AM

sudo du -skc ~/.Trash/ /.Trashes/ /Volumes/*/.Trashes/ | grep total | awk '{print $1}'

i also wrote a basic konfabulator widget to do this (i was bored), it needs some one to do the art work but ill post the code/widget if any one wants it. its faster than using the apple script.

http://kmirror.deskmod.com is the konfabulator home page.

Konfabulator is a program that lets you run widgets (like weather, iTunes remotes, wrapers for cgi utils) that are writen in xml and java script. The widgets are easy to write.

---
"The time has come," the walrus said. "To talk of many things..."



[ Reply to This | # ]
Display the size of the trash prior to emptying
Authored by: VEGx on Feb 28, '03 10:34:02AM

Except that sometimes you will get an annoying .DS_Store file in it!!!<p>You might want to check for that file first and if it exsist then substract 4K.<p>I mean, it tells the right size of the .Trash, sure, but normally I would not really want to include the .DS_Store files to my calculations. ["I put a 4K text file to the Trash and now it's TWICE the size!!?" kind of a situation.]<p>Just a small correction... so that no one will panic about this 4K "mistake"



[ Reply to This | # ]
Display the size of the trash prior to emptying
Authored by: JoeGrind on Feb 28, '03 11:07:31AM
Maybe that additional line could be changed to...

if amount > 1024 set amount to amount / 1024

To only change to MB if the size is over a MB.

[ Reply to This | # ]
Display the size of the trash prior to emptying
Authored by: nosaj56 on Feb 28, '03 01:02:40PM
Actually, that additional line should be:

if amount > 1024 then set amount to amount / 1024

[ Reply to This | # ]
How about a file count
Authored by: robmorton on Feb 28, '03 12:39:57PM

Any one have an idea how to get the total file count of a folder or disk?



[ Reply to This | # ]
How about a file count
Authored by: pierrot-lunaire on Mar 01, '03 06:10:41AM

There are many ways for this. The two I use regularly are:

ls <folder> | wc -l
quick 'n dirty, gives the number of objects (files, folders, links..), no
recursion, no .files.

If you want recursion OR if you want to distinguish files from folders OR
if you want to include .files, use find:

find <folder> -type f -maxdepth 1 | wc -l

this is for files only, includes .files.
Use -type d for directories only, remove -maxdepth 1 to find all files/
folders recursively, remove -type argument to find everything (like ls
-a).

Use man wc to find information on wc (probably coming from 'word
count' - the -l options counts lines.



[ Reply to This | # ]
Display the size of the trash prior to emptying
Authored by: sophistry on Feb 28, '03 02:22:10PM

You can convert the kilos into megas using this expanded unix
command. It's actually just useful to show a few shell techniques like
command substitution and formatting strings to send to bc.

do command substitution on the du command to get it into printf to
format a stream suitable to send to bc. notice the use of cut rather than
awk '{print $1}' since it is a simpler tool for the job.

printf 'scale=2;%s/1024\n' `du -sk ~/.trash | awk '{print $1}'` | bc -l
versus
printf 'scale=2;%s/1024\n' `du -sk ~/.trash | cut -f1` | bc -l

note: using single quotes for surrounding the printf string in case you
want to use this in an applescript.



[ Reply to This | # ]
update: Display the size of the trash prior to emptying
Authored by: sophistry on Feb 28, '03 02:27:59PM

I just realized a simple way to get the MB reading is to just use the built-
in ability of awk to do lots of amazing things that you alwys forget about:

du -sk ~/.trash | awk '{print $1/1024}'

does the trick.



[ Reply to This | # ]
Okay, now let's make it pretty...
Authored by: GORDYmac on Feb 28, '03 06:47:22PM

Thanks so much for the tip. I compiled everything and added an icon and a 5 second timeout:

set amount to do shell script "printf 'scale=2;%s/1024
' `du -sk ~/.trash | awk '{print $1}'` | bc -l "
display dialog "There are " & amount & " MB in the trash. " & return & "Would you like to empty it?" with icon caution giving up after 5
if button returned of result = "OK" then
tell application "Finder"
empty trash
end tell
end if



[ Reply to This | # ]
Display the size of the trash prior to emptying - Major revision to include above requests
Authored by: cougar718 on Feb 28, '03 08:40:52PM

on run {}
set thepath to path to trash -- The folder to size up
-- Get the contents of the trash
set thefiles to list folder thepath -- Gets the directory contents of of the folder specified above
set filteredlist to {} -- Holds the files that don't begin with "."
-- Build the list of files
repeat with thefile in thefiles
if (thefile does not start with ".") then
set end of filteredlist to thefile as string
end if
end repeat
set amount to GetSize(thepath) --Get the physcial size of the folder
set filtercount to count of filteredlist --Count of items in filteredlist
set filecount to count of thefiles --Count of items in thefiles
-- Show user the information gathered
display dialog "There is " & amount & " in the trash. Consists of..." & return
& filecount - filtercount & " invisible(s)" & return
& filtercount & " normal(s)" & return & return
& "Total Files : " & filecount & return & return
& "Would you like to empty the trash?" buttons {"No", "Yes"} default button 2
if button returned of result = "Yes" then
tell application "Finder" to empty trash
end if
end run

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
if (thesize > 1024) then
set thesize to thesize / 1024 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 GetSize

---
Rick Bargerhuff alias cougar
Programmer / Developer
Apple Technician / Specialist / Web Designer
Personal Email: cougar718@comcast.net



[ Reply to This | # ]
Display the size of the trash prior to emptying - Major revision to include above requests
Authored by: toddsnc on Mar 01, '03 02:23:47PM

nice!



[ Reply to This | # ]
Human Readable
Authored by: Anonymous on Mar 01, '03 11:44:43AM

Umm... is there any reason why you're not using the human readable
flag?

du -sh

from the man page:

"-h, --human-readable
print sizes in human readable format (e.g., 1K 234M 2G)"



[ Reply to This | # ]
Human Readable
Authored by: extra88 on Mar 01, '03 01:33:35PM
I was thinking that when I was reading the thread (coincidentally I only learned of the -h switch yesterday when I was doing something on a Linux box). Unfortunately 'du' in Mac OS X doesn't support the -h switch and has no equivalent. The 'du' command in Linux distros has this "human readable" switch and a number of other switches for controlling the display but unfortunately OS X uses a more limited version of the command.

The OSXFAQ site includes Mac OS X man pages

[ Reply to This | # ]

Human Readable
Authored by: Anonymous on Mar 03, '03 12:25:11PM

sorry, you're right... i had forgotten that i had installed the fileutils package in Fink. that was what let me use -h with du.



[ Reply to This | # ]
Even cooler?
Authored by: iPost on Mar 01, '03 09:10:12PM

So far this is one of the coolest hints I've seen yet.

However, if you really want to make this gorgeous, there is one more thing that you can do to it -- but would probably require AS Studio...

With an old Connectix utility called Speed Doubler [I think], you could actually get a dialog window with a scrollable list of everything in the trash before you emptied it, each time you went to empty it. I loved that.

So far you've come very close to expanding this script to that kind of coolness. Anyone up for that kind of challenge?



[ Reply to This | # ]
Display the size of the trash prior to emptying - Major revision 2 to include above requests :D
Authored by: cougar718 on Mar 01, '03 11:50:48PM

Here you go everyone. This targets everything said above. Let me know what everyone thinks. (The View List option sees two buttons, both do the same thing, I could not make it just one button)

on run {}
set thepath to path to trash -- The folder to size up
-- Get the contents of the trash
set thefiles to list folder thepath -- Gets the directory contents of of the folder specified above
set filteredlist to {} -- Holds the files that don't begin with "."
-- Build the list of files
repeat with thefile in thefiles
if (thefile does not start with ".") then
set end of filteredlist to thefile as string
end if
end repeat
set amount to GetSize(thepath) --Get the physcial size of the folder
if (amount = "0 kB") then
display dialog "Trash currently contains no items" buttons {"Ok"} default button 1
return false
end if
set filtercount to count of filteredlist --Count of items in filteredlist
set filecount to count of thefiles --Count of items in thefiles
set thebutton to ""
repeat until thebutton is in {"Yes", "No"}
-- Show user the information gathered
display dialog "There is " & amount & " in the trash. Consists of..." & return
& filecount - filtercount & " invisible(s)" & return
& filtercount & " normal(s)" & return & return
& "Total Files : " & filecount & return & return
& "Would you like to empty the trash?" buttons {"View Files", "No", "Yes"} default button 2
set thebutton to the button returned of result
if thebutton = "Yes" then
tell application "Finder" to empty trash
else if thebutton = "View Files" then
set thelist to my CreateList(thefiles, thepath)
choose from list thelist with prompt "Current Files in the Trash" OK button name "Back" with empty selection allowed
end if
end repeat
end run

on CreateList(thefiles, thepath)
set newlist to {}
repeat with i from 1 to (count of thefiles)
tell application "Finder"
set filepath to ((thepath & item i of thefiles) as string) as alias
set theamount to my GetSize(filepath)
set end of newlist to theamount & " - " & (item i of thefiles) as string
end tell
end repeat
return newlist
end CreateList

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
if (thesize > 1024) then
set thesize to thesize / 1024 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 GetSize

---
Rick Bargerhuff alias cougar
Programmer / Developer
Apple Technician / Specialist / Web Designer
Personal Email: cougar718@comcast.net



[ Reply to This | # ]
Display the size of the trash prior to emptying - Major revision 2 to include above requests :D
Authored by: MaxMarino on Mar 02, '03 06:09:52AM

I do not do Applescript (yet?) but shouldn't the line from GetSize()


tell application "Finder" to set thesize to ((physical size of thepath) / 1024)

be instead

tell application "Finder" to set thesize to ((physical size of thepath))

then the subsequent check whether thesize is >1024 is meaningful. Othewise looks to me that you get 0 everytime thesize is less than 1024 (int).

PS

I tried it but it does not see any file in the Trash: always sees Trash as empty.



[ Reply to This | # ]
Display the size of the trash prior to emptying - Major revision 2 to include above requests :D
Authored by: bostmass on Mar 02, '03 08:24:35AM

This is great. Unfortunately, it doesn't tell you if you have anything from another volume or partition. Other than that, it works like a charm.



[ Reply to This | # ]
Display the size of the trash prior to emptying - Major revision 2 to include above requests :D
Authored by: MaxMarino on Mar 02, '03 09:34:18AM

Ahhh, that's it.

I have 4 partitions.



[ Reply to This | # ]
Display the size of the trash prior to emptying - Major rev 3 :D
Authored by: cougar718 on Mar 02, '03 05:52:44PM

Hello everyone!

I'm lovin' the input. Anyway, here is a revision that includes above and emailed requests I got. This should work real well. Please test it and let me know via email if you have problems. The only partition I could mount was a Zip Disk. So I had to do the testing with a Zip Disk and Main volume.

Changes : * This will now recursively list files inside directorys inside the trash bin. * Now retrieves trash information on all volumes.

property trashcontents : {} -- Holds the list of trash contents for each directory
property trashamount : 0
property itemstoskip : 0 -- Holds how many trash folders were processed
property filelist : {} -- Holds contents of traversed directorys

on run {}
-- Initalize variables
set trashcontents to {}
set trashamount to 0
set itemstoskip to 0
set filelist to {}
-- Grab a list of trash contents for each user
set thepath to path to users folder -- The folder to size up
set list_users to list folder thepath
repeat with userfolder in list_users
if ((userfolder as string) does not start with ".") and ((userfolder as string) is not "Shared") then
try -- See if the trashfolder exists
my GetTrashContents(((path to users folder) as string) & userfolder & ":.Trash" as alias)
if (filelist is not {}) then
set end of trashcontents to "** User " & userfolder & "'s Trash Contents **" as string
set itemstoskip to itemstoskip + 1
set trashcontents to trashcontents & filelist
set filelist to {}
end if
end try
end if
end repeat
--Grab a list of trash contents for each volume mounted
set list_volumes to list disks
repeat with thevolume in list_volumes
try
my GetTrashContents(thevolume & ":.Trashes" as alias)
if (filelist is not {}) then
set end of trashcontents to "** Volume " & thevolume & "'s Trash Contents **" as string
set itemstoskip to itemstoskip + 1
set trashcontents to trashcontents & filelist
set filelist to {}
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
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 ""
repeat until thebutton is in {"Yes", "No"}
-- Show user the information gathered
display dialog "There is " & amount & " in the trash. Consists of..." & return
& filtercount & " invisible(s)" & return
& filecount - filtercount & " normal(s)" & return & return
& "Total Files : " & filecount & return & return
& "Would you like to empty the trash?" buttons {"View Files", "No", "Yes"} default button 2
set thebutton to the button returned of result
if thebutton = "Yes" then
tell application "Finder" to empty trash
else if thebutton = "View Files" then
choose from list trashcontents with prompt "Current Files in the Trash" OK button name "Back" with empty selection allowed
end if
end repeat
end run

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(thesize)
if (thesize > 1024) then
set thesize to thesize / 1024 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)
-- 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, which is 18 items , its a record
if folder of theinfo then
set end of filelist to name of theinfo & ":" as string
my GetTrashContents(thealias) -- Call the function
else
set theamount to my GetSize(thealias)
set trashamount to trashamount + theamount -- increment trashamount by theamount
set theamount to my FormatSize(theamount)
set end of filelist to theamount & " - " & name of theinfo as string
end if
end repeat
end GetTrashContents

---
Rick Bargerhuff alias cougar
Programmer / Developer
Apple Technician / Specialist / Web Designer
Personal Email: cougar718@comcast.net



[ Reply to This | # ]
Display the size of the trash prior to emptying - Major rev 3 :D
Authored by: cougar718 on Mar 02, '03 06:54:31PM

Don't use this , there is a problem with the recursion.

Thanks.

Rick

---
Rick Bargerhuff alias cougar
Programmer / Developer
Apple Technician / Specialist / Web Designer
Personal Email: cougar718@comcast.net



[ Reply to This | # ]
Display the size of the trash prior to emptying - Major rev 4 :D
Authored by: cougar718 on Mar 02, '03 09:05:12PM

Hello everyone!

Well I'm having fun with this script. Love helpin' others and I think this one will be the final revision unless somone finds a BUG. The previous revision, #3, had a problem because it was recursively listing files inside folders and this I thought was too much. Also, revision #3 would check each user folder's .Trash folder and this I thought this would not be a good idea.

This revision I had had no problems with. I stuck 64 files inside my trash bin and 64 files inside another partitions trash bin and this worked out well.

on run {}
-- Initalize variables
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
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 ""
repeat until thebutton is in {"Yes", "No"}
-- Show user the information gathered
display dialog "There is " & amount & " in the trash. Consists of..." & return
& filtercount & " invisible(s)" & return
& filecount - filtercount & " normal(s)" & return & return
& "Total Files : " & filecount & return & return
& "Would you like to empty the trash?" buttons {"View Files", "No", "Yes"} default button 2
set thebutton to the button returned of result
if thebutton = "Yes" then
tell application "Finder" to empty trash
else if thebutton = "View Files" then
choose from list trashcontents with prompt "Current Files in the Trash" OK button name "Back" with empty selection allowed
end if
end repeat
end run

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(thesize)
if (thesize > 1024) then
set thesize to thesize / 1024 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
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

---
Rick Bargerhuff alias cougar
Programmer / Developer
Apple Technician / Specialist / Web Designer
Personal Email: cougar718@comcast.net



[ Reply to This | # ]
Display the size of the trash prior to emptying - Major rev 4 :D
Authored by: nosaj56 on Mar 02, '03 09:41:15PM

The application is perfect, except you have one little syntax error. On line 31, "if {trashamount ? 0} then" should be "if {trashamount is 0} then" . Here is the fixed code:

on run {}
-- Initalize variables
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 is 0} then
display dialog "Trash currently contains no items" buttons {"Ok"} default button 1
return false
end if
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 ""
repeat until thebutton is in {"Yes", "No"}
-- Show user the information gathered
display dialog "There is " & amount & " in the trash. Consists of..." & return
& filtercount & " invisible(s)" & return
& filecount - filtercount & " normal(s)" & return & return
& "Total Files : " & filecount & return & return
& "Would you like to empty the trash?" buttons {"View Files", "No", "Yes"} default button 2
set thebutton to the button returned of result
if thebutton = "Yes" then
tell application "Finder" to empty trash
else if thebutton = "View Files" then
choose from list trashcontents with prompt "Current Files in the Trash" OK button name "Back" with empty selection allowed
end if
end repeat
end run

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(thesize)
if (thesize > 1024) then
set thesize to thesize / 1024 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
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



[ Reply to This | # ]
Display the size of the trash prior to emptying - Major rev 4 :D
Authored by: cougar718 on Mar 02, '03 09:57:48PM

Hiya Nosa!

It is not really a syntax error, either one will work. I tested the "is" versus <= 0 and I had the same results. If you had different results that would be strange. Let me know ok.

Thanks for your input!!

---
Rick Bargerhuff alias cougar
Programmer / Developer
Apple Technician / Specialist / Web Designer
Personal Email: cougar718@comcast.net



[ Reply to This | # ]
Display the size of the trash prior to emptying - Major rev 4 :D
Authored by: cougar718 on Mar 02, '03 10:04:01PM

Oh I see now. Wish I would have seen that before my last post... HAHA. I thought you didn't know what I was doing which was indicated by the "?" mark. So what happened...

MacOSXhints put a ? question mark there. It should be this instead...

if (trashamount <= 0) then
display dialog "Trash currently contains no items" buttons {"Ok"} default button 1
return false
end if

Compile to get the less than or equal sign.

Thanks Noja for seeing this.

---
Rick Bargerhuff alias cougar
Programmer / Developer
Apple Technician / Specialist / Web Designer
Personal Email: cougar718@comcast.net



[ Reply to This | # ]
Display the size of the trash prior to emptying - Major rev 4 :D
Authored by: nosaj56 on Mar 02, '03 10:58:58PM

I changed "is" on that line to what you told me to change it to. Script Editor 2.0b changed that to ≤. I get the same result as with "is." The script wouldn't complie with the "?".

[ Reply to This | # ]

Display the size of the trash prior to emptying - Major rev 4 :D
Authored by: maxruffi on Mar 02, '03 10:10:28PM
Hi, Rick. A little question: with your script, my trash don't detect folders, only files.You can help me?
My mac is a Cube 450 MHz, with Jaguar 10.2.4.
Thanks in advance

---
Max Ruffi

[ Reply to This | # ]

Display the size of the trash prior to emptying - Major rev 4 :D
Authored by: iPost on Mar 03, '03 03:23:03AM

RICK, YOU ROCK!



[ Reply to This | # ]
Display the size of the trash prior to emptying
Authored by: iPost on Mar 03, '03 04:24:23AM
Okay, here are my changes, take 'em or leave 'em... But this is exactly what I was looking for... I am very happy. And I could have never done this from scratch so thank you, thank you, thank you, Rick! Here goes: ---

on run {}
	-- Initalize variables 
	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
	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 ""
	-- Show user the information gathered 
	choose from list trashcontents with prompt "There are " & filecount & " files" & " in the trash totaling " & amount & return & return 
		& "Empty Trash?" OK button name "Okay" with empty selection allowed
	if thebutton = "Okay" then
		tell application "Finder" to empty trash
		if thebutton = false then
		end if
	end if
end run

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(thesize)
	if (thesize > 1024) then
		set thesize to thesize / 1024 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
		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

--- As you can see, I put the file list right up from and made it just okay or cancel. I also re-ordered and combined the size and count into one coherent sentence. Hope you find it an improvement.

[ Reply to This | # ]
Oooops! Need assistance!
Authored by: iPost on Mar 03, '03 05:04:11AM

Boy do I feel like a heel. I guess I don't understand the handling of prompts as opposed to dialogs. The Trash doesn't actually get emptied as it is. If I remove the button qualifiers, it works, but the Trash gets emptied even on Cancel!?!

I can't find any info on prompt buttons, and I really need to get to sleep!

I like the look of the prompt I made, can anyone help make it truly usable but making the Cancel cancel and Okay be okay?

Thanks.



[ Reply to This | # ]
Oooops! Need assistance! Revision #5!!
Authored by: cougar718 on Mar 03, '03 04:09:58PM
Hey ipost! Thanks for the compliments. Your problem is a limitation which is why I was not able to go right to the list as you did. After doing some trick coding :D I was able to pull off what you wanted to do. Here it is..

on run {}
  -- Initalize variables 
  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
  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 \"\"
  -- Show user the information gathered 
  set theResult to choose from list trashcontents with prompt \"There are \" 
  & filecount & \" files\" & \" in the trash totaling \" & amount & return & return 
    & \"Empty Trash?\" OK button name \"Cancel\" cancel button name 
    \"Ok\" with empty selection allowed
  try
    if (not theResult) then
      tell application \"Finder\" to empty trash
      if thebutton = false then
      end if
    end if
  end try
end run

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(thesize)
  if (thesize > 1024) then
    set thesize to thesize / 1024 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
    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
--- Rick Bargerhuff alias cougar
Programmer / Developer
Apple Technician / Specialist / Web Designer
Personal Email: cougar718@comcast.net

[robg adds: I cleaned up the formatting here at Rick's request, so if anything's broken now, it's probably my fault!]

[ Reply to This | # ]
Oooops! Need assistance! Revision #5!!
Authored by: cowboy on Mar 04, '03 12:26:47AM

It doesn't work... Not sure how to fix it (I assume something got messed up in posting). Can it be reposted?

Thanks!!



[ Reply to This | # ]
Just missing one thing...
Authored by: GORDYmac on Mar 03, '03 10:57:17AM

This is, by far, the coolest tip on MacOSXHints so far.

I need one thing to make my app perfect; text in the title bar. Is that possible? I just want it to say "Empty Trash" or "Trash Info" or something like that. I can't find anything, anywhere, on this.



[ Reply to This | # ]
Rev. 6 - Gold Master?
Authored by: iPost on Mar 04, '03 06:24:48AM
Okay! This should be the final :) Rick fixed my dilemma and it works... however, the code in his post as a reply to me included a bunch of extra backslashes that screwed up the script. Here is EVERYTHING everyone has asked for (except a window name for the dialog window -- but I simply don't think it is possible with a dialog created in Applescript). Again 99% of this is Rick's, I just had a suggestion and did some rearranging for aesthetics. So once more, thanks cougar! Here we go:
on run {}
	-- Initalize variables 
	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
	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 ""
	-- Show user the information gathered 
	set theResult to choose from list trashcontents with prompt 
		"There are " & filecount & " files" & " in the trash totaling " & amount & return & return 
		& "Empty Trash?" OK button name "Cancel" cancel button name 
		"Ok" with empty selection allowed
	try
		if (not theResult) then
			tell application "Finder" to empty trash
			if thebutton = false then
			end if
		end if
	end try
end run

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(thesize)
	if (thesize > 1024) then
		set thesize to thesize / 1024 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
		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
Enjoy!

[ Reply to This | # ]
Rev. 6 - Gold Master?
Authored by: cougar718 on Mar 04, '03 08:17:11AM

Hey iPost and everyone else!

Looks good to me everyone. Thanks for the repost iPost.
Hehe, I gotta learn how to post! :D

Well it was fun enhancin' this script for you all...I think it is finally done.

Thanks and your welcome all.

Cheers,

---
Rick Bargerhuff alias cougar
Programmer / Developer
Apple Technician / Specialist / Web Designer
Personal Email: cougar718@comcast.net



[ Reply to This | # ]
Rev. 6 - Gold Master?
Authored by: iPost on Mar 04, '03 12:14:34PM

Glad to help.

In the future, to post Applescript I recommend that you change the tag button below the message field to "HTML Formatted."

Then before the script use <pre> and after </pre> so the script's preformatting sticks.

Thanks again, Rick!



[ Reply to This | # ]
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 | # ]
Rev. 6.5 International Version
Authored by: Nimitz on May 15, '06 06:25:25AM
Of course, we silly Europeans use the comma as a decimal placeholder, so all the scripts won't work unless adapted as below. Is there anyone who knows where to get this info (now it is hardwired in a property) out of the System settings (Preference pane International)? This is still the weakest point of Applescript (and Apple): Internationalization.

property decimaldelimiter : ","

on run {}
	-- Initalize variables 
	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
	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 ""
	-- Show user the information gathered 
	set theResult to choose from list trashcontents with prompt 
		"There are " & filecount & " files" & " in the trash totaling " & amount & return & return 
		& "Empty Trash?" OK button name "Cancel" cancel button name 
		"Ok" with empty selection allowed
	try
		if (not theResult) then
			tell application "Finder" to empty trash
			if thebutton = false then
			end if
		end if
	end try
end run

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(thesize)
	if (thesize > 1024) then
		set thesize to thesize / 1024 as string
		set olddelim to AppleScript's text item delimiters
		set AppleScript's text item delimiters to decimaldelimiter
		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
		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

---
-----------
The Nimitz

[ Reply to This | # ]

Rev. 6 - Gold Master?
Authored by: acc on May 14, '13 07:37:36AM

Unfortunately, it does not work for me. I have just one volume. When I run the script I get a dialog with the following text and an OK button:

Trash currently contains no items.

There is in fact almost 7 gig in trash! Any thoughts?? The original short version works, it is Rev. 6 that doesn't.

Edited on May 14, '13 07:41:13AM by acc



[ Reply to This | # ]