Autonamed dump/temp folder

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
on adding folder items to this_folder
    my update_name(this_folder)
end adding folder items to

on removing folder items from this_folder
    my update_name(this_folder)
end removing folder items from

on update_name(this_folder)
    set this_name to POSIX path of this_folder
    set sh_script to do shell script "P=\"" & (POSIX path of (path to me) as string) & "\";echo ${P/%.scpt/.sh}"
    set new_name to do shell script "\"" & sh_script & "\" \"" & this_name & "\""
    tell application "Finder" to set name of this_folder to new_name
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
# Set item count as folder name.sh

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