<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Martin Ström &#187; shell</title>
	<atom:link href="http://my-domain.se/tag/shell/feed/" rel="self" type="application/rss+xml" />
	<link>http://my-domain.se</link>
	<description></description>
	<lastBuildDate>Wed, 11 Jan 2012 11:04:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>Autonamed dump/temp folder</title>
		<link>http://my-domain.se/autonamed-dump-folder/</link>
		<comments>http://my-domain.se/autonamed-dump-folder/#comments</comments>
		<pubDate>Thu, 06 Jul 2006 22:04:23 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[applescript]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[osx]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://burnfield.com/martin/2006/07/07/autonamed-dump-folder/</guid>
		<description><![CDATA[To keep my desktop nice and clean I have a folder called &#8220;Dump&#8221; where I put temporary stuff like apps I want to try, PDF:s to read later that day or files I&#8217;m currently working with. But sometimes I forget to look inside that folder to find out what&#8217;s there so I wrote this Folder [...]]]></description>
			<content:encoded><![CDATA[<p>To keep my desktop nice and clean I have a folder called &#8220;Dump&#8221; where I put temporary stuff like apps I want to try, PDF:s to read later that day or files I&#8217;m currently working with. But sometimes I forget to look inside that folder to find out what&#8217;s there so I wrote this <a href="http://www.apple.com/applescript/folderactions">Folder Action</a> to let the folders name reflect the number of containing files/folders. (I know I could just enabled &#8220;Show item info&#8221; from Finders &#8220;View Options&#8221; but I don&#8217;t want to see all info for my hard drives and iPod)</p>

<p><img src="http://my-domain.se/wordpress/wp-content/uploads/2006/07/autonamed-dump-folder.png" alt="autonamed-dump-folder.png" class="centered" /></p>

<p>Just create a folder on your desktop, name it something like &#8220;Dump (temp)&#8221; and attach this Folder Action to it (Folder Actions need to be located in <code>~/Library/Scripts/Folder Actions</code>).</p>

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

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

<pre><code>#!/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 &lt;&lt;&lt; "$(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)"
</code></pre>

<p>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.</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

