Sorry I’ve been quiet. I’m unemployed after being made redundant and I’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 "Macintosh HD:Developer:"
tell application "Finder"
set filelist to sort (get folders in f) by creation date
end tell
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’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’s HFS+, the file system on Macs, does store metadata about a file which can be accessed in other ways. The mdls command lists the metadata for a file. Using the -name switch, we can see a specific item of the metadata:
>mdls -name kMDItemFSCreationDate test_doc.pdf
kMDItemFSCreationDate = 2009-02-22 19:13:59 +0000
The stat command gives us a better way to do it. stat 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.
> stat -f "%m%t%Sm %N" test_doc.pdf
1014405239 Feb 22 19:13:59 2002 test_doc.pdf
The -f switch tells stat to format the output using the string that follows. In this case, it’s timestamp, tab, long date, space, file name.
Next we sort the output numerically in reverse order
stat -f "%m%t%Sm %N" * | sort -rn
And finally, we remove the timestamp that we sorted on by cutting everything in the first column
stat -f "%m%t%Sm %N" * | sort -rn | cut -f2-
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.
property f : alias "Server:Work In Progress:"
do shell script "stat -f " & quote & "%m%t%Sm %N" & quote & " " & quoted form of POSIX path of f & "* | sort -rn | cut -f2-"
Posted under AppleScript
This post was written by stetho on June 22, 2009
