Script for making batch jpgs does not work sometimes

Here’s a script I have written to make batch jpgs. I frequently need to make jpgs of Illustrator files to be mailed to the client. Most of the time the script works without a hitch, but sometimes it throws up an error message saying “The variable the_image is not defined”. It works again after I relaunch ScriptEditor. Need help!!

property type_list : {"TIFF", "PNGf", "PICT", "AI"}
property extension_list : {"tif", "tiff", "png", "pict", "pct", "ai"}

set target_files to choose file with multiple selections allowed
tell application "Finder"
	try
		repeat with a_file in target_files
			set item_info to the info for a_file
			if the file type of item_info is in the type_list or the name extension of item_info is in the extension_list then
				set file_path to a_file as string
				tell application "Image Events"
					set the_image to open file file_path
					save the_image as JPEG in folder "Desktop" of folder "kaushik" of folder "Users" of startup disk with icon
					close the_image
				end tell
			end if
		end repeat
	on error error_msg
		display dialog error_msg
	end try
end tell

Model: G4
AppleScript: 2.1.1
Browser: Camino
Operating System: Mac OS X (10.4)

This might be a bit more reliable; you didn’t need the Finder’s involvement, so this cuts down on message passing, and rather than find the destination each time through the loop, you should declare it up front. Also, the ‘tell item_info’ bit just shortens the line and makes two lookups into one. Otherwise, I don’t know what your problem was - this worked every time for me.

Question: is the source folder a server? If so, you might be experiencing a timeout. Finally, it won’t work on an alias - giving the error you report.

property type_list : {"TIFF", "PNGf", "PICT", "AI"}
property extension_list : {"tif", "tiff", "png", "pict", "pct", "ai"}
property destination : (path to desktop folder)

set target_files to choose file with multiple selections allowed
try
	repeat with a_file in target_files
		set item_info to the info for a_file
		tell item_info to if file type is in type_list or name extension is in extension_list then
			set file_path to a_file as string
			tell application "Image Events"
				set the_image to open file file_path
				save the_image as JPEG in destination
				close the_image
			end tell
		end if
	end repeat
on error error_msg
	display dialog error_msg
end try

In addition to Adam’s improvements, try inserting the instruction launch at the beginning of what you tell Image Events. Faceless scripting applications often require that:

tell application "Image Events"
	launch
	set the_image to open a_file
	-- etc.
end tell

Although it’s not relevant to your problem, you should be aware that the in parameter used with a save command is properly the file to which you want to save an image or document, not the folder to which you want to save the file. Image Events obviously understands what you mean, but some applications don’t “ or, at least, haven’t in the past. I once managed to replace my desktop with a document file called “Desktop” because I’d specified the desktop as the save destination! I forget which application I was scripting at the time.

Hello

For this kind of task, it would be useful to study the scripts delivered by Apple with the operating system.
They are available in the folder named “Folder Action Scripts” and it is quite easy to locate them searching for “Image - Duplicate as”.

Yvan KOENIG (from FRANCE vendredi 22 septembre 2006 20:34:27)

Thanks a lot guys! Your tips really helped.