Hi, Please Help.
I’m trying to process every file in a folder and if I encounter a problem with a file I would like to take the filename and display it as a dilaog message. I can do this but a dialog message appears after each time it processes a file but what I would like to do is process every file first and then display a dialog message at the end with the name of each file that has failed.
tell application "Finder"
set display_string to ""
set all_files to every file of (folder "Test Folder" of desktop) -- replace with your folder
repeat with current_file in all_files
set problem_encountered to true -- replace with code that sets problem_encountered to true for failed files only
if problem_encountered then set display_string to display_string & return & name of current_file
end repeat
end tell
activate
if display_string is "" then
display alert "No file problems encountered."
else
display alert "Problem encountered with the following files:" & return & display_string
end if
I think I’d rely on a try block:
tell application "Finder"
set all_files to files of (entire contents of (choose folder))
repeat with F in all_files
try
-- do some processing to each file or call a handler to do it
on error e
set N to name of F
display dialog "Oops - an error with file: " & N & return & e
-- do something with the failed file, perhaps move it to a problem folder
end try
end repeat
end tell
As Adam says, a try block is certainly the way forward if you can’t check a specific condition. It really depends on the nature of the “problem”.
The trick is to check for the exception, whatever it may be, gathering the names of the offending items in a list as you go (which is a bit more efficient than concatenating text).
You can then convert the list to text, delimiting it in the most appropriate way ” perhaps something like this:
on |get text list| from item_list around item_separator
set original_delimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to item_separator
set text_list to item_list as Unicode text
set AppleScript's text item delimiters to original_delimiters
text_list
end |get text list|
set exception_list to {} (* start with an empty list *)
set target_folder to path to applications folder (* or choose folder *)
tell application "Finder"
set app_list to target_folder's application files
repeat with this_app in app_list
(* apply the relevant test: either check a value, attempt an operation within a try statement ” or do both *)
set this_version to this_app's version
if this_version is "" then (* add the item's name to the exception list *)
set end of exception_list to this_app's name
end if
end repeat
end tell
if (count exception_list) is 0 then
set report_text to "No exceptions were found."
else
(* you can delimit the list any way you like ” as demonstrated by the examples below *)
(* uncomment the option you prefer, commenting out the others ” or modify one as required *)
set text_list to |get text list| from exception_list around return
-- set text_list to |get text list| from exception_list around " / "
-- set text_list to " \"" & (|get text list| from exception_list around "\", \"") & "\"."
(* then put your report together *)
set report_text to "The following exceptions were found:" & return & return & text_list
end if
display dialog report_text
And before anyone suggests a more efficient way of identifying the particular “problem” above ” yes, it could be done sans repeat loop. But that was just an example.
tell application "Finder" to set exception_list to name of (path to applications folder)'s application files whose version is ""