iView to iPhoto Script

iPhoto is a great and useful application, but iView MediaPro is better at managing very large numbers of images. As I'm writing this I have about 25,000 photos managed in iView. During our 2004 RV trip I began using iView, but missed some of the easy features of iPhoto - ordering those nice photo books, posting to .Mac, and using the nifty myPhoto web serving utility.

This AppleScript takes the selected photos from within iView and copies them to a new album within iPhoto. It also copies the photo's caption into iPhoto's Comments field.

Place this script in /Users/yourname/Library/Application Support/iView/Plug-ins/Scripts/ .

UPDATE: iPhoto 6 changed its implementation of importing. I've made the necessary changebelow, but that change may cause this script to fail on iPhoto 5. This version of the script is tested with iView MediaPro 3.1 and iPhoto 6.0.3, and works fine. If you're using iPhoto 5 and the script fails, remove the "Repeat while (importing is true) ... end repeat" segment.

--script to copy an iView set into iPhoto
-- currently only gets selected media items; is there a way to get selected set name?
-- could fake it by checking which sets the photos belong to...
-- select every media item --if we can guarantee there is a set selected
-- Mitch Cohen mcohen -at- proactiveinteractive -dot- com
--

on run
set selectedItems to GetSelection()
set image_count to 0
set allFiles to {}
set allNames to {}
set allStrippedNames to {}
set allCaptions to {}

set theReply to display dialog "Name of new iPhoto album:" default answer ((time of the (current date)) as string)
set theNewAlbumName to text returned of theReply

repeat with theItem in selectedItems
tell application "iView MediaPro"
activate
set theName to the name of theItem
set thePath to the path of theItem as string
set theCaption to the caption of theItem as string
end tell

set allFiles to allFiles & (the POSIX path of (thePath as alias))
set allNames to allNames & theName
set allStrippedNames to allStrippedNames & stripExtension(theName)
set allCaptions to allCaptions & theCaption

set image_count to image_count + 1
set this_file to (thePath)
--ok, got the stuff from iView, now send it to iPhoto!

end repeat
tell application "iPhoto"
activate
set the view to organize
--set theNewAlbumName to ((time of the (current date)) as string)
--do to: check for album already existing, then what?
new album name theNewAlbumName
select album theNewAlbumName
--import from the POSIX path of (this_file as alias) --if just one file
import from allFiles to album theNewAlbumName
repeat while (importing is true) --Possibly remove this with iPhoto 5
--do nothing, just wait for importing to complete
end repeat --Possibly remove this with iPhoto 5
set the view to organize
--select photo library album
select album theNewAlbumName
set the photo_list to photos of album theNewAlbumName
--still need to figure out the set/album creation thing
select ""
repeat with i from 1 to the count of the photo_list
set theCurrentPhotoID to item i of photo_list
set theCurrentiPhotoName to name of theCurrentPhotoID
if theCurrentiPhotoName is in allStrippedNames then
repeat with x from 1 to the count of allNames --is there a way to see where it is?
if theCurrentiPhotoName = item x of allStrippedNames then
if item x of allCaptions ≠ "" then set the comment of theCurrentPhotoID to item x of allCaptions
end if
end repeat
end if
end repeat

end tell
say "done!"
end run

-----------------
-- function to strip out the extension from a filename

on stripExtension(theFileName)
set theFileName to theFileName
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"."}
set something to last text item of theFileName
set AppleScript's text item delimiters to oldDelims
set theLength to (the length of something) + 1
set theLeft to ((the length of theFileName) - theLength)
set theNameWithoutExtension to text 1 through theLeft of theFileName
return theNameWithoutExtension
end stripExtension

-- get the selected media items in an array ---------------------------------------------
-- this function swiped from one of the iView sample scripts
on GetSelection()
set selectedItems to {}
tell application "iView MediaPro"
if catalog 1 exists then set selectedItems to the selection of catalog 1
end tell
if number of items in selectedItems is 0 then
display dialog ¬
"You need to select at least one media item in the front catalog in order to use this script." buttons {"OK"} default button ¬
"OK" --with icon NOTE --giving up after 10 --don't know why this fails now
error number -128
end if
return selectedItems
end GetSelection