Import Installed Folder To iPhoto

Hi Guys,

This is my first post here, so go easy on me :slight_smile:

Anyways, I just finished creating an installer that basically just installs a folder of files onto the user’s desktop. However, afterwords, the company wants me to pop up a dialog asking if the user wants one of the folders containing all photos to be imported automatically into iPhoto. I have a script that was written to do just this, but for some reason it is failing in multiple spots. The first spot it fails is at the beginning, I will attach that code snippet and hopefully you can give me some pointers as to what I am doing wrong, or if there’s an easier way to accomplish this. Also, I am running the latest version of iPhoto just to let you know.

The error I get is (which is due to the first 5 lines of code I believe), “Installer got an error: “Installation is almost complete. Would you like the images added to your iPhoto Library as albums? This is recommended.” doesn’t understand the display dialog message.”

Here’s the code:


set workout_list to {"PumpOne for Men"} -- WORKOUTS_LIST
tell application "Installer"
	activate
	display dialog "Installation is almost complete. Would you like the images added to your iPhoto Library as albums? This is recommended." buttons {"No", "Yes"} default button 2
	if the button returned of the result is "Yes" then
		set i to 1
		try
			tell application "iPhoto" to quit
			tell application "System Events"
				repeat until not (process "iPhoto" exists)
					delay 1
				end repeat
			end tell
			repeat 5 times
				tell application "iPhoto" to activate
				tell application "System Events"
					if process "iPhoto" exists then
						exit repeat
					end if
				end tell
			end repeat
		on error
			display dialog "The installer cannot create iPhoto albums because iPhoto is not in the Applications folder. The image folders have been copied to the desktop." buttons {"Ok"} default button 1
			return -1
		end try
		set vLessSeven to "False"
		tell application "iPhoto"
			if (version as string) < "7" then --This section only executes if iPhoto has version less than 7
				set vLessSeven to "True"
				tell application "iPhoto" to select photo library album
				tell application "System Events"
					set frontmost of process "iPhoto" to true
					tell process "iPhoto" to set film_roll to the value of attribute "AXMenuItemMarkChar" of menu item "Film Rolls" of menu 1 of menu bar item "View" of menu bar 1
				end tell
			end if
		end tell
		repeat count (workout_list) times
			tell application "iPhoto"
				if not (album (item i of workout_list) exists) then
					new album name (item i of workout_list)
				end if
				select album (item i of workout_list)
				tell application "System Events" to tell process "iPhoto" to (click menu item "by Title" of menu 1 of menu item "Sort Photos" of menu 1 of menu bar item "View" of menu bar 1)
			end tell
			set i to i + 1
		end repeat
		set i to 1
		repeat count (workout_list) times
			if (my import_iphoto(item i of workout_list) is not 0) then
				repeat
					tell application "Installer"
						activate
						display dialog "An error occurred during import. Try importing " & (item i of workout_list) & " album again?" buttons {"No", "Yes"} default button 2
						if the button returned of the result is "No" then
							exit repeat
						else
							if (my import_iphoto(item i of workout_list) is 0) then
								exit repeat
							end if
						end if
					end tell
				end repeat
			end if
			tell application "iPhoto" to select album (item i of workout_list)
			tell application "System Events" to tell process "iPhoto" to (click menu item "by Title" of menu 1 of menu item "Sort Photos" of menu 1 of menu bar item "View" of menu bar 1)
			set i to i + 1
		end repeat
	end if
	tell application "System Events"
		delay 1
		if (vLessSeven) = "True" then --This section only executes if iPhoto has version less than 7
			if film_roll is not "" then
				tell application "iPhoto" to select photo library album
				tell process "iPhoto" to click menu item "Film Rolls" of menu 1 of menu bar item "View" of menu bar 1
			else
				tell application "iPhoto" to select photo library album
			end if
		end if
		delay 1
		try --try added since it might not find the appropriate album
			if (vLessSeven) = "True" then
				tell application "iPhoto" to select album (item (i - 1) of workout_list)
			else
				tell application "iPhoto" to select album "Events"
			end if
		on error
			--do nothing
		end try
		set frontmost of process "Installer" to true
	end tell
end tell

on import_iphoto(workout)
	try
		tell application "iPhoto"
			activate
			tell application "System Events" to tell process "iPhoto"
				repeat until not (static text "Would you like to import them?" of window 1 exists)
					delay 1
				end repeat
				repeat until not (window "Duplicate Photo" exists)
					delay 1
				end repeat
				repeat until not (static text "Your photo library was not found. Do you want to find your iPhoto Library folder?" of window 1 exists) and not (window "New Photo Library" exists) and not (window "Open Photo Library" exists)
					delay 1
				end repeat
			end tell
			if not (album workout exists) then
				new album name workout
			end if
			select album workout
			tell application "System Events" to tell process "iPhoto" to (click menu item "by Title" of menu 1 of menu item "Sort Photos" of menu 1 of menu bar item "View" of menu bar 1)
			set the_directory to (path to "root" from user domain) & "Desktop:PumpOne for Men:" & workout as string
			remove every photo of album workout from photo library album
			import from POSIX path of the_directory to album workout
			tell application "System Events"
				tell process "iPhoto"
					repeat until not (progress indicator 1 of splitter group 1 of group 1 of window "iPhoto" exists)
						delay 1
					end repeat
					repeat until not (sheet 1 of window "iPhoto" exists)
						delay 1
					end repeat
				end tell
			end tell
		end tell
		return 0
	on error
		return -1
	end try
end import_iphoto

Just to reiterate, I don’t have much experience in AppleScript, but I do have a ton of experience coding and with other scripting languages, so I can kind of understand what is going on, but any clarification would also be helpful.

Any ideas why this is failing?

Model: Macbook Pro Unibody
Browser: Safari 530.18
Operating System: Mac OS X (10.5)

Hi,

the error message says, that the application “Installer” doesn’t understand the display dialog command.
That’s because there is a global application tell block at the beginning. Using this programming habit is strongly discouraged.
Use application tell blocks only for lines with the appropriate terminology

Ok, I understand the line you are talking about, but just to clarify I did not write this script, I’m just adapting it (like I said I don’t have much experience with applescripting). What would you recommend as the best way to fix this problem?

I have no idea, what this application “Installer” does, but what happens after removing the global tell block?


.
tell application "Installer"
.


.
end tell
.

I think they just wanted to open up the application ‘Installer’ (the one used to install new programs) and then have a dialog box prompt the user if he/she wants to add the photos to iphoto. Is there a better/easier to accomplish this, maybe without Installer and just a regular prompt somewhere else, or is this syntax just wrong?

I’ll try removing the tell block when I wake up first thing tomorrow and post back with those results if I don’t hear from you before then.

“Installer” is actually no scriptable, but you can launch it and make it frontmost with

activate application "Installer"

Consider, that there is no end tell

Ok so I got it working, what I had to do was remove these lines from the front of the code:

activate application "Installer"
display dialog "Installation is almost complete. Would you like the images added to your iPhoto Library as albums? This is recommended." buttons {"No", "Yes"} default button 2
if the button returned of the result is "Yes" then

However, this makes the script run automatically without prompting the user. Is there an easy way to prompt the user (ideally within Installer) if they want to add these photos? What is wrong with the display dialog syntax above (it works in Script Editor, but the dialog won’t show up when in Installer through the pkg)?