Hello!
If you can’t avoid to have that try catch block, for other reasons, and still want such a test to succed, then there is the option of forking out code into a handler.
This is problems I have dealt with myself for a aday, so thank you for helping me understand it!
The problem is that you can’t really have nested error handlers in the same script or handler, and except thing to work, as error handlers, performs kind of longjumps in your code, and therefor doesn’t really consider all statements in the inner nested block.
Now this is similar to your problems, but not exactly the same.
The common solution, where one either want another level of error handlers, or turning the one in charge off, is to fork out that code within the errorhandler into its own handler.
In your particular case, code that would have worked would have looked like this:
As I would have had a second error handler block in there in order for having the variables I set within the block being considered in the outer try catch block.
to checkFile for hfsAtDesktop
tell application "Finder"
if exists file hfsAtDesktop then
return true
else
return false
end if
end tell
end checkFile
try
set afail to false
-- stuff here ...
if checkFile for "hfstest.txt" then
log "it did"
else
log "it didn't"
end if
-- much more stuff here
on error
set afail to true
end try
Edit:
I can’t seem to reproduce this error, but if it was so in ChangeAgent’s case, that he didn’t get any error when the file indeed didn’t exist, then that is what must have happened.
Or, maybe an error was raised earlier, if there were more code inside his try - end try block.
Try inserting an
log e & ": " & n
inside the on error e number n block of your code, because if an error is raised before your code executes, then your test for file-existance will never be executed, as the code there will be just jumped over.
When I run the code below, both as a script and as an applet, it gives the right answers, as to whether the file exists, or not.
try
set afail to false
-- stuff here ...
tell application "Finder"
if exists file "hfstest.txt" then
set checkFile to true
else
set checkFile to false
end if
end tell
if checkFile then
display dialog "it did"
else
display dialog "didn't"
end if
-- much more stuff here
on error
set afail to true
end try
And you should really construct the filename in another way, not using braces, where you can just concatenete the text items.