Exporting Address Book images to files

I’m trying to export my adressbook images to a folder. I want them to be jpeg images, so that i can load them onto my Sonyericsson k750i (wich doesn’t support image syncing with contacts).

I manage to export them to a dir as TIFF images using:


tell application "Address Book"
	set PathName to "Macintosh HD:img:"
	
	set npersons to count of people
	repeat with i from 1 to npersons
		
		if ((image of person i) exists) then
			set current_image to (image of person i)
			set personName to (name of person i) as text
			set target_file to (PathName & personName & ".tiff") as Unicode text
			
			try
				set f to open for access file target_file with write permission
				try
					set eof of f to 0
				end try
				write current_image to f
				close access f
			on error
				try
					close access f
				end try
			end try
		end if
		
	end repeat
end tell

Does anyone know how i save as a JPEG file instead? The image stored in address book is a TIFF image… Can i use “Image Events” somehow? I’ve tried the following:


tell application "Address Book"
	set PathName to "Macintosh HD:img:"
	
	set npersons to count of people
	repeat with i from 1 to npersons
		
		if ((image of person i) exists) then
			set current_image to (image of person i)
			set personName to (name of person i) as text
			set target_file to (PathName & personName & ".jpeg") as Unicode text
			
			tell application "Image Events"
				save current_image in target_file as JPEG
			end tell
			
		end if
		
	end repeat
end tell

but it doesn’t work, can anyone help me in the right direction?

Thanks, this works fine!
Is there no way of converting the data before you save? So that two disc-acesses are avoided?
Not a problem really, it works, just woundering…

Since Image Events expects to work with a file, I don’t think so, thid.

From Image Events’ AppleScript dictionary:

I was about to post my own suggestion for this. As you can see, apart from one or two suggested precautions, it’s very similar to Jacques’ solution:

to get_path to folder_name at main_path
	tell application "Finder" to tell folder main_path
		tell folder folder_name to if exists then return it as Unicode text
		(make new folder at it with properties {name:folder_name}) as Unicode text
	end tell
end get_path

to strip_chars of charList from the_text
	set tid to text item delimiters
	considering case
		repeat with strip_char in charList
			if strip_char is in the_text then
				set text item delimiters to strip_char
				set the_text to the_text's text items
				set text item delimiters to ""
				tell the_text to set the_text to beginning & ({""} & rest)
			end if
		end repeat
	end considering
	set text item delimiters to tid
	the_text
end strip_chars

to make_file from the_data at file_path
	set open_file to open for access file file_path with write permission
	set eof open_file to 0
	write the_data to open_file
	close access open_file
end make_file

to convert_image_to_JPEG at file_path
	tell application "Image Events" to tell (open file_path)
		save as JPEG in file_path
		close
	end tell
end convert_image_to_JPEG

to make_jpg from image_data for this_person at folder_path
	set file_path to folder_path & (strip_chars of {".", ":"} from this_person) & ".jpg"
	make_file from image_data at file_path
	convert_image_to_JPEG at file_path
end make_jpg

set folder_path to get_path to "img" at path to startup disk
tell application "Image Events" to close images
tell application "Address Book" to repeat with this_person in people
	tell this_person to if image exists then my (make_jpg from image for name at folder_path)
end repeat