"Variable not defined" error

I’m hoping one of you helpful scripters can give me a hand. I am making an AppleScript Studio application that puts a graphical interface on top of many Image Events functions. Ideally, this script will help me automate resizing and formatting several images at one time. First, it may help to get a look at my GUI:
http://mat.ucsb.edu/~benlozano/images/thesis/photomatic.jpg

The user will choose one or more images in a dialog box, then choose one or all of the functions on the right, and finally click on the “Start Job” button to start the script.

I can get the app to work 9 out of 10 times on my laptop. However, once I move the app to another computer, the success ratio falls to maybe 1 out of 30. Virtually everytime I run it on a different machine, I get an error that says “The variable printImage is not defined”.

Below is my code, hopefully you can understand what is happening from the comments. Does anyone have any idea why this script will work sometimes and not others?


global originalFile
global numItems
global saveHere
-- These variables will be used throughout the script

on mouse up theObject event theEvent
	-- activates when the user clicks (and releases) the "Choose a file..." button
	
	set originalFile to (choose file with prompt "Choose one or more image files:" with multiple selections allowed without invisibles) as list
	-- Prompts the user to select one or more images
	set numItems to count items in originalFile
	-- Gets a count of the number of items the user selected
	
	if numItems is 1 then
		display dialog "You have selected " & (numItems as string) & " image for processing." buttons {"OK"}
	else
		display dialog "You have selected " & (numItems as string) & " images for processing." buttons {"OK"}
	end if
	-- Alerts the user how many items will be processed
	
end mouse up

on clicked theObject
	
	set runPrint to state of button "checkPrint" of window "main"
	set runWeb to state of button "checkWeb" of window "main"
	set runDVD to state of button "checkDVD" of window "main"
	set runCustom to state of button "checkCustom" of window "main"
	-- Checks the status of 4 checkboxes in the GUI
	-- If a box is checked, the corresponding function is run below
	
	set saveHere to (choose folder name with prompt "Choose a place to save the processed images:")
	-- Prompts the user for a location to save the images after they have been formatted
	
	if runPrint is 1 then
		-- this section is executed if the "Print" checkbox has been checked	
		
		repeat with x from 1 to numItems
			-- a loop is started to perform this action for each file that has been selected
			
			set outputPrintFile to (saveHere as string) & "print_" & x & ".tif"
			--a formatted image file will be saved in the user specified folder
			-- the formatted image will be named "print_(loop nember).tif"
			
			try
				tell application "Image Events"
					-- calls Image Events
					set printImage to (open (item x of originalFile))
					--a variable is assigned to the nth item in the list "orignalFile"
					-- that variable is also opened by Image Events
					save printImage as TIFF in file outputPrintFile with icon
					-- saves the formatted image in TIFF format
					close printImage
					--closes the image
				end tell
				
			on error errorMessage
				display dialog errorMessage
			end try
			
		end repeat
		
	end if

end clicked

Note: I have omitted the remainder of the script since it is repeats the same basic format as above.

Model: Powerbook G4/667 (DVI)
Browser: Safari 412
Operating System: Mac OS X (10.4)

I don’t understand why you declare global variables; is it absolutely necessary?
I haven’t seen the rest of the script, but since the variable ‘printImage’ is derived from one of your globals [set printImage to (open (item x of originalFile))] this may be giving you your issue. When a handler changes something other than the value it returns, such as a global, its called a ‘side effect’. Because of this you should avoid using ‘globals’ unless absolutely needed.

SC

I do have to declare global variables so that I can save it in one handler (on mouse up) and pass it to another (on clicked). I did some more experimenting with where the variable is declared. I also had the app generate a text file of what gets saved in the “printImage” variable in each loop, a sort of log of what’s going on.

There has been some success with this. In running the app on a different computer, I selected 3 test files. All but one complete successfully. The one that does not complete gives the same error I have recieved in the past “The variable printImage is not defined”. Furthermore, the file that gives the error is the first one in the stored list (which for some reason is the last one selected). For example, if I choose “Photo1.jpg”, “Photo2.jpg”, “Photo3.jpg”; the list stores it in reverse order, and it is “Photo3.jpg” that gives me an error.

Three “log” files also get created, however one is blank. I would like to share what the other two log files say, but this website doesn’t like the syntax it’s in and it gets deleted when I try to post this message. Maybe I’ll do a screen shot later if it’s helpful seeing what gets generated.

Below is my updated code, any suggestions on why one file does not get processed correctly? And thank you for the help.

on mouse up theObject event theEvent
	-- activates when the user clicks (and releases) the "Choose a file..." button
	
	global originalFile
	set originalFile to (choose file with prompt "Choose one or more image files:" with multiple selections allowed without invisibles) as list
	-- Prompts the user to select one or more images
	
	global numItems
	set numItems to count items in originalFile
	-- Gets a count of the number of items the user selected
	
	if numItems is 1 then
		display dialog "You have selected " & (numItems as string) & " image for processing." buttons {"OK"}
	else
		display dialog "You have selected " & (numItems as string) & " images for processing." buttons {"OK"}
	end if
	-- Alerts the user how many items will be processed
	
end mouse up

on clicked theObject
	
	set runPrint to state of button "checkPrint" of window "main"
	set runWeb to state of button "checkWeb" of window "main"
	set runDVD to state of button "checkDVD" of window "main"
	set runCustom to state of button "checkCustom" of window "main"
	-- Checks the status of 4 checkboxes in the GUI
	-- If a box is checked, the corresponding function is run below
	
	local counter
	set counter to my numItems
	
	local fileList
	set fileList to my originalFile
	
	local saveHere
	set saveHere to (choose folder name with prompt "Choose a place to save the processed images:")
	-- Prompts the user for a location to save the images after they have been formatted
	
	if runPrint is 1 then
		-- this section is executed if the "Print" checkbox has been checked	
		repeat with x from 1 to counter
			-- a loop is started to perform this action for each file that has been selected
			
			local outputPritFile
			set outputPrintFile to (saveHere as string) & "print_" & x & ".tif"
			--a formatted image file will be saved in the user specified folder
			-- the formatted image will be named "print_(loop nember).tif"
			
			try
				tell application "Image Events"
					-- calls Image Events
					local printImage
					set printImage to (open (item x of fileList))
					set printLog to (path to desktop as string) & "Print Log " & x & ".txt"
					set writeToLog to open for access printLog with write permission
					write printImage to writeToLog
					close access writeToLog
					--a variable is assigned to the nth item in the list "orignalFile"
					-- that variable is also opened by Image Events					
					save printImage as TIFF in file outputPrintFile with icon
					-- saves the formatted image
					close printImage
					--closes the image
				end tell
				
			on error errorMessage
				display dialog errorMessage
			end try
			
		end repeat
     end if
end clicked