23 May 2007

My First Ever Applescript

A discussion on dpreview.com about Aperture and how it doesn't read maker note EXIF fields, specifically the Lens ID, got me thinking. The wonderful exiftool can read this data, why can't Aperture with its team of crack Apple programmers?

I thought of a workaround. Aperture supports the addition of arbitrary metadata to images, and I use it for things like saving the URL and flickr ID of the photos I upload there. Why not read the data from the image using exiftool and stick the result into a new custom field.

I have diddled around with Applescript in the past, but never really did anything that worked. I got bits & pieces working here and there, but never finished because the need went away, I figured out how to do it some other way (like in Perl), found someone else's solution or some similar result. This task didn't seem too hard, so I poked around and found out that there is Applescript support to write custom metadata.

I then encountered the big hurdle. Talking to Aperture with Applescript is easy and you can do a lot of stuff, but everything is relative to the program's internal database, yet I need the path to the actual image file so that exiftool can do its thing. I didn't see an easy way to get Aperture to reveal a path to the original file or get a reference to the image in Aperture's database based on the path of the image file.

Frustrating! I know both bits of data, I just had no clue how to use them to get to my end result. And I still don't, really, because somebody else figured it out and did the heavy lifting for me: Brett Gross' "Reveal Photo File" Applescript. This handy tool will reveal in the Finder the currently selected image(s) in Aperture. Mr. Gross was kind enough to distribute his Applescript in a readable form, so I was able to yank out the bits that open the finder window and instead squirt the path over to exiftool, get the data I need and then write it back to Aperture.

Extended Metadata in Aperture

So now this script is chugging away on my 15k images. It's not the fastest, but most of the slowdown seems to be due to the size of my Aperture library. I tried it on a smaller library of about 200 images and it was done in a minute or two. The current task has been running for a couple days, and at the rate it is going, it will require a couple more. Really strange. And frustrating, because my CPUs are never going above 50% utilization and I have a >1GB of free RAM. I guess the disk is the bottleneck here. I just don't understand why the execution time is so non-linear.

My next step will be to write a new version of the script that will do this all at import, which I hope will be faster.

Please read my post at dpreview.com for the download link and instructions for use.

Labels: , ,


At Thursday, 24 May, 2007 , Anonymous Steven said...

Are your images organized such that there are lots of them per directory? It's just a wild guess, but I have seen situations in both Linux and Windows (i.e. two very different OSes) where directories with lots of files in them cause very long directory lookup times, so that you get an n^2 effect if you're doing something do every file.

At Thursday, 24 May, 2007 , Blogger Allan said...

Steven - Thanks for the insight. Now that you mention it, I seem to remember something about that in computer science classes way back when.

I think you may be right - there is a lot of stuff in these directories and lots of files to search though. I am trying to come up with some experiments to check this.

At Friday, 08 June, 2007 , Anonymous Flo said...

I think I found a bug:
(and an explanation for "Can't get item 2 of {""}.")

My Aperture Library is on a different volume, but you don't put this volume's path into the image's path.

So exiftool does not find the file.

Debugging changes to the script:
-- Ask exiftool for tab-separated fields
set output to do shell script "echo exiftool -t " & DesiredEXIFData & " " & (quoted form of m_imagePath) & ">/tmp/exif"
set output to do shell script "exiftool -t " & DesiredEXIFData & " " & (quoted form of m_imagePath) & ">>/tmp/exif"
set output to do shell script "exiftool -t " & DesiredEXIFData & " " & (quoted form of m_imagePath)

Contents of /tmp/exif:
exiftool -t -LensID -MeteringMode -Flash -ExposureMode -ExposureProgram -LightSource RAW/Landschaft/Berlin/bei Nacht/PICT6478.MRW

Correct path to the image file:
/Volumes/Vielfrass/RAW/Landschaft/Berlin/bei Nacht/PICT6478.MRW

Hope that helps to make the script better :-)


Flo from Berlin

At Monday, 11 June, 2007 , Blogger Allan said...

Hi Flo:

I did an experiment. When I put a space in the Aperture project name, I get that error.

The error goes away when I replace the "exiftool line" with:

set output to do shell script "exiftool -t " & DesiredEXIFData & space & quoted form of m_imagePath

Not sure why getting rid of the parentheses works, it seems to. Please give it a shot.

Send me an email: allan at hise dot org to follow up.

At Sunday, 05 August, 2007 , Anonymous Anonymous said...

look here for an applescript to get the full file path of an selected Aperture image: http://homepage.mac.com/brettgross/Aperture/Aperture/DatabaseAccess.html

At Sunday, 05 August, 2007 , Blogger Allan said...

Thanks anonymous, However, I have already added that to my script. I should have it online for download soon...


Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home