To keep my desktop nice and clean I have a folder called “Dump” where I put temporary stuff like apps I want to try, PDF:s to read later that day or files I’m currently working with. But sometimes I forget to look inside that folder to find out what’s there so I wrote this Folder Action to let the folders name reflect the number of containing files/folders. (I know I could just enabled “Show item info” from Finders “View Options” but I don’t want to see all info for my hard drives and iPod)

autonamed-dump-folder.png

Just create a folder on your desktop, name it something like “Dump (temp)” and attach this Folder Action to it (Folder Actions need to be located in ~/Library/Scripts/Folder Actions).

-- Set item count as folder name.scpt<br />
on adding folder items to this_folder<br />
	my update_name(this_folder)<br />
end adding folder items to

on removing folder items from this_folder<br />
	my update_name(this_folder)<br />
end removing folder items from

on update_name(this_folder)<br />
	set this_name to POSIX path of this_folder<br />
	set sh_script to do shell script "P=\"" & (POSIX path of (path to me) as string) & "\";echo ${P/%.scpt/.sh}"<br />
	set new_name to do shell script "\"" & sh_script & "\" \"" & this_name & "\""<br />
	tell application "Finder" to set name of this_folder to new_name<br />
end update_name

The script needs a shell script to work (since I don’t like working with AppleScript that much). It should be named exacly as the AppleScript but with an .sh extension instead of .scpt and put in the same directory. Remember to make it executable (chmod +x "Set item count as folder name.sh").

#!/usr/bin/env bash<br />
# Set item count as folder name.sh

# Since the folder has a custom icon and therefore a `Icon?` file I<br />
# decrease the item count by 1. Bad solution but works for now.<br />
NUM_ITEMS=`bc <<< "$(ls "$1" | wc -l) - 1"`<br />
if [[ $NUM_ITEMS == 0 ]]; then<br />
	ITEMS="empty"<br />
elif [[ $NUM_ITEMS == 1 ]]; then<br />
	ITEMS="$NUM_ITEMS item"<br />
else<br />
	ITEMS="$NUM_ITEMS items"<br />
fi<br />
BASE=`ruby -e "puts %x(basename \"$1\").match(/(.+) \(/)"[1]`<br />
echo "$BASE ($ITEMS)"

I first renamed the folder from the shell script but then Finder would visually move the folder one step down or left on the desktop for every update. Instead I just let the shell script generatr the new name and pass it back to the AppleScript that will rename the file as I want.