Difficulty translating between POSIX and non-POSIX file system paths

Hi everyone,

I have written a script that will help me export all of my photos from iPhoto, multiple albums at a time. I’ve already written the parts that allow the user to select the album(s) to export, and to select that album and iterate through all photos in the album. I’m stuck on the very last line of the script where I’m attempting to copy the photo image to a target folder.

The culprit appears to be this line. I’ve tried many variations:

tell application "Finder" to duplicate POSIX file source_Image_Path to export_Folder & ":" & (a_Photo's name) & ".jpg"

The error I receive is as follows:

error "Finder got an error: Can't set \"Macintosh HD:Users:eh:Desktop:iPhoto Export:2009-09-28 Dumbarton Bridge:IMG_1656.JPG.jpg\" to POSIX file \"/Users/eh/Pictures/iPhoto Library/Originals/2009/Dumbarton Bridge/IMG_1656.JPG\"." number -10006 from "Macintosh HD:Users:eh:Desktop:iPhoto Export:2009-09-28 Dumbarton Bridge:IMG_1656.JPG.jpg"

The challenge seems to be that the way I’ve defined “export_Folder” and the way that the iPhoto Album’s Photos collection handle paths are different, and I don’t know how to make them the same so that the Finder’s “duplicate” command doesn’t gag on the inputs.

Here is how I have defined export_Folder:

set export_Folder to "Macintosh HD:Users:eh:Desktop:iPhoto Export:" & selected_Album

Any help with this path issue would be a great help, thanks in advance!

For reference, here is the entire script. If you want to run it on your own system, you’ll have to change the hard-coded path to the Desktop where export_Folder is defined, and you’ll have to have at least one Album in iPhoto.

Export Albums v0.01 - Eric Heitzman

tell application "iPhoto"
	-- For convenience, you can hardcode the name of some iPhoto albums (useful during development)
	-- set iPhoto_All_Albums_List to every album
	-- set selected_Albums to {"2009-10-19 Australia Sydney Bondi, Cape Tribulation", "2009-10-17 Snorkeling Cape Tribulation"}
	
	set iPhoto_All_Albums_List to every album
	set iPhoto_All_Album_Names_List to {}
	
	-- Create a variable called iPhoto_All_Albums_List containing the names of all albums with type "regular album" 
	repeat with iPhoto_Album in the iPhoto_All_Albums_List
		if ((iPhoto_Album's type as text) contains "regular album") then
			set end of iPhoto_All_Album_Names_List to iPhoto_Album's name
		end if
	end repeat
	
	-- Allow the user to select with of the "regular album" entries to export
	set selected_Albums to choose from list iPhoto_All_Album_Names_List with title "iPhoto Album Exporter" with prompt "Select which album(s) you want to export." with multiple selections allowed
	
	-- debug output: display the names of the selected output (all run together)
	-- display dialog "You selected: " & selected_Albums
	
	repeat with selected_Album in the selected_Albums
		repeat with iPhoto_Album in the iPhoto_All_Albums_List
			if ((iPhoto_Album's name) contains (selected_Album as text)) then
				-- debug output: display the name each selected album
				-- display dialog (iPhoto_Album's name as text) giving up after 1
				
				-- Create a new folder for each Album to be exported.  This works as long as there isn't a collision.
				tell application "Finder" to make new folder at alias "Macintosh HD:Users:eh:Desktop:iPhoto Export:" with properties {name:selected_Album}
				set export_Folder to "Macintosh HD:Users:eh:Desktop:iPhoto Export:" & selected_Album
				
				-- Duplicate each photo file in the iPhoto_Album to the export_Folder
				set all_Photos_List to photos of iPhoto_Album
				repeat with a_Photo in the all_Photos_List
					-- debug output: display the name and path of each photo in the album
					-- display dialog (a_Photo's image path as text) giving up after 1
					
					set source_Image_Path to (a_Photo's image path)
					tell application "Finder" to duplicate POSIX file source_Image_Path to export_Folder & ":" & (a_Photo's name) & ".jpg"
				end repeat
			end if
		end repeat
	end repeat
end tell

Hi,

do the coercion before the Finder line


.
	set source_Image_Path to POSIX file (a_Photo's image path) as alias
					tell application "Finder" to duplicate source_Image_Path to export_Folder & ":" & (a_Photo's name) & ".jpg"
.

btw: to retrive the album name list, this is sufficient


.
set iPhoto_All_Album_Names_List to name of albums whose type is regular album
.

Hey Stefan, thank you for the prompt reply.

That seems to get me past one error and into another one. Here’s the modified code I’m now using:

set source_Image_Path to POSIX file (a_Photo's image path) as alias
tell application "Finder" to duplicate source_Image_Path to export_Folder & ":" & (a_Photo's name)/applescript]

And here is the error that is being returned:

error “Finder got an error: Can’t make "Macintosh HD:Users:eh:Desktop:iPhoto Export:2009-10-06 Melbourne:IMG_1744.JPG.jpg" into type folder.” number -1700 from “Macintosh HD:Users:eh:Desktop:iPhoto Export:2009-10-06 Melbourne:IMG_1744.JPG” to folder



I wondered if perhaps the "duplicate" function was only seeing "export_Folder" and somehow ignoring the string concatenation where I build the full file path, so I tried splitting it into two lines, but got the exact same error.

set source_Image_Path to POSIX file (a_Photo’s image path) as alias
set export_Filename to export_Folder & “:” & (a_Photo’s name)
display dialog ("source_Image_Path is: " & source_Image_Path)
display dialog ("export_Filename is: " & export_Filename)
tell application “Finder” to duplicate source_Image_Path to export_Filename



The two "display dialog" commands produce exactly the output I would expect - they both point to real files.  Is it possible that the Finder "duplicate" function doesn't accept POSIX paths or something?  I'm struggling with why it would think that export_Filename is of type folder.

/eh

You don’t need to specify the name of the file at destination location


set source_Image_Path to POSIX file (a_Photo's image path) as alias
tell application "Finder" to duplicate source_Image_Path to folder export_Folder

Awesome, that was it! I should have read the specification on duplicate more closely. Thank you very much!

In case anyone wants to export their photos from iPhoto, the corrected script is posted below. It has a few known bugs though:

Known Bugs

  1. You have to change the hard-coded export_Folder path to something that makes sense on your system.
  2. If you try to export the same album more than once, it will fail because it will try to make another folder in the same location and there will be a name collision.
  3. I’m not sure how it would handle movie files inside your iPhoto Album.
  4. It does not capture “keywords.”

Here is the script. Despite those limitations, it is still useful for exporting albums in bulk and correctly pulling images from “Original” and “Modified” folders.

tell application "iPhoto"
	set iPhoto_All_Albums_List to every album
	set iPhoto_All_Album_Names_List to name of albums whose type is regular album
	
	-- Allow the user to select with of the "regular album" entries to export
	set selected_Albums to choose from list iPhoto_All_Album_Names_List with title "iPhoto Album Exporter" with prompt "Select which album(s) you want to export." with multiple selections allowed
	
	-- debug output: display the names of the selected output (all run together)
	-- display dialog "You selected: " & selected_Albums
	
	repeat with selected_Album in the selected_Albums
		repeat with iPhoto_Album in the iPhoto_All_Albums_List
			if ((iPhoto_Album's name) contains (selected_Album as text)) then
				-- debug output: display the name each selected album
				-- display dialog (iPhoto_Album's name as text) giving up after 1
				
				-- Create a new folder for each Album to be exported.  This works as long as there isn't a collision.
				tell application "Finder" to make new folder at alias "Macintosh HD:Users:eh:Desktop:iPhoto Export:" with properties {name:selected_Album}
				set export_Folder to "Macintosh HD:Users:eh:Desktop:iPhoto Export:" & selected_Album
				
				-- Duplicate each photo file in the iPhoto_Album to the export_Folder
				set all_Photos_List to photos of iPhoto_Album
				repeat with a_Photo in the all_Photos_List
					-- debug output: display the name and path of each photo in the album
					-- display dialog (a_Photo's image path as text) giving up after 1
					
					set source_Image_Path to POSIX file (a_Photo's image path) as alias
					set export_Filename to export_Folder & ":" & (a_Photo's name)
					tell application "Finder" to duplicate source_Image_Path to folder export_Folder
					export_Filename
				end repeat
			end if
		end repeat
	end repeat
end tell