Display folder item counts via folder action script

Apr 07, '06 05:30:00AM

Contributed by: thavron

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.]

Comments (10)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20060401140625207