Trash Items and Size AppleScript?

Discussion in 'Mac Programming' started by TheSpaz, Jan 17, 2007.

  1. macrumors 604

    TheSpaz

    Joined:
    Jun 20, 2005
    #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?
     
  2. macrumors member

    Joined:
    Jan 9, 2007
    #2
    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
     
  3. thread starter macrumors 604

    TheSpaz

    Joined:
    Jun 20, 2005
    #3
    THANKS SO MUCH DUDE! Is it okay if I use this code and modify it to my needs. You rock!
     
  4. thread starter macrumors 604

    TheSpaz

    Joined:
    Jun 20, 2005
    #4
    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.
     
  5. macrumors member

    Joined:
    Jan 9, 2007
    #5
    Sure. Go crazy with it.
     

Share This Page