Media Browser - Can it only be called using Automator

I recently started playing with Applescript as it seemed to offer more flexibiliy than Automator.

My partner is still getting used to using a mac so I’m trying to make his life easier with some scripts. My first script was a folder action that copies photos from a folder into a specific iPhoto album, then deletes them from the original folder and closes iPhoto. It took a while but so far so good. The next script I need is to select and resize photos for emailing via hotmail.

It was easy enough to set up an Automator action that fires up Media Browser to select the photos then resize and copy them to a folder on the desktop but this is not as flexible as I’d like. I decided to try it using Applescript but can’t find any references on how, if at all, you call the Media Browser. Is it possible?

Hi,

why do you want an extra script to resize and email photos?
You can do this directly in iPhoto without any temporary folder

You can only partially do it in iPhoto. To email the photos using a webmail client, you need to copy them from iPhoto because you cannot browse the photo library unless you’re using Apple Mail. I want to create a script that allows me to select the photos, resize them and copy them to a desktop folder ready to be attached into an email. If it isn’t simple, my partner will still be complaining about it in 2020!

actually you can do this with the export function in iPhoto, but try this.
Adjust the properties, select the photos in iPhoto and run the script


property newSize : 640
property destinationFolderName : "resized_photos:"

tell application "iPhoto" to set sel to selection
set destinationFolder to ((path to desktop as Unicode text) & destinationFolderName)
do shell script "/bin/mkdir -p " & quoted form of POSIX path of destinationFolder

launch application "Image Events"
repeat with i in sel
	tell application "iPhoto" to tell i to set {_name, _location} to {image filename, image path}
	tell application "Image Events"
		open POSIX file _location as alias
		tell image 1
			scale it to size newSize
			save it in (destinationFolder & _name) with icon
			close
		end tell
	end tell
end repeat


Thanks for that. I tried it but get the error

with the code {image filename, image path}highlighted.

Any suggestions? Really appreciate the help.

You’ve probably selected a whole album, this version of the script considers single photos and albums


property newSize : 640
property destinationFolder : ""
property destinationFolderName : "resized_photos:"

set destinationFolder to ((path to desktop as Unicode text) & destinationFolderName)
do shell script "/bin/mkdir -p " & quoted form of POSIX path of destinationFolder

tell application "iPhoto" to set theSelection to selection
set selclass to class of item 1 of theSelection as text
if selclass is "album" then
	repeat with oneAlbum in theSelection
		tell application "iPhoto" to set PhotoList to photos of oneAlbum
		exportPhotos(PhotoList)
	end repeat
else
	exportPhotos(theSelection)
end if

on exportPhotos(sel)
	launch application "Image Events"
	repeat with i in sel
		tell application "iPhoto" to tell i to set {_name, _location} to {image filename, image path}
		tell application "Image Events"
			open POSIX file _location as alias
			tell image 1
				scale it to size newSize
				save it in (destinationFolder & _name) with icon
				close
			end tell
		end tell
	end repeat
end exportPhotos

Thanks Stefan, that works perfectly.

I am now trying to idiot proof it for my partner so there were two additional things I wanted incorporated

  1. The script relies on the user launching iPhoto and making a selection first. I tried to open iPhoto as the first part of the script and then popup a dialog asking the user to make a selection but
    if the display dialog command is inside the tell statement, the popup has to be closed before making a selection
    if the display dialog command is outside the tell statement, the popup goes behind the iPhoto window
    so it doesn’t really work - Any suggestions?

  2. I wanted the user to be able to select a range of sizes for the photos. This bit worked without any problems.

I know this seems an odd way to do it but you haven’t met my partner!

-- get selection
(*tell application "iPhoto"
	activate
	display dialog "Select the photos you want to resize and copy to the desktop then click OK"
end tell*)

-- declare variables
property newSize : ""
property destinationFolder : ""
property destinationFolderName : "test:"

-- select size for photos
set selectSize to {"Thumbnail (150px)", "Email (640px)", "Desktop (1024px)"}
choose from list selectSize with prompt "Choose new size for photos"

-- define size for selected option
if item 1 of result = "Thumbnail (150px)" then
	set newSize to 150
else if item 1 of result = "Email (640px)" then
	set newSize to 640
else if item 1 of result = "Desktop (1024px)" then
	set newSize to 1024
end if

-- define path for saving resized photos
set destinationFolder to ((path to desktop as Unicode text) & destinationFolderName)
do shell script "/bin/mkdir -p " & quoted form of POSIX path of destinationFolder

tell application "iPhoto" to set theSelection to selection
set selclass to class of item 1 of theSelection as text
if selclass is "album" then
	repeat with oneAlbum in theSelection
		tell application "iPhoto" to set PhotoList to photos of oneAlbum
		exportPhotos(PhotoList)
	end repeat
else
	exportPhotos(theSelection)
end if

-- export photos
on exportPhotos(sel)
	launch application "Image Events"
	repeat with i in sel
		tell application "iPhoto" to tell i to set {_name, _location} to {image filename, image path}
		tell application "Image Events"
			open POSIX file _location as alias
			tell image 1
				scale it to size newSize
				save it in (destinationFolder & _name) with icon
				close
			end tell
		end tell
	end repeat
	tell application "iPhoto"
		quit
	end tell
end exportPhotos

Thanks again for your help with this.