Size of Folder in its Name

It’s occasionally useful to keep a running tally of the size of a folder as you fill it. The script below running as a folder action will append the folder size to the folder name: MyFolder[73] where the answer is in kB. It will start correctly with a folder whose name does not contain the brackets. Obviously, the folder name itself may not include brackets. If you have used [ … ] in your folder name, switch the script below to some other delimiter, { … }, for example.

on adding folder items to thisFolder after receiving addedItems
	my setTheSize(thisFolder)
end adding folder items to

on removing folder items from thisFolder after losing addedItems
	my setTheSize(thisFolder)
end removing folder items from

on setTheSize(theFolderAlias)
	tell application "Finder"
		set theFolder to (theFolderAlias)
		set theName to name of theFolder
		set mySize to (size of (info for theFolder)) / 1024 as integer -- corrected per Nigel Garvey's later post
		set otid to AppleScript's text item delimiters
		set AppleScript's text item delimiters to "["
		set baseName to text item 1 of theName
		set AppleScript's text item delimiters to otid
		if mySize > 0 then
			set nameEnd to "[" & mySize & "]"
			set name of theFolder to baseName & nameEnd
		else
			if mySize = 0 then set name of theFolder to baseName
		end if
	end tell
end setTheSize

was wondering if theres anything else that has to be done

attached as folder action
but still dont see name ??

:smiley:

Hi doglok.

The name (under or next to the icon) of the folder to which the script is attached will change.

I just attached the script to a folder named “test action” which became “test actions[331927]” after I dropped another folder into it.

Thanks for the script Adam.

j

The divisor should be 1024! :slight_smile: