AppleScript and File Creation Dates

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

Tags: , ,

More AppleScript stuff

Creating a PDF from AppleScript

This creates test.pdf on your desktop from a variable containing text:

do shell script ("echo " & quote & your_text_variable & quote &
" | /usr/bin/enscript -q -B --word-wrap -p -f Helvetica12 - 
 | /usr/bin/pstopdf -i -o ~/Desktop/test.pdf")

Posted under AppleScript

This post was written by stetho on December 19, 2008

Tags: ,

AppleScript Questions on allexperts.com

For the past couple of years, I’ve been a voluteer on the questions and answers site, AllExperts.com. In that time I’ve mainly answered questions about broken hard disks and printers but every now and then an AppleScript question has appeared.

I’m going to start putting the questions and answer up on this blog for other people to see. There’s bound to be more than one person having a problem.

Is there a specific AppleScript function to alter the boundaries of Firefox windows?
FireFox AppleScript support is really poor. Almost non-existent in fact. As the windows are part of the window manager you can address them through System Events:

tell application "System Events" to set size of window 1 of process "Firefox" to {300,300}

How do you enable Spaces and add an extra row and column?

tell application "System Events"
tell spaces preferences of expose preferences
set spaces enabled to true
set spaces columns to 3
set spaces rows to 3
end tell
end tell

How can I get all elements of a list except the last one?

if (count of your_list) is greater than 1 then set your list to items 1 thru -2 of your_list

Posted under AppleScript

This post was written by stetho on November 26, 2008

Tags:

AppleScript to convert CMYK to RGB values

A couple of days ago someone asked me if I knew how to convert CMYK to RGB using AppleScript. After boring them silly with my explanation of colour models and explaining that it’s not really possible, I came up with this which is a close approximation and finally gives me something to put on my blog.

on cmykrgb(c, m, y, k)

    set r to 255 - (round (2.55 * (c + k)))
    set g to 255 - (round (2.55 * (m + k)))
    set b to 255 - (round (2.55 * (y + k)))

    if (r < 0) then set r to 0
    if (g < 0) then set g to 0
    if (b < 0) then set b to 0

    return {r, g, b}
end cmykrgb

Posted under AppleScript

This post was written by stetho on June 12, 2008

Tags: , ,