<?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>stetho:scope &#187; mdls</title>
	<atom:link href="http://blog.stetho.co.uk/tag/mdls/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.stetho.co.uk</link>
	<description>I have my uses</description>
	<lastBuildDate>Mon, 07 Jun 2010 20:06:15 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>AppleScript and File Creation Dates</title>
		<link>http://blog.stetho.co.uk/2009/06/22/applescript-and-file-creation-dates/</link>
		<comments>http://blog.stetho.co.uk/2009/06/22/applescript-and-file-creation-dates/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 21:51:22 +0000</pubDate>
		<dc:creator>stetho</dc:creator>
				<category><![CDATA[AppleScript]]></category>
		<category><![CDATA[mdls]]></category>
		<category><![CDATA[stat]]></category>

		<guid isPermaLink="false">http://blog.stetho.co.uk/?p=37</guid>
		<description><![CDATA[Sorry I&#8217;ve been quiet. I&#8217;m unemployed after being made redundant and I&#8217;m putting all my efforts in to finding a new job. However, I did come across this little problem the other day and wanted to share my solution. Getting a list of folders from the Finder, sorted by their creation date:
property f : alias [...]]]></description>
			<content:encoded><![CDATA[<p>Sorry I&#8217;ve been quiet. I&#8217;m unemployed after being made redundant and I&#8217;m putting all my efforts in to finding a new job. However, I did come across this little problem the other day and wanted to share my solution. Getting a list of folders from the Finder, sorted by their creation date:<br />
<code>property f : alias "Macintosh HD:Developer:"</p>
<p>tell application "Finder"<br />
	set filelist to sort (get folders in f) by creation date<br />
end tell</code></p>
<p>This does the job perfectly for a small number of folders. However, when you execute this on a large number of folder or, as was the case, on a large number of folder on a server volume, it becomes very slow and can even time out. So the way round it is to use the command line. However, Unix in general doesn&#8217;t store a creation date in the inode. By extension, neither does OS X or any version of Linux. But there is a way round this. Apple&#8217;s HFS+, the file system on Macs, does store metadata about a file which can be accessed in other ways. The <strong>mdls</strong> command lists the metadata for a file. Using the -name switch, we can see a specific item of the metadata:<br />
<code><br />
>mdls -name kMDItemFSCreationDate test_doc.pdf<br />
kMDItemFSCreationDate = 2009-02-22 19:13:59 +0000<br />
</code></p>
<p>The <strong>stat</strong> command gives us a better way to do it. <strong>stat</strong> returns the status of a file or folder. The string it returns contains various information including the creation date for the target. Unlike mdls, stat also gives us quite a few command line options that allow us to manipulate the output.<br />
<code><br />
> stat -f "%m%t%Sm %N" test_doc.pdf<br />
1014405239	Feb 22 19:13:59 2002 test_doc.pdf<br />
</code><br />
The -f switch tells stat to format the output using the string that follows. In this case, it&#8217;s timestamp, tab, long date, space, file name.</p>
<p>Next we sort the output numerically in reverse order<br />
<code><br />
stat -f "%m%t%Sm %N" * | sort -rn<br />
</code></p>
<p>And finally, we remove the timestamp that we sorted on by cutting everything in the first column<br />
<code>stat -f "%m%t%Sm %N" * | sort -rn | cut -f2-</code></p>
<p>So now we have code that lists our files in the current directory by creation time, we just need to turn it in to AppleScript.<br />
<code><br />
property f : alias "Server:Work In Progress:"<br />
do shell script "stat -f " &#038; quote &#038; "%m%t%Sm %N" &#038; quote &#038; " " &#038; quoted form of POSIX path of f &#038; "* | sort -rn | cut -f2-"</p>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.stetho.co.uk/2009/06/22/applescript-and-file-creation-dates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
