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


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: 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 | # ]