How do I avoid a Repeat loop to abort upon errors?
I am attempting to use an applescript to embed metadata to each of my iphoto library’s photographs. The problem is that some of the photos cause an error in the last part of the loop (exiftool, a command line application, fails to write data to a few photos), which causes the loop to be aborted. I would like to implement into this repeat loop: 1-) in case of error, the error is exported to a log file 2-) the photo that caused the error is skipped and the repeat loop continues with the next photo
Below is the repeat loop I am currently using (adapted from the script I found here http://highearthorbit.com/289/). How could I skip eventual errors? Please bear in mind that I have just begun exploring applescript. Thanks in advance.
repeat with i from 1 to the count of these_images set the keywordslist to “” set this_photo to item i of these_images tell this_photo set the image_file to the image path set the image_title to the title set the image_filename to the image filename set the image_comment to the comment set the assigned_keywords to the name of keywords end tell repeat with j from 1 to the count of assigned_keywords set the keywordslist to keywordslist & ” -keywords=’” & item j of assigned_keywords & “‘” end repeat set output to do shell script ¬ “exiftool -title=”" & image_title & ¬ “”" & keywordslist & ¬ ” ” & ” -Caption-Abstract=”" & image_comment & ¬ “”" & ” -Copyright=’” & copyright & ¬ “‘ ” & ” -CopyrightNotice=’” & copyright & ¬ “‘ ” & ” -Rights=’” & copyright & ¬ “‘ ” & ” -Marked=’” & Copyrighted & ¬ “‘ ” & “”" & image_file & “”" do shell script “rm “” & image_file & “”" & exifToolOriginal end repeat
Related posts:
- exit repeat abuse;-)
- Repeat Until Dialog is Acknowledged?
- Avoid application launch
- Avoid application launch
- Re: Avoid application launch
- How to avoid text distortion
- How to avoid the movie distortion ?
- How to avoid text distortion
- Avoid Compiler Warning regarding IB 3.2
- Should I try to avoid loading principal class of plug-in?
On 25-Oct-2009, at 17:34, André Sartori wrote:
on error errtext
Insert a try block¹ where you want to trap the error, then use the ³on error² to tell your script what to do when the error happens. I¹ve included a handler that allows you to write to a file as an example, but you can do anything you want with the error. I have not tested this, but it should be what you¹re looking for.
Thanks. Jim
set logFileName to “This is the name of my log file.log” set logFile to (((path to desktop from user domain) as text) & logFileName) as text — this is the path to the log file (right now the desktop), but you can change the path to wherever
tell application “iPhoto” repeat with i from 1 to the count of these_images set the keywordslist to “” set this_photo to item i of these_images tell this_photo set the image_file to the image path set the image_title to the title set the image_filename to the image filename set the image_comment to the comment set the assigned_keywords to the name of keywords end tell repeat with j from 1 to the count of assigned_keywords set the keywordslist to keywordslist & ” -keywords=’” & item j of assigned_keywords & “‘” end repeat
Thank you very much for the replies. That solved my problem!
2009/10/26 Jim Skibbie: > Insert a ‘try block’ where you want to trap the error, then use the “on > error” to tell your script what to do when the error happens. I’ve included > a handler that allows you to write to a file as an example, but you can do > anything you want with the error. I have not tested this, but it should be > what you’re looking for. > > Thanks. > Jim > > set logFileName to “This is the name of my log file.log” > set logFile to (((path to desktop from user domain) as text) & logFileName) > as text — this is the path to the log file (right now the desktop), but you > can change the path to wherever > > > tell application “iPhoto” > repeat with i from 1 to the count of these_images > set the keywordslist to “” > set this_photo to item i of these_images > tell this_photo > set the image_file to the image path > set the image_title to the title > set the image_filename to the image filename > set the image_comment to the comment > set the assigned_keywords to the name of keywords > end tell > repeat with j from 1 to the count of assigned_keywords > set the keywordslist to keywordslist & ” -keywords=’” & item j of > assigned_keywords & “‘” > end repeat > – insert the try block to trap the error > try > set output to do shell script ¬ > “exiftool -title=”" & image_title & ¬ > “”" & keywordslist & ¬ > ” ” & ” -Caption-Abstract=”" & image_comment & ¬ > “”" & ” -Copyright=’” & copyright & ¬ > “‘ ” & ” -CopyrightNotice=’” & copyright & ¬ > “‘ ” & ” -Rights=’” & copyright & ¬ > “‘ ” & ” -Marked=’” & Copyrighted & ¬ > “‘ ” & “”" & image_file & “”" > do shell script “rm “” & image_file & “”" & exifToolOriginal > on error errMsg number errNum > – if an error occurs, you can write a line out to a log file > using this DebuggingLog handler > set logFileErrorString to “The following error occurred: ” & > errMsg & ” Error No: ” & errNum & return > try > my DebuggingLog(logFile, logFileErrorString) > on error errMsg number errNum > display dialog “An error occurred writing to the log file: ” > & errMsg & ” No: ” & errNum > end try > end try > end repeat > end tell > > on DebuggingLog(outFile, myData) > try > set fileRef to (open for access outFile with write permission) > write (myData) to fileRef starting at eof > close access fileRef > on error errMsg number the errNum > try > close access outFile > error errMsg number errNum > end try > end try > end DebuggingLog