on error script is not working and I do not understand as to why

I have the following script (simplified to test). Both do not work. If the file is there I get confirmation. If the file is not nothing happens.


set {theFile} to {("week ") & (do shell script "date +%U") & (".gz")}

try
	
	tell application "Finder"
		if exists file theFile then
			display dialog "It exists."
		end if
	end tell
	
on error
	display dialog "There was an error"
end try


I also tried:

 
set {theFile} to {("week ") & (do shell script "date +%U") & (".gz")}
try
	tell application "Finder"
		if exists file theFile then
			display dialog "It exists."
		end if
	end tell
on error
	handleError()
end try
on handleError()
	display dialog "There was an error"
end handleError

Hi,

display dialog “There is never an error” :wink:

the script is working as expected, because with this code an error never occurs.
The exists line return either boolean true if the file exists or false.
You can omit the whole try block

If you want to see the error do something insane


set theFile to {("week ") & (do shell script "date +%U") & (".gz")} as record
.

Not sure if we understand each other. I need the script to stop if the file is not existing. Because (I omitted the rest of the script) it will hang later on if the file does not exist.

So if this is according to you not the way to do it how do I do it?

I have a deja vu

http://macscripter.net/viewtopic.php?pid=155574#p155574
http://macscripter.net/viewtopic.php?pid=155560#p155560


set theFile to ("week ") & (do shell script "date +%U") & (".gz")

tell application "Finder" to set weekFileExists to exists file theFile
if weekFileExists then
	display dialog "It exists."
else
	-- do the error handling
end if

Hello.

What happens in your code, is that you have mixed two concepts, in the hope of making a better one.

When StefanK posted his solution to you, with the timeout, I figured that was good, that one uses the file exists construct. And System Events is better to use really than finder for this, since finder may be bogged down.

When you put a try - error block outside that code, you are telling applescript to turn off regular throwing of exceptions when files doesn’t exist. Now if System Events and Finders file exist clause, finds out that a file exists by seeing that an exception is not thrown, then that test will be useless, since you have turned off throwing of that exeption, by putting the file exist clause, into a try - error block.

Thanks McUsr and StefanK, now I understand what I am doing wrong. Much appreciated! And thanks for putting up with idiots like me.

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.

I updated the post above.

It is most probably that there is another error in your script, that gets trapped by the on error block, so the test isn’t performed.

And for the record; what I have had problems with has been nesting try catch block within the on error part of that block, I think the approach with forking the code into a handler then will see to that set variables within that handler are recognized outside the on error block

Thank you! This helps. Although I must say I needed to do some reading to follow your thinking as I am not of the same caliber a scripter as you obviously are. (and StefanK)

I do not think so, since the simple script I pasted already did not work.

This is the part I had to read up about to understand. However I think I get the gist.

At the moment it runs a dream thanks to the suggestions posted, so thanking you all for helping.

Regards Eric

Hello!

I am glad it works for you, I would really like to see the solution you came up with, that works for you, just out of curiousity. It interests me, because there isn’t applets that are run from cron every other day around here.

Just the part which tests for the file existance, please! :slight_smile:

No problem, I hope these are the bits you liked to see.



-- get file

do shell script "curl http://192.1.1.190/data_tmp/gespr_dat.csv.gzl -s -o /Users/name/Desktop/gespr_dat.csv.gz"

delay 2

-- rename file and send mail if failed


set {weekName} to {("week ") & (do shell script "date +%U") & (".gz")}


set time_out to 10
tell application "System Events"
	repeat until (exists file "gespr_dat.csv.gz" of desktop folder) or (time_out is 0)
		delay 1.0
		set time_out to time_out - 1
	end repeat
	if time_out = 0 then
		
		-- next I  make sure mail programme is not hanging, just in case...
		
		-- next I send a failure message per mail
		
		delay 3
		set {tTo, tSender, tSubject, tContent} to {"eric@me.com", ("!! Stage 1 of BU Phone Failed Week  " & (do shell script "date +%U")), ".gz"}
		
		delay 2
		tell application "Mail"
			activate
			delay 4
			set nMessage to make new outgoing message with properties {sender:tSender, subject:tSubject, content:((do shell script "date '+%Y.%m.%d'") & (tContent & return & return)), visible:true}
			delay 4
			tell nMessage
				make new to recipient at end of to recipients with properties {address:tTo}
				
				delay 5
			end tell
			send nMessage
		end tell
	else
		set name of file "gespr_dat.csv.gz" of desktop folder to weekName
	end if
end tell

--



set theFile to ("week ") & (do shell script "date +%U") & (".gz")

tell application "Finder" to set weekFileExists to exists file theFile
if weekFileExists then
	
	-- if all is well we now send the file per email
	
	delay 3
	
	
	
	
	-- if not a failure message
else
	delay 3
	set {tTo, tSender, tSubject, tContent} to {"eric@me.com", ("!! Stage 2 BU Phone Failed-Week " & (do shell script "date +%U")), ".gz"}
	
	delay 2
	tell application "Mail"
		activate
		delay 4
		set nMessage to make new outgoing message with properties {sender:tSender, subject:tSubject, content:((do shell script "date '+%Y.%m.%d'") & (tContent & return & return)), visible:true}
		delay 4
		tell nMessage
			make new to recipient at end of to recipients with properties {address:tTo}
			
			delay 5
		end tell
		send nMessage
	end tell
end if


-- next I dump the file in the rubbish-bin and do some other stuff, like closing windows, clean-up, etc.


Thank you!

No problem, and it is me who has to say thank you!