Status
D&D just got Facebook'd - http://bit.ly/tinyadventures
Location
Arlington, VA
Subscribe to GeoRSS Subscribe to KML


Writing iPhoto Exif Data

Published in Applescript, Photography, Programming  |  24 Comments


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.

Similar Posts


Responses

  1. Geir A says:

    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.)

  2. Bill says:

    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!

  3. High Earth Orbit » Blog Archive » Geotagging Flickr photos - the right way says:

    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. [...]

  4. Tom Kerswill says:

    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!

  5. Tom Kerswill says:

    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!

  6. Marty says:

    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 ####” ?

  7. Heather says:

    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”?

  8. Andrew says:

    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.

  9. Matias says:

    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!

  10. Ryan says:

    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

  11. Ryan says:

    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

  12. Chris says:

    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.

  13. Josh says:

    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.

  14. the loPEZ dispenser » Blog Archive » iPhoto IPTC/EXIF/XMP Tags says:

    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. [...]

  15. Robert says:

    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!

  16. Greg says:

    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!

  17. Comment on Writing iPhoto Exif Data by Robert says:

    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. [...]

  18. Ville says:

    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.

  19. dayna says:

    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…

  20. Ville says:

    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.)

  21. Ville says:

    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”

  22. dayna says:

    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*

  23. Roger Banks says:

    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.

  24. BaroqueW and his sidekick nikkitaa » iPhoto et tags EXIF says:

    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, [...]

Leave a Response