Return Folder Size (Trash)

Hey Guys

I am working on a bit of code to return the value of my trash can, but i can’t seem to get it to work

Any ideas?

I am using:


tell application "Finder"
	set x to "$Home/Pictures:" as alias 
	get physical size of folder x as integer
	set y to result
	display dialog y
end tell

’ using pictures as a test folder

Cheers

Hi,

UNIX path variables like $HOME don’t work in AppleScript,
there is the path to command to specify well-known folders
For example the alias to the pictures folder is


set x to path to pictures folder

to retrieve the size of the trash folder you can use this,
it considers that the Finder takes some time to calculate the size


tell application "Finder"
	repeat until size of trash is not missing value
		delay 0.2
	end repeat
	display dialog (get size of trash as text)
end tell

Hey Stefan,

That is not working OMM running Snow Leopard. I get missing value for any folder I try.
I get the same thing when using physical size. Not sure what is up with that.

How is this for a shell command?

set thePath to quoted form of POSIX path of (path to pictures folder)
do shell script "du -sh " & thePath & " | awk '{print $1}'"

#Or for the trash

do shell script "du -sh ~/.Trash | awk '{print $1}'"

Hi Craig,

does the deprecated info for work, or doesn’t info for work at all in 10.6 ?

Yes, this works.


set theSize to size of (info for theFolder)

Cheers craig / Stefan

I went with this in the end:


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 "Trash contains " & number_of_items & " items, which use " & trash_size & " of disk space. Do 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


almost everything after the display dialog line is not necessary.
display dialog aborts the script automatically if the user presses the Cancel button
and the Finder tell block is defined at the beginning


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

cheers Stefan

Just anouther question,

I have been playing arround with a bit of code to add the following sizes together and display a total

Desktop
Pictures
Music
Documents
Movies

I have used the method above, however, when i run it i get Cannot Turn 40G into a number.

Any Ideas Guys? :frowning:

Hi, I found various ways of getting a number,
But you would still need to distinguish the actual size in KB MB GB TB…

a return of 40G I think is not very helpful in calculating you results.
a return in kb may be better. Du offers a -k option to do this.

set trash_sizeKB  to (do shell script "du -k  -c ~/ | tail -rn1 | awk '{print \"\" $1}' ") -- option -k uses Kb to display the results.

set trash_sizeMB to ((trash_sizeKB / 1024 * 100000) / 100000) --MB
set trash_sizeGB to ((trash_sizeKB / 1048576 * 100000) / 100000) --GB
set trash_sizeTB to ((trash_sizeKB * 100000 / 1.073741824E+9) / 100000) --TB

I’m sure some one will improve this… :slight_smile: