Photoshop script errors

I’m trying to get a couple of scripts working together to automate my iPhoto/Photoshop tasks. One (I assume the PS one) keeps giving me an error. This script performs a PS action on photos selected in iPhoto, and then saves it to the selected folder (Temp). Here’s the text:

property default_action : "AutoContrast"
property default_set : "CustomActions"

tell application "iPhoto"
	activate
	try
		set the view to organize
		copy (my selected_images()) to these_images
		if these_images is false then error "Please select some images before using this script."
		
		repeat
			display dialog "Do Photoshop Action" & return & return & "This script will perform the indicated Photoshop action on the selected images." & return & return & "Default Action: " & default_action & return & "Action Set: " & default_set buttons {"Cancel", "Set Prefs", "Continue"} default button 3
			set the user_choice to the button returned of the result
			if the user_choice is "Continue" then
				exit repeat
			else
				-- prompt to set the action
				display dialog "Enter the name of the action to play." & return & return & "Note that the case of letters in the Action name is important and must match the case of the name in the Photoshop Actions palette." default answer default_action buttons {"Set", "Skip"} default button 2
				copy the result as list to {text_returned, button_pressed}
				if the button_pressed is "Set" then set default_action to (the text_returned) as string
				-- prompt to set the action set
				display dialog "Enter the name of the action set containing the action being played." & return & return & "Note that the case of letters in the Action Set name is important and must match the case of the name in the Photoshop Actions palette." default answer default_set buttons {"Set", "Skip"} default button 2
				copy the result as list to {text_returned, button_pressed}
				if the button_pressed is "Set" then set default_set to (the text_returned) as string
			end if
		end repeat
		
		set the target_folder to (choose folder with prompt "Choose a destination folder for the duplicated images:") as string
		
		set these_files to {}
		repeat with i from 1 to the count of these_images
			set this_image to item i of these_images
			-- get image paths and convert POSIX paths to alias references
			set this_file to the image path of this_image
			set this_file to (this_file as POSIX file) as alias
			set the end of these_files to this_file
		end repeat
	on error error_message number error_number
		if the error_number is not -128 then
			display dialog error_message buttons {"OK"} default button 1
		end if
		return "user cancelled"
	end try
end tell

tell application "Adobe Photoshop 7.0"
	activate
	try
		set display dialogs to never
		set the processed_images to {}
		repeat with i from 1 to the count of these_files
			set this_file to item i of these_files
			close every document saving no
			open this_file showing dialogs never
			set the doc_name to the name of the current document
			set the target_file to (target_folder & doc_name)
			save current document in file target_file as JPEG
			do action default_action from default_set
			flatten current document
			save current document
			close current document saving no
			set the end of the processed_images to ((file target_file) as alias)
		end repeat
	on error error_message
		display dialog error_message buttons {"Cancel"} default button 1
	end try
end tell


on selected_images()
	tell application "iPhoto"
		try
			-- get selection
			set these_items to the selection
			-- check for single album selected
			if the class of item 1 of these_items is album then error
			-- return the list of selected photos
			return these_items
		on error
			return false
		end try
	end tell
end selected_images

Then, a folder action script attached to the Temp folder (which the last script saved the edited pics to) imports any files added to that folder back into iPhoto. Here’s that one:

on adding folder items to this_folder after receiving these_items
	set this_item to this_folder
	-- convert the file reference to UNIX style
	set this_path to the POSIX path of this_item
	display dialog "Import these Photos to iPhoto?" buttons {"Yes", "No"} default button 1
	if button returned of the result is "Yes" then
		-- bring the target application to the front
		tell application "iPhoto"
			activate
			try
				tell application "System Events"
					tell process "iPhoto"
						-- summon the import dialog
						click menu item "Import�" of menu "File" of menu bar 1
						-- enter the path to the image in the dialog input
						set the value of text field 1 of group 1 of group 2 of window "Import Photos" to this_path
						delay 1
						-- click to start the import
						click button "Import" of window "Import Photos"
						if the last character of (this_item as string) is ":" then
							-- it's a folder so click the import button again
							click button "Import" of window "Import Photos"
						end if
					end tell
				end tell
			on error error_message
				display dialog error_message buttons {"OK"} default button 1
			end try
		end tell
		display dialog "Delete Originals?" buttons {"Yes", "No"} default button 2
		if button returned of the result is "Yes" then
			tell application "Finder" to delete items of this_folder
		end if
	end if
end adding folder items to

The problem I’m having is that somewhere between the PS action script finishing and the folder action script finishing, I get this error: NSRecieverEvaluationScriptError: 4. The PS action script is working, b/c the edited files are in the Temp folder. But the error appears after the “Import photos into iPhoto” dialog, and before the “Delete Originals?” dialog. One other bizarre behavior here, is that the import script will then repeat itself, at least until the originals are actually deleted.
Please help!

Yeah, I know it’s taboo to replay to yourself, but the post was so long, I don’t think an edit would get noticed.
This problem is too inconsistent to be significant. I restarted iPhoto, and the problem went away completely. So this one’s done.