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


Click here to return to the 'Display the size of the trash prior to emptying - Major rev 3 :D' 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 - 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 | # ]