restart a loop


repeat with aFile in Files_To_Open
		
		set TheFile to aFile as alias
		try
		-- open TheFile
		end

		tell application "PhotoShop"
		-- Do other stuff! e.g. change the colour mode
		end

end
	

If the try causes an error, I don’t want to do the other stuff!

I want to go back the start of the loop and try to open the next file!

Can anyone suggest how to restart the loop on error?

Thanks,

Jason

like this, if an error occurs, the following code until end try will be skipped

repeat with aFile in Files_To_Open
	
	set TheFile to aFile as alias
	try
		-- open TheFile
	
	tell application "Adobe Photoshop CS"
			-- Do other stuff! e.g. change the colour mode
		end tell
	end try
	
end repeat


set failed to {}
repeat with aFile in Files_To_Open
	set TheFile to aFile as alias
	try
		-- do something that might fail
		-- tell PS to change color mode
		-- end tell
	on error
		set end of failed to TheFile
	end try
end repeat

Ok, so I hoped it wouldn’t come to this but I’m going to include my whole script.

The problem I face is that that I have a few ifs in my repeat and to structure it the way Adam suggested, I think would be messy.

I also don’t understand how the ‘failed’ bit helps!

Here is the code, see what you think:


global ConvertTypes

on run -- open TheFolder
	
	set ExistingTID to AppleScript's text item delimiters
	
	set OpenTypesList to {"PhotoShop EPS", "Illustrator AI", "TIF"}
	
	set ConvertModeTypesList to {"Grayscale", "CMYK", "RGB"}
	
	set SaveTypesList to {"PhotoShop EPS", "TIF"}
	
	display alert "Convert Me" & return & return & "Converts colour modes and file types of choice, and saves a copy to the ' Converted' folder in the same folder" message "¢ The converted file(s) will have a blue label to easily identify that you have run them. You should remove this label when you have finished dealing with the files." & return & return & "IMPORTANT:" & return & "Do not do anything else with your computer whilst this process is running!" buttons {"Cancel", "OK"} cancel button 1 giving up after 20
	
	set OpenTypes to (choose from list OpenTypesList with prompt "Please choose the file type you would like to open:" default items {item 1 of OpenTypesList}) as Unicode text
	if OpenTypes is "false" then error number -128
	
	tell application "Finder"
		
		set TheFolder to (choose folder with prompt "Please choose the folder of files to convert:") --as alias
		
		set TheFolder to TheFolder as Unicode text
		
		set IncSubFolders to display dialog "Include Sub Folders?" buttons {"Yes", "No"} default button 2 with icon 1
		set IncSubFoldersCh to button returned of IncSubFolders
		
		if IncSubFoldersCh is "No" then
			if OpenTypes is "PhotoShop EPS" then
				set Files_To_Open to files in folder TheFolder whose kind is "Adobe Photoshop EPS file"
			else if OpenTypes is "Illustrator AI" then
				set Files_To_Open to files in folder TheFolder whose kind is "Adobe Illustrator Document"
			else if OpenTypes is "TIF" then
				set Files_To_Open to files in folder TheFolder whose file type is "TIFF"
			end if
		else
			if OpenTypes is "PhotoShop EPS" then
				set Files_To_Open to files in entire contents of folder TheFolder whose kind is "Adobe Photoshop EPS file"
			else if OpenTypes is "Illustrator AI" then
				set Files_To_Open to files in entire contents of folder TheFolder whose kind is "Adobe Illustrator Document"
			else if OpenTypes is "TIF" then
				set Files_To_Open to files in entire contents of folder TheFolder whose file type is "TIFF"
			end if
		end if
		
	end tell
	
	activate
	if (count of Files_To_Open) is greater than 0 then
		display dialog "Files Found: " & (count of Files_To_Open) buttons {} cancel button 1 default button 2 with icon 1 giving up after 10
	else
		display dialog "No Files Found in Folder " buttons {} cancel button 1 default button 2 with icon 1 giving up after 10
		error number -128
	end if
	
	set ConvertTypes to (choose from list ConvertModeTypesList with prompt "Please choose the colour mode to convert to:" default items {item 1 of ConvertModeTypesList}) as Unicode text
	if ConvertTypes is "false" then error number -128
	
	set SaveTypes to (choose from list SaveTypesList with prompt "Please choose the file type you would like save each file as:" default items {item 1 of SaveTypesList}) as Unicode text
	if SaveTypes is "false" then error number -128
	
	set failed to {}
	
	repeat with aFile in Files_To_Open
		
		set TheFile to aFile as alias
		
		tell application "Finder"
			if not (exists folder " Converted" in parent of TheFile) then
				make new folder in parent of TheFile with properties {name:" Converted"}
			end if
			
			set TheConvertFolder to folder " Converted" of parent of TheFile as Unicode text
		end tell
		
		tell application "Adobe Photoshop CS2"
			
			set OpenAIOptions to {bits per channel:eight, crop page:art box, mode:grayscale, page:1, resolution:1200, suppress warnings:true, use antialias:true}
			
			set OpenEPSOptions to {mode:grayscale, use antialias:true}
			
			if OpenTypes is "PhotoShop EPS" then
				try
					open TheFile with options OpenAIOptions showing dialogs never
				on error
					display dialog "PhotoShop EPS File Open Error"
					set end of failed to TheFile
					-- error number -128
				end try
			else if OpenTypes is "Illustrator AI" then
				try
					open TheFile with options OpenAIOptions showing dialogs never
				on error
					display dialog "Illustrator AI File Open Error"
					error number -128
				end try
			else if OpenTypes is "TIF" then
				try
					open TheFile showing dialogs never --with options OpenEPSOptions 
				on error
					display dialog "TIF File Open Error"
					error number -128
				end try
			end if
			--set BitmapOptions to {conversion method:middle threshold, resolution:1200}
			
			if ConvertTypes is "Grayscale" then
				change mode of current document to grayscale --with options BitmapOptions
			else if ConvertTypes is "CMYK" then
				change mode of current document to CMYK --with options BitmapOptions
			else if ConvertTypes is "RGB" then
				change mode of current document to RGB --with options BitmapOptions
			end if
			
			delay 2
			--error number -128
			
			set SaveTIFFOptions to {byte order:Mac OS, embed color profile:false, image compression:none, interleave channels:true, layer compression:ZIP, save alpha channels:false, save annotations:false, save image pyramid:false, save layers:false, save spot colors:false, transparency:false}
			
			set SaveEPSOptions to {embed color profile:false, encoding:maximum quality JPEG, halftone screen:false, image interpolation:false, PostScript color management:false, preview type:eight bit TIFF, transfer function:false, transparent whites:false, vector data:false}
			
			set AppleScript's text item delimiters to "."
			
			set CurrentDocument to current document
			set ThedocName to name of CurrentDocument as Unicode text
			set docName to text item 1 of ThedocName as Unicode text
			
			if SaveTypes is "PhotoShop EPS" then
				set ConvertedName to TheConvertFolder & docName & ".eps" as Unicode text
				save current document in ConvertedName as Photoshop EPS appending lowercase extension with SaveEPSOptions and copying
			else if SaveTypes is "TIF" then
				set ConvertedName to TheConvertFolder & docName & ".tif" as Unicode text
				save current document in ConvertedName as TIFF appending lowercase extension with SaveTIFFOptions and copying
			end if
			
			close current document saving no
			
			set AppleScript's text item delimiters to ExistingTID
			
		end tell
		
		tell application "Finder"
			
			--set NewTIF to (item 1 of folder TheConvertFolder whose name contains docName)
			
			--set label index of NewTIF to 4
			
		end tell
	end repeat
	
	activate
	display alert "PROCESS COMPLETE" buttons {"Cancel", "OK"} cancel button 1 giving up after 10
	
end run

The failed bit presumes that you’d like to know that some of your files were not processed, and which ones.

  1. you can use more than one try block
  2. you can nest try blocks
  3. Where is this likely to fail? (I don’t have any of those apps, so can’t text it. If you leave out all the tries, where is it likely to die? What logic applies to each type of failure?

One way to avoid messes is to put different functions into handlers and then your main script organizes and calls them.

Hey Adam,

The failed bit makes sense now.

The script could fail on ‘open’ if a file is corrupt for example.

Yes, handlers would tidy up, but how would they help for restarting the repeat?

The only issue I have with try blocks that wrap around alot of commands is that it is then hard to determine where an error occurs within. It might come to this, as you say with nested try’s

You can give yourself a huge hint on where a failure is in two ways. First is this:


try
	-- do stuff that might bomb
on error e -- this will be the error message.
	display dialog e
	-- do the failure stuff
end try

try
	-- do stuff that might bomb
	-- log a variable here
	-- do some more of it
	-- log a variable here
	-- etc.
	-- log again
on error e
	display dialog e
	-- do the failure stuff
end try

set A to true
set B to true
try
	set A to doStuff(args)
	set B to doMoreStuff(diffArgs)
	if A or B is false then display dialog "A & return & B"
on error e
	display dialog e
end try

to doStuff(args)
	try
		-- do something with args
		return result
	on error
		return false
	end try
end doStuff

to doMoreStuff(diffArgs)
	-- same as above
end doMoreStuff

Thanks for your help Adam. I’ve learnt a few new tricks.

I still pose the original question of can you simply restart a loop when an error occurs within? Kinda like exit repeat.

Hi Tortle,

If you don’t want to use a big try block, then you can use a flag as Adam wrote. Use the flag to see if you should do more processing within the repeat loop. Something like this:


set item_list to {1, 2, 3, 4, 5}
repeat with i in item_list
	set no_error to true
	try
		display dialog i -- Cancel causes error
	on error
		set no_error to false
	end try
	if no_error then
		display dialog "Doing more stuff."
	end if
end repeat

Here, the error handler just errors if the user presses cancel. Somethiing like what would happen is your open command errors. When it errors the flag is set to false, so it skips the folowing statements in the repeat loop.

gl,