|
|
Rev. 6 - Gold Master?
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!
Rev. 6 - Gold Master?
Hey iPost and everyone else!
Rev. 6 - Gold Master?
Glad to help.
Rev. 7
Added secure file erasing, and it outputs to a text file named Trashdata.txt in the documents folder, enjoy!
Rev. 6.5 International Version
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.
---
Rev. 6 - Gold Master?
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: Edited on May 14, '13 07:41:13AM by acc
|
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.09 seconds |
|