Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Display folder item counts via folder action script Desktop
You can have a folder that maintains its item count in its name. Just attach the following script via Folder Actions:
on adding folder items to thisFolder after receiving addedItems
  my setTheCount(thisFolder)
end adding folder items to

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

on setTheCount(theFolderAlias)
  tell application "Finder"
    set theFolder to (folder theFolderAlias)
    set myCount to the count of (every item of theFolder)
    set nameEnd to ""
    if myCount > 0 then set nameEnd to " (" & myCount & ")"
    set name of theFolder to (comment of theFolder) & nameEnd
  end tell
end setTheCount
And then set the comment of that folder (under Get Info) to the name of the folder. As you add and remove items, the name of the folder will include its item count (for example, Downloads (5)). I tested this with Safari, and it's smart enough to maintain your Downloads folder across the rename.

[robg adds: To use this script, save it as a script to your user's Library -> Scripts -> Folder Action Scripts folder. Then control-click the folder you wish to track and select Attach Folder Action (assuming you've previously enabled Folder Actions). Navigate to the Folder Action Scripts folder and choose the script you just saved. Remember to add the folder's name in the Get Info Comments area, and it should work as described -- it did when I tested it on 10.4.6.]
    •    
  • Currently 3.40 / 5
  You rated: 3 / 5 (5 votes cast)
 
[14,023 views]  

Display folder item counts via folder action script | 10 comments | Create New Account
Click here to return to the 'Display folder item counts via folder action script' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Display folder item counts via folder action script
Authored by: machard on Apr 07, '06 09:42:48AM

Unless I'm missing something, you can select view options (command-J) when on the desktop and select "Show Item Info"



[ Reply to This | # ]
Display folder item counts via folder action script
Authored by: diamondsw on Apr 07, '06 09:58:37AM

However, that only works in icon view. This works in list or column view as well.



[ Reply to This | # ]
Display folder item counts via folder action script
Authored by: morespace54 on Jun 18, '07 11:12:24AM

OMG! I have been on Mac since... 7.0 and never thought about that option... thanks! But this hint is also working on listing (or columns) view.



[ Reply to This | # ]
Display folder item counts via folder action script
Authored by: diamondsw on Apr 07, '06 10:13:49AM

Hold it - what's with the dependency on "comment of theFolder"? Seems like you have to store the name of the folder in the comment ahead of time. A more robust solution would find if there is already a count attached and if necessary remove it, then add the new count.



[ Reply to This | # ]
Display folder item counts via folder action script
Authored by: tmm8a on Apr 07, '06 02:14:39PM
I fixed the need to put the name in the comment with a simple addition to the script. It looks at the comment and if it is blank copies in the name. This makes sure you never lose the folder name.

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

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

on setTheCount(theFolderAlias)
	tell application "Finder"
		set theFolder to (folder theFolderAlias)
		set myCount to the count of (every item of theFolder)
		if comment of theFolder = "" then
			set comment of theFolder to name of theFolder
		end if
		set nameEnd to ""
		if myCount > 0 then set nameEnd to " (" & myCount & ")"
		set name of theFolder to (comment of theFolder) & nameEnd
	end tell
end setTheCount
I played around with trying to strip the "(xx)" at the end of the name string but it seemed to break the script. This is my first look into Applescript so I'm sure it is quite easy to play around with strings but I just couldn't figure it out. I also don't know how to step through a script and see what values the variables have. If I could do this than I would have some idea where the script was failing. It did fail gracefully in that it just did nothing.

[ Reply to This | # ]
No need to store name in Finder Comment
Authored by: Krioni on Apr 10, '06 02:32:30PM
Here's a modified version of the script that extracts the base name of the folder (everything before the last open parenthesis). Also, you could change the delimiters that surround the number to something else by just changing the first two lines of the script. Here it is:

property countStart : space & "("
property countEnd : ")"


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

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

on setTheCount(theFolderAlias)
	
	tell application "Finder"
		set theFolder to (folder theFolderAlias)
		set oldName to name of theFolder
		
		set myCount to the count of (every item of theFolder)
		if myCount > 0 then set nameEnd to " (" & myCount & ")"

	-- here is where Dan Shockley (Krioni) removed need to store folder name in Finder Comment:
		
		if oldName contains countStart and oldName contains countEnd then
			set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, countStart}
			set nameBase to items 1 through -2 of (text items of oldName)
			set nameBase to nameBase as string
			set AppleScript's text item delimiters to od
			set newName to nameBase & countStart & myCount & countEnd
		else -- no previous count to replace
			set newName to oldName & countStart & myCount & countEnd
		end if
		
		set name of theFolder to newName
		
	end tell
end setTheCount


[ Reply to This | # ]
No need to store name in Finder Comment
Authored by: lifeclever on Jul 25, '06 10:45:49AM

This revision to the code is great and makes it a lot easier to setup. Is there a way in AppleScript to find all the aliases of a folder and rename them with the item count to match the original folder as well?

---
http://www.lifeclever.com
Tips for design and life



[ Reply to This | # ]
Display folder item counts via folder action script
Authored by: nickyc on Sep 11, '06 03:13:22AM
Is there a way in AppleScript to find all the aliases of a folder and rename them with the item count to match the original folder as well?

Yep. I've hacked this together -- it's intended for the lifeclever article, so assumes you've stored your aliases on the desktop, and that their original name was the same as the folder they link to. Hope it helps.


property countStart : space & "("
property countEnd : ")"


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

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

on setTheCount(theFolderAlias)
	
	tell application "Finder"
		set theFolder to (folder theFolderAlias)
		set oldName to name of theFolder
		
		set myCount to the count of (every item of theFolder)
		if myCount > 0 then set nameEnd to " (" & myCount & ")"
		
		-- here is where Dan Shockley (Krioni) removed need to store folder name in Finder Comment:
		
		if oldName contains countStart and oldName contains countEnd then
			set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, countStart}
			set nameBase to items 1 through -2 of (text items of oldName)
			set nameBase to nameBase as string
			set AppleScript's text item delimiters to od
			set newName to nameBase & countStart & myCount & countEnd
		else -- no previous count to replace
			set newName to oldName & countStart & myCount & countEnd
		end if
		
		set name of theFolder to newName
		
		-- this addition looks for aliases on the desktop and renames them accordingly
		
		set desktopAliases to every item of desktop where class of it is alias file
		
		repeat with theAlias in desktopAliases
			if oldName contains the name of theAlias then
				set name of theAlias to newName
				update theAlias
			end if
		end repeat
		
		
	end tell
end setTheCount


[ Reply to This | # ]
Display folder item counts via folder action script
Authored by: el heffe on Jun 17, '07 09:22:13PM

Wow, I have been looking for a way to auto-sort my downloads folder, then have it show me the count in some way for a bit now. Of course, I should have known where I would find the answer, Mac OS X Hints. Been reading since the first iteration of this site, and still I find new kernals of gooey knowledge. This is a great hint!



[ Reply to This | # ]
Display folder item counts via folder action script
Authored by: morespace54 on Jun 18, '07 11:20:56AM

Great hint!
Is there a way to add the total size (MB or GB) of files included in the folder?



[ Reply to This | # ]