PDA

View Full Version : Trash Items and Size AppleScript?




TheSpaz
Jan 17, 2007, 02:10 PM
I'm trying to make a simple script that tells me the number of items in the Trash and also the Size in MB. Here's a line of code I found online that creates a dialog with the Trash size but, I don't know how to make it display the number of items too.

display dialog (do shell script "du -h ~/.Trash | tail -rn1 | awk '{print \"Size of Trash is \" $1}'")

This script works good but, does anyone care to show me how to add the number of items in the Trash too?



applelabs
Jan 17, 2007, 09:50 PM
You need to add:
set number_of_items to (count (every item of the trash))

For example...

tell application "Finder"
set trash_size to (do shell script "du -h ~/.Trash | tail -rn1 | awk '{print \"The size of the Trash is \" $1}'")
set number_of_items to (count (every item of the trash))
activate
display dialog trash_size & return & "The number of items is " & number_of_items & "." with icon stop buttons {"Empty Trash", "OK"} default button 2
if the button returned of the result is "Empty Trash" then
tell application "Finder"
empty trash
end tell
end if
end tell

TheSpaz
Jan 17, 2007, 10:32 PM
You need to add:
set number_of_items to (count (every item of the trash))

For example...

tell application "Finder"
set trash_size to (do shell script "du -h ~/.Trash | tail -rn1 | awk '{print \"The size of the Trash is \" $1}'")
set number_of_items to (count (every item of the trash))
activate
display dialog trash_size & return & "The number of items is " & number_of_items & "." with icon stop buttons {"Empty Trash", "OK"} default button 2
if the button returned of the result is "Empty Trash" then
tell application "Finder"
empty trash
end tell
end if
end tell

THANKS SO MUCH DUDE! Is it okay if I use this code and modify it to my needs. You rock!

TheSpaz
Jan 18, 2007, 08:13 AM
Here's what I ended up doing to the script:


tell application "Finder"
set trash_size to (do shell script "du -h ~/.Trash | tail -rn1 | awk '{print \"\" $1}'")
set number_of_items to (count (every item of the trash))
activate
display dialog "The Trash contains " & number_of_items & " items, which use " & trash_size & " of disk space. Are you sure you want to delete these items?" with icon stop buttons {"Cancel", "Empty Trash"} default button 2
if the button returned of the result is "Empty Trash" then
tell application "Finder"
empty trash
end tell
end if
end tell


Thanks again.

applelabs
Jan 18, 2007, 10:08 AM
THANKS SO MUCH DUDE! Is it okay if I use this code and modify it to my needs. You rock!
Sure. Go crazy with it.