Writing iPhoto Exif Data
Here is some quick Applescript that uses ExifTool to write Copyright, Title, Comments, and Keyword exif data to selected photos in iPhoto:
This script is released under the Creative Commons.
-- This applescript will set the exif keywords, name,
-- and comments of all selected iPhoto images using
-- the information current in iPhoto.
--
-- Author: Andrew Turner (http://highearthorbit.com)
--
property copyright : ¬
"Copyright Andrew Turner, 2005. All Rights Reserved."
property URL : "http://highearthorbit.com"
property exifToolOriginal : "_original"
-- True retains copyright, False means Public Domain
property Copyrighted : "True"
tell application "iPhoto"
activate
try
copy (my selected_images()) to these_images
if these_images is false or (the count of these_images) ¬
is 0 then ¬
error "Please select a single image."
repeat with i from 1 to the count of these_images
set the keywordslist to ""
set this_photo to item i of these_images
tell this_photo
set the image_file to the image path
set the image_title to the title
set the image_filename to the image filename
set the image_comment to the comment
set the assigned_keywords to the name of keywords
end tell
repeat with j from 1 to the count of assigned_keywords
set the keywordslist to keywordslist & " -keywords+=" ¬
& item j of assigned_keywords
end repeat
set output to do shell script ¬
"exiftool -title='" & image_title & ¬
"' " & keywordslist & ¬
" " & " -comment='" & image_comment & ¬
"' " & " -Copyright='" & copyright & ¬
"' " & " -CopyrightNotice='" & copyright & ¬
"' " & " -Rights='" & copyright & ¬
"' " & " -Marked='" & Copyrighted & ¬
"' " & "'" & image_file & "'"
do shell script "rm '" & image_file & "'" ¬
& exifToolOriginal
end repeat
display dialog "Exif writing complete."
on error error_message number error_number
if the error_number is not -128 then
display dialog error_message buttons {"Cancel"} ¬
default button 1
end if
end try
end tell
on selected_images()
tell application "iPhoto"
try
-- get selection
set these_items to the selection
-- check for single album selected
if the class of item 1 of these_items is album then error
-- return the list of selected photos
return these_items
on error
return false
end try
end tell
end selected_images
You can grab this script and a simpler Copyright only, as well as a Location (latitude/longitude/city,region,country) script here.
My name is
October 25th, 2005 at 12:45 pm (#)
A really useful script. I amended it to write “image_comment” to the EXIF fields “Description” and “Image Description” instead – this way it is compatible with the way Photoshop writes EXIF data. I also added the fields “Artist” and “URL” to include my name and website btw.
(Note that “URL” appears to be a reserved word; hence I had to rename the variable at the start to “site” to get it to run properly.)
January 7th, 2006 at 5:17 am (#)
Hi
Thanks for the script ideas. I’ve got exiftool installed and got a coup0le of my own scripts to write the EXIF data.
I’ve got a script that will update the iPhoto photo comment. It took me a while to figure out that the iPhoto comment wasn’t part of the EXIF or IPTC metadata (silly way to do it if you ask me).
Anyways, that script works fine. The comment gets updated immediately in iPhoto.
Another script I have using exiftool updates the EXIF maker, model, usercomment, and artist tags. However, none of these changes show up in iPhoto!
Finding the file in the Finder and checking the EXIF content reveals all the stuff my script added. So the script works. But how do I get it so the new EXIF data shows up in iPhoto?
If I export the photo, the EXIF data is there. If I then import that exported photo, iPhoto even refers to it as a duplicate! This “duplicate” does show up with the correct EXIF info in iPhoto now.
So exporting ALL the changed photos and then re-importing them would work but that’s obviously a last resort.
Do you know of a way to “reset” a photo so iPhoto will refresh the EXIF metadata and display it?
I’ve tried the options of rebuilding the iPhoto library with option-command at app startup but no luck.
I’ve seen your other post about “iPhoto was often beligerent and required a restart of the iPhoto (and possible database recreation) to read the location information (which was viewable in the “info” panel).”
So what did you actually do to get it to work?
I’m hoping a newly improved iPhoto gets released next week at MacWorld Expo!
Thanks for any suggestions and comments you can provide!
September 24th, 2006 at 4:00 pm (#)
[...] After I’ve either meshed up my coordinates, or have a list of locations, I fire up iView Media Pro, or iPhoto, and use my Applescript scripts in addition to ExifTool to actually write the GPS metadata. Because photo editing applications (like the aforementioned Photoshop) are usually very mean and don’t restore geo-metadata on edit and save, I suggest you edit all your photos first, and apply the geo Exif as the last step before uploading. [...]
January 7th, 2007 at 10:14 pm (#)
That’s a brilliant script – thanks. It’s particularly great with Keyword Assistant for iPhoto, as you can just go through and easily tag everything, then use this to synchronise. I’ve also got it setup with Quicksilver so it’s nice and quick to run the script. Only problem so far is that if you run the script on the same photo a few times, it appends the keywords, so you get multiple keywords that are the same on the photo. That’s probably something to do with the exiftool command line… just off to have a look at the man page now!
January 7th, 2007 at 10:16 pm (#)
Ah yeah, so to alter this behaviour in the script just change:
set the keywordslist to keywordslist & ” -keywords+=” & item j of assigned_keywords
to:
set the keywordslist to keywordslist & ” -keywords=” & item j of assigned_keywords
This will just clear the keywords before setting the new ones,
Thanks!
January 15th, 2007 at 4:13 pm (#)
This script doesn’t seem to work when the roll name for the photos has more than one space. Does it assume the roll/enclosing folder will always be named “Roll ####” ?
January 18th, 2007 at 8:31 pm (#)
Thanks for the script, it’s a great idea! Any idea why I might be getting an error along the lines of “sh: line 1: exiftool: command not found”?
January 19th, 2007 at 9:50 am (#)
@Tom – thanks for pointing out the fix
@Heather – you need to first install ExifTool onto your Mac.
@Marty – you just need to select the photos you want to geocode and shouldn’t depend on the name of the roll – I will have to test more to check this out.
April 12th, 2007 at 1:17 am (#)
The script works great, is just what I was looking for. But I have 2 error popups:
The first one is when I have a two words keyword like “Los Angeles”. I get this popup:
Error: Error opening file – Angeles
And the second is this popup:
Error: [minor] Bad NikonPreview directory pointer for tag 0×2 – Users/Saraza/Pictures/2004/040928-11.JPG
I couldn’ t understand why this one appears.
Thanks!
May 11th, 2007 at 3:55 pm (#)
This looks very very useful. I have been looking for a way to take the iPhoto date and somehow embed/attach it when exporting to external web galleries that need this date inf). I have to manually enter the dates in iPhoto because the originals are scans, and thus have no EXIF data.
Does anyone know how one would modify this script to also copy the iPhoto date within an EXIF capture date header?
Cheers,
Ryan
May 13th, 2007 at 7:46 pm (#)
Well, this is to answer my question above. I now got a modified version of the script to simply add the iPhoto date as the Original Date in the EXIF header within my iPhoto library… which also applies to any exported versions, etc. (NOTE: it works, but I have no clue about applescript, so this may not be 100% correct, it’s just based on my trial-and-error).
Basically in the “tell this_photo” part I added:
set the image_date to the date
and then changed the exiftool shell script call to simply be:
“exiftool -DateTimeOriginal=’” & image_date & “‘ ” & “‘” & image_file & “‘”
Dunno if that’s useful to anyone, but this result saves me hours and hours of trouble in terms of getting my (properly dated) photos from iPhoto to Gallery 2.2 (http://gallery.menalto.com/)
Cheers!
Ryan
June 13th, 2007 at 2:08 am (#)
Thanks Ryan, that’s exactly what I was looking for.
Will give it a go later tonight, I have hundreds of scanned images which I need to export with the correct (iPhoto) date.
June 28th, 2007 at 12:12 am (#)
Thank you very much for this script.. Apple tricks people into thinking they are doing things “the right way”, when in reality it’s “Apple’s way” (iPhoto metadata as a prime example)
Just a mental note to anyone viewing this:
Think anyone could build in a versioning comment system that lets a commenter mark up (any) code, hide the revised full version behind css (or something) while showing the markups? (It would have to roll into a blog/new commenting system)
Would be great for a page like this, where there would have been at least 3 different versions so far, and encourage more.
August 7th, 2007 at 2:39 am (#)
[...] I was disappointed to find iPhoto didn’t “do” IPTC tags when I started using it to organise photos, and export them to Gallery2. Luckily I found an AppleScript that uses the excellent ExifTool. I’ve modified it a bit, to work better with Gallery 2, and fixed it so comments and tags are escaped properly. [...]
October 13th, 2007 at 1:03 am (#)
Great script! I’m using the most recent EXIFtool. Keywords do not seem to be written to the JPG. Does anyone experience similar problems and have a solution to this? Thx in advance!
November 19th, 2007 at 6:51 pm (#)
Does anyone have a solution to Matias’ problem when using two word keywords? For a two word keyword, I’d rather not have to use two separate tags “Los” and “Angeles”, but rather just “Los Angeles.”
Is there an easy way to modify the script so that two word keywords are functional?
Thanks so much for the awesome script!
December 5th, 2007 at 5:11 pm (#)
[...] life, Technology, Photography) Comment on Writing iPhoto Exif Data by Robert Great script! I’m using the most recent EXIFtool. Keywords do not seem to be written to the JPG. [...]
February 26th, 2008 at 9:26 am (#)
Regarding the multi-word keyword issue:
This can easily be fixed by modifying the script by adding single quotes around each keyword, as it is already done for the other tags (have a look at the code above).
I.e. change this part to:
repeat with j from 1 to the count of assigned_keywords
set the keywordslist to keywordslist & ” -keywords+=’” ¬
& item j of assigned_keywords & “‘”
end repeat
Note that it might be also more practical to change the += to =, as somebody already commented.
What could be also added is storing the rating information, I think it should be available via Applescript as well.
March 24th, 2008 at 4:58 pm (#)
just found this today.. am i right in thinking that any comment with an apostrophe will cause trouble? That’s what I’m seeing so far…
March 31st, 2008 at 6:24 am (#)
About the apostrophe problem:
Your guess is probably correct. Seems a more generic solution would be needed.
One idea would be to write the tag/content as tab-separated pairs to a temporary file and then let exiftool read that file.
That file could probably be also stdin, leading to something like this (note that this is a coarse example from memory without any syntax check):
do shell script “echo Keywords \t” item j of assigned_keywords | exiftool -t …
(Note the piping character. I hope you are a bit familiar with unix scripting.)
(BTW, regarding my previous comment about the rating info: for reading, that seems to be available only via the plist iPhoto archive file, for writing, UI scripting would be needed. Of course it is also not very difficult to generate rating-based tags like R0, R1, R2..R5.)
March 31st, 2008 at 7:15 am (#)
More on the apostrophe issue, this is a quick hack:
Of course, you can also quote the string with the parenthesis character, then it would probably look like this:
‘exiftool -title=”‘ & image_title & ¬
‘” ‘ & keywordslist…
That way, you can use the apostrophe within the title (but not the parenthesis).
The better solution (that will allow all characters) will probably need to be based on exiftool’s argfile option:
From the exiftool documentation:
“-@ ARGFILE
Read command-line arguments from the specified file. The file contains one argument per line (NOT one option per line — some options require additional arguments which must be placed on separate lines). Blank lines and lines beginning with # and are ignored. Normal shell processing of arguments is not performed, which among other things means that arguments should not be quoted. ARGFILE may exist relative to either the current directory or the exiftool directory unless an absolute pathname is given.
For example, the following ARGFILE will set the value of Copyright to “Copyright YYYY, Phil Harvey”, where “YYYY” is the year of CreateDate:
-d
%Y
-copyright<Copyright $createdate, Phil Harvey”
April 19th, 2008 at 7:17 pm (#)
thank you for replying.. i think i’m going to have to spend a bit more time on this (not played with programming much..)… or i’ll find a friend to help me. *grin*
June 2nd, 2008 at 5:08 pm (#)
exiftool has an -overwrite_original option that you could use instead of deleting the *._original files after the fact.
August 17th, 2008 at 7:58 am (#)
[...] et tags EXIF Posted in August 17th, 2008 by BaroqueW in Tech >> Computer Adapté d’un script par Andrew Turner, ce script Applescript permet d’ajouter des tags Exif (commentaires, titre, [...]
May 14th, 2009 at 1:51 pm (#)
After much playing around with this, and trying to get around the apostrophe (single-quote [']) issue, I figured that a small change needs to be made….
Need to replace this part of the script:
—————
set output to do shell script ¬
“exiftool -title=’” & image_title & ¬
“‘ ” & keywordslist & ¬
” ” & ” -comment=’” & image_comment & ¬
“‘ ” & ” -Copyright=’” & copyright & ¬
“‘ ” & ” -CopyrightNotice=’” & copyright & ¬
“‘ ” & ” -Rights=’” & copyright & ¬
“‘ ” & ” -Marked=’” & Copyrighted & ¬
“‘ ” & “‘” & image_file & “‘”
do shell script “rm ‘” & image_file & “‘” & exifToolOriginal
—————
With this:
—————
set output to do shell script ¬
“exiftool -title=\”" & image_title & ¬
“\” ” & keywordslist & ¬
” ” & ” -comment=’” & image_comment & ¬
“‘ ” & ” -Copyright=’” & copyright & ¬
“‘ ” & ” -CopyrightNotice=’” & copyright & ¬
“‘ ” & ” -Rights=’” & copyright & ¬
“‘ ” & ” -Marked=’” & Copyrighted & ¬
“‘ ” & “\”" & image_file & “\”"
do shell script “rm \”" & image_file & “\”" & exifToolOriginal
—————
The only changes are on the image_file and image_file strings. For all three occurrences, I had to replace the surrounding single quotes with a \”.
Now it works like a charm!!
July 2nd, 2009 at 12:29 pm (#)
[...] AppleScript that works for me is from Andrew Turner. I modified it as described in this comment to enable apostrophes in keywords. In addition, my [...]
October 25th, 2009 at 7:37 pm (#)
[...] is the repeat loop I am currently using (adapted from the script I found here http://highearthorbit.com/289/). How could I skip eventual errors? Please bear in mind that I have just begun exploring [...]
November 5th, 2009 at 2:45 pm (#)
Hello and thank you for this script
But there is a problem when there are pictures edit (/ iPhoto Library / Modified) original photos are not marked by the script.
Have you a solution to remedy this?
Thank you
December 27th, 2009 at 12:35 am (#)
First of all, thank you for this script: it’s nice, it issues a problem I care about, and it seems is the best thing available.
My main goal is to avoid loosing all the comments written in iPhoto if the files are exported.
I have the ‘09 version of iPhoto.
@iStar yes, it works on the “modified” file, not on the original. Though, when you export the photo, if you choose the right “kind” when exporting, and ask to include “title and keywords” and “location information”, the exif fields will be written in the exported file.
The problem seems to be that the scripts works fine, and is actually able to write the required fields in the exif info of the “modified” file, but all the exif info present in the original file are lost!
Now, I am trying to figure out how to “merge” all the exif fields, so to be able to export a file that has all the exif informations (i.e. all the fields), those from the original file (written by the camera) and those from iPhoto (e.g. the comments)
January 31st, 2010 at 8:49 am (#)
Please note: you’ll have to replace single – with a double -. Unfortunately this comment forum is a little too smart and automagically formats entries
January 31st, 2010 at 8:56 am (#)
This comment software is terrible for reformatting stuff.
You can fix the problems using a Find and Replace All.
You’ll have to replace the “smart” quotes with a regular quote and do the same for the smart ’single quotes’. When you’re replacing the single – with two real dashes make sure that you check that the subtraction signs for the date and time calculation are functional.
Happy iPhotoing
January 31st, 2010 at 9:01 am (#)
One final comment…
Make sure you replace the ‘opening’ smart single quote in the exiftool command with a regular single quote. Otherwise iPhoto will return EOF errors (which it in turn gets from exiftool).
February 6th, 2010 at 12:31 pm (#)
Hmm. My apologies. It seems like the script I tried to incorporate as a comment wasn’t accepted. Will try to figure out a way to post it here so that others can use it if they need to.
February 6th, 2010 at 12:32 pm (#)
Hmm #2. Never mind. It seems that it’s awaiting moderation and you can’t see it (#30). Maybe some day in the future it’ll be approved.