Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

TheSpaz

macrumors 604
Original poster
Jun 20, 2005
7,032
1
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

macrumors member
Jan 9, 2007
75
0
You need to add:
Code:
set number_of_items to (count (every item of the trash))

For example...

Code:
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

macrumors 604
Original poster
Jun 20, 2005
7,032
1
You need to add:
Code:
set number_of_items to (count (every item of the trash))

For example...

Code:
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

macrumors 604
Original poster
Jun 20, 2005
7,032
1
Here's what I ended up doing to the script:

Code:
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.