|
|
Display the size of the trash prior to emptying
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.
Oooops! Need assistance!
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!?!
Oooops! Need assistance! Revision #5!!
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..
---
Rick Bargerhuff alias cougarProgrammer / 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!]
Oooops! Need assistance! Revision #5!!
It doesn't work... Not sure how to fix it (I assume something got messed up in posting). Can it be reposted? |
SearchFrom our Sponsor...Latest Mountain Lion HintsWhat's New:HintsNo new hintsComments last 2 daysLinks last 2 weeksNo recent new linksWhat's New in the Forums?
Hints by TopicNews from Macworld
From Our Sponsors |
|
Copyright © 2014 IDG Consumer & SMB (Privacy Policy) Contact Us All trademarks and copyrights on this page are owned by their respective owners. |
Visit other IDG sites: |
|
|
|
Created this page in 0.08 seconds |
|