Export selected iPhoto file to external folder

EDIT EDIT - Issue resolved, see my final post for the details and a copy of the script.

EDIT - I have updated this thread with more info.

I hope I am not being to much of a pest with these questions! Further to my earlier queries I have realised after discussions with the client, the situation is more complex than first realised.

Essentially the files are images of artwork, they need to be exported in full resolution to one location and in the reduced (750 pixels) resolution to another location. But each image needs to be saved to a folder with the artists name within the artwork folder.

So the structure is something like this, an image 1234Y.jpg of an artwork by the artist, Gulumbu Yunipingu needs to be saved at full resolution in ~/Artwork_Originals/Gulumbu and the reduced resolution version in ~/Filemaker_Images/Gulumbu

The ‘Gulumbu’ folders will contain all the images of her artwork, there are several hundred of these artist folders within the main folders.

The image titles are actually the catalogue numbers of the artworks, ie 1234Y.jpg - it would be possible to use the artists name, and therefore the folder name as a keyword for the images, if that helps to export them to the correct folders.

I really appreciate your help with these issues and any suggestions you can give for solving them.

It is also feasible to use other applications to export the images if it works better, so Photoshop for example.

Cheers, Rick
Original post

[i]Hi All,

I am looking for some expert help in creating a script.

I have been trying to understand how to write a simple export script to send selected photos to a folder.

I need to do this to allow the images to be accessed by FileMaker for a database. The folder will be created on a server and various workstations will need to run the script to allow transfer of the images to the folder.

In very simple terms the process will be, import photos from camera to iPhoto, edit if necessary in PhotoShop, then the selected image,

1 File Export…
2. Set export to Kind “JPEG”, JPEG Quality “Maximum”, Include “title & keywords”, Size “Custom”, Max Dimension of “750” px, Filename use “title”
3. Export to /folder

Thanks in Anticipation![/i]

Hi,

this exports selected images to a folder. If an album is selected, all photos of this album will be exported


property destinationFolder : "/Volumes/myVolume/path/to/folder/" -- the trailing slash is required

tell application "iPhoto"
	set sel to selection
	if class of item 1 of sel is album then
		set sel to photos of item 1 of sel
	end if
end tell
repeat with onePhoto in sel
	tell application "iPhoto" to set {pLocation, pName} to {image path, image filename} of onePhoto
	set destinationPath to quoted form of (destinationFolder & pName)
	do shell script "/bin/cp " & quoted form of pLocation & space & destinationPath
end repeat

Hi Stephan,

Thanks for the script, that certainly works. The only issue I have is that it exports it at the full size of the original image, as I posted in my query, I was hoping to export at the longest edge, 750 pixels.

I can set the export settings manually, but was hoping to automate the process,

Cheers, Rick

EDIT - Also, I want the destination folder to reside on the server, is there any trick to defining the folder destination in terms of specifying a share like a server folder?

The export settings of iPhoto aren’t scriptable.
This is a workaround using the shell command sips to scale and convert the files


property maxPixel : 750
property destinationFolder : "/Volumes/myVolume/path/to/folder/" -- the trailing slash is required

tell application "iPhoto"
	set sel to selection
	if class of item 1 of sel is album then
		set sel to photos of item 1 of sel
	end if
end tell
repeat with onePhoto in sel
	tell application "iPhoto" to set {pLocation, pName} to {image path, title} of onePhoto
	do shell script "sips -Z 750 -i -s format jpeg -s formatOptions best " & quoted form of pLocation & " --out " & quoted form of (destinationFolder & pName & ".jpg")
end repeat

but it might be better to use Photoshop

Thanks again, Stephan. I am certainly very appreciative of your prompt responses to my queries.

I managed to sort out addressing the shared drives issue, I will try your script for the sizing.

Cheers, Rick

I have updated my original post to reflect changed ifo on the issue. Stefan has been a great help with PM’s on the subject but I thought it was worth posting the issues for a wider audience.

cheers.

Ok, with Stefan’s help I have got the fullsize and resized versions of the image files exporting to discrete folders, the next step is to be able to export to chosen sub folders of the discrete folders as per the original OP.

Is there a way to get the script to ask the user to select an existing subfolder from the folder that it is directed to?

eg, file 1234Y.jpg needs to be exported to both the “fullsize” folder as an original version and the “filemaker” folder as a resized version, the script already allows this, but i want to be able to choose to put it in a specific sub-folder of each of those folders, so if it was an image of a work by Gulumbu Yunipingu, i could choose to export it to /fullsize/gulumbu and /filemaker/gulumbu.

If I was to arrange all of the images pertaining to a particular artist in an iPhoto Album, is there a way to pick up the album name and use that to export to a folder of the same name?

this script asks for a base folder, creates the subfolders filemaker and fullsize if necessary
and exports and distributes the files.
Of course it’s also possible to take the name of an album


property maxPixel : 750

set baseFolder to POSIX path of (choose folder with prompt "choose base folder")

tell application "iPhoto"
	set sel to selection
	if class of item 1 of sel is album then
		set sel to photos of item 1 of sel
	end if
end tell
repeat with onePhoto in sel
	tell application "iPhoto" to set {pLocation, pName} to {image path, title} of onePhoto
	do shell script "/bin/mkdir -p " & quoted form of (baseFolder & "filemaker")
	do shell script "/usr/bin/ditto " & quoted form of pLocation & space & quoted form of (baseFolder & "fullsize/" & pName)
	do shell script "sips -Z 750 -i -s format jpeg -s formatOptions best " & quoted form of pLocation & " --out " & quoted form of (baseFolder & "filemaker/" & pName & ".jpg")
end repeat

Thanks Stefan,

I havent made myself clear, the folders ‘Fullsize’ and "Filemaker’ exist already, they also have sub-folders of all the artist names, each sub-folder holding the actual artwork image files for the particular artist.

What I need to script is for the image of an artwork to be exported to the appropriate sub-folder in ‘Fullsize’ and ‘Filemaker’, these sub-folders will both be the artists name.

eg /Fullsize/Gulumbu/ and /Filemaker/Gulumbu/

The folders are populated already with works from the last 10 years, so there are hundreds of sub-folders already containing thousands of images.

All of this was done in an intensely manual process, which I am trying to automate.

If I can get the script to present some form of dialogue box to select the artist sub-folders then that will work within the existing script I have for export.

If not, the best solution may be to set up albums with the Artists names in iPhoto and when exporting reference the album name to create the paths for the exports. So in effect file 1234Y.jpg exports in 2 versions, one to /Fullsize/“Album Name”/1234Y.jpg and the other to /Filemaker/“Album Name”/1234Y.jpg

I guess the final consideration would be that if it is a new artist then it would be desirable to have the script create a new sub-folder with the artist’s name - which would be the album name if I used albums to generate the path.

this takes the name of the album, you have to specify the parent filemaker and fullsize folders


property maxPixel : 750

property fullSizeFolder : "/Volumes/myVolume/path/to/fullSizeFolder/" -- the trailing slash is required
property filemakerFolder : "/Volumes/myVolume/path/to/filemakerFolder/" -- the trailing slash is required

tell application "iPhoto"
	set sel to selection
	if class of item 1 of sel is album then
		set sel to photos of item 1 of sel
	end if
	set currentAlbum to name of current album
end tell
repeat with onePhoto in sel
	tell application "iPhoto" to set {pLocation, pName} to {image path, title} of onePhoto
	do shell script "/bin/mkdir -p " & quoted form of (filemakerFolder & currentAlbum)
	do shell script "/usr/bin/ditto " & quoted form of pLocation & space & quoted form of (fullSizeFolder & currentAlbum & "/" & pName)
	do shell script "sips -Z 750 -i -s format jpeg -s formatOptions best " & quoted form of pLocation & " --out " & quoted form of (filemakerFolder & currentAlbum & "/" & pName & ".jpg")
end repeat

Stefan,

You are a total legend - that script does everything I need. I certainly owe you one for that - not sure how I can repay you but I am a very happy man now!

Thanks a million,

Cheers, Rick

Ok, with more help from the very generous Stefan I have been able to create a script that does everything that I wanted, in the most efficient way possible.

I thought I should post a copy of the finished product for anyone that may be interested.

--Checks for shared disk and mounts if not already--

set mountedDisks to list disks

if mountedDisks does not contain "VolName" then
	mount volume "afp://User:password@192.168.x.x/VolName/"
end if

property maxPixel : 750 --note--

property fullSizeFolder : "Volumes/path/folder/" -- the trailing slash is required, insert path here for reference by script--
property filemakerFolder : "Volumes/path/Folder/" -- the trailing slash is required, insert path here for reference by script--

--script checks whether selection is an album or photo, goes to first photo, sets the location and name of image, finds 1st keyword for image, warns if there is no keyword--

tell application "iPhoto"
	set sel to selection
	if class of item 1 of sel is album then
		set sel to photos of item 1 of sel
	end if
end tell
repeat with onePhoto in sel
	tell application "iPhoto"
		set {pLocation, pName} to {image path, title} of onePhoto
		try
			set pKeyword to name of keyword 1 of onePhoto
		on error
			set msg to "no keyword!"
			display dialog msg
		end try
	end tell
	
	--exports full size and resized versions to respective destinations, adds to existing folder if already created, otherwise adds keyword as a sub-folder to destination folder. If an album was selected script will export all selected images to folders based on Keywords. --
	
	do shell script "/bin/mkdir -p " & quoted form of (filemakerFolder & pKeyword)
	do shell script "/usr/bin/ditto " & quoted form of pLocation & space & quoted form of (fullSizeFolder & pKeyword & "/" & pName & ".jpg")
	do shell script "sips -Z 750 -i -s format jpeg -s formatOptions best " & quoted form of pLocation & " --out " & quoted form of (filemakerFolder & pKeyword & "/" & pName & ".jpg")
end repeat

It is thanks to this fantastic forum and the help of knowledgeable and helpful members such as Stefan that I have been able to sort this out and along the way learn a little bit about Applescript.

Hi,

I have moved this script to another workstation and it is working fine except for the final step. For some reason I am getting an error with the --out command in sips,

error “Error 10: out_dir_not_found /Users/museum2/ArtistPicFolders/Gadalminy Munyarryun/BL97Gadalminy.jpg\rTry ‘sips --help’ for help using this tool” number 10

The script is creating the folder as it should, ie “/Users/museum2/ArtistPicFolders/Gadalminy Munyarryun/” but its not adding the resized image to the folder.

The full size image is being copied to its folder correctly.

The error message says that the path to the folder represented by the variable filemakerFolder is not valid

Thanks Stefan, thats how i read it to, thing is the path to the folder is correct, i have tried creating a new folder in a different location and it still gives the same error.

I also set it up on a 3rd computer and it worked fine again.

Seems its something specific to this computer, I have restarted, repaired system prefs and any other things i could think of - all with no success!

Edit - the path must be correct as it creates the folder in the earlier step, its only when the sips --out command tries to send the actual image to the created folder that the directory error comes up - which is not very logical!

Since StefanK helped me develop this script its been working well, this week the clients requested a change - they wanted the resized images to be a specific size (around 1200KB) rather than using the 750px to resize.

After much fiddling and learning I installed ImageMagick and got it to work,

property fullSizeFolder : "/Volumes/stockroom/ARTWORK_DATABASE/Artwork/" -- the trailing slash is required, insert path here for reference by script--
property resizedFolder : "/Users/stockroom/ARTWORK_DATABASE/Artist/" -- the trailing slash is required, insert path here for reference by script--

--script checks whether selection is an album or photo, goes to first photo, sets the location and name of image, finds 1st keyword for image, warns if there is no keyword--

tell application "iPhoto"
	set sel to «class selc»
	if class of item 1 of sel is «class ipal» then
		set sel to every «class ipmr» of item 1 of sel
	end if
end tell
repeat with onePhoto in sel
	tell application "iPhoto"
		set {pLocation, pName} to {«class ipth», «class titl»} of onePhoto
		try
			set pKeyword to name of «class ikwd» 1 of onePhoto
		on error
			set msg to "no keyword!"
			display dialog msg
			
		end try
	end tell
	
	--exports full size and resized versions to respective destinations, adds to existing folder if already created, otherwise adds keyword as a sub-folder to destination folder. If an album was selected script will export all selected images to folders based on Keywords. --
	
	do shell script "/bin/mkdir  -p " & quoted form of (resizedFolder & pKeyword)
	do shell script "/usr/bin/ditto " & quoted form of pLocation & space & quoted form of (fullSizeFolder & pKeyword & "/" & pName & ".jpg")
	--do shell script "sips -Z 700 -i -s format jpeg -s formatOptions best " & quoted form of pLocation & " --out " & quoted form of (resizedFolder & pKeyword & "/" & pName & ".jpg")--
	do shell script "/usr/bin/ditto " & quoted form of pLocation & space & quoted form of (resizedFolder & pKeyword & "/" & pName & ".jpg")
	do shell script "/usr/local/bin/mogrify -define jpeg:extent=1200KB " & quoted form of (resizedFolder & pKeyword & "/" & pName & ".jpg")
end repeat

One other occasional issue I have is that the script will stop working, oddly enough I can get it to work by renaming the Volume as stockroom-1

That works for a while and then it stops working again and I have to rename it back to stockroom to get the script to work. Not sure why this happens and whether I could change something to prevent it?