Osax and non fatal errors

Here is a deliberately dirty piece of code written only to study the behavior of calls to Osaxen like Standard Additions.


on run
	set thePathname to (path to desktop as text) & "Truc.csv"
	tell application "TextEdit"
		try
			close access file thePathname
		end try
		set theOpenFile to open for access file thePathname with write permission # EDITED
		set eof theOpenFile to 0
	end tell
	tell application "Preview"
		try
			write "Text;all;emea;common;cs;pub.promo" & return to theOpenFile as «class utf8»
		on error errmsg number errNbr
			display dialog "Erreur : " & errmsg & ", number : " & errNbr
		end try
	end tell
	close access theOpenFile
end run

The log report is :

tell current application
path to desktop as text
→ “Macintosh HD:Users:yvankoenig:Desktop:”
end tell
tell application “TextEdit”
close access file “Macintosh HD:Users:yvankoenig:Desktop:Truc.csv”
→ error number -1708
«event ascrgdut»
→ error number -1708
close access file “Macintosh HD:Users:yvankoenig:Desktop:Truc.csv”
→ error number -10004
end tell
tell current application
close access file “Macintosh HD:Users:yvankoenig:Desktop:Truc.csv”
end tell
tell application “TextEdit”
open for access file “Macintosh HD:Users:yvankoenig:Desktop:Truc.csv” with write permission
→ error number -10004
end tell
tell current application
open for access file “Macintosh HD:Users:yvankoenig:Desktop:Truc.csv” with write permission
→ 2800
end tell
tell application “TextEdit”
set eof 2800 to 0
→ error number -10004
end tell
tell current application
set eof 2800 to 0
end tell
tell application “Preview”
write "Text;all;emea;common;cs;pub.promo
" to 2800 as «class utf8»
→ error number -10004
end tell
tell current application
write "Text;all;emea;common;cs;pub.promo
" to 2800 as «class utf8»
close access 2800
end tell

As you see, calling an OSAX function like write generate a non fatal error forcing the system to repeat the call enclosed in a tell current application . end tell block.

As you see too, the try . on error . end try block doesn’t trap the error.

Knowing that, when I see that an instruction generates this kind of non fatal error, I hard code the tell current application statement and so the code no longer issue the non fatal error.

So, at this time, I know that if I must call :
close access, open for access, set eof, write.
in a tell application “xxx” . end tell block, it’s good practice to hard code the statement tell current application.

My problem is about calls to Osax which don’t generate a non fatal error when the script is ran in the Script Editor.
How may I know their behavior when the script will be ran as an application ?

I was hoping that the try . on error . end try block would report the error but, as you saw, it doesn’t.

Must I hard code tell current application for every calls to OSAX functions ?

Yvan KOENIG (VALLAURIS, France) dimanche 9 septembre 2012 12:12:55

Yvan,

This may help you:

www.mailinglistarchive.com/html/applescript-users@lists.apple.com/2009-08/msg00452.html

Thanks Shane

I didn’t knew the linked thread but I knew the revisions note.

I asked because I saw some surprising behaviors.

In some scripts like that :

tell application “System Events”
– some code#1
end tell

– some code#2

I discovered in the log report that

the code#2 was reported inside the tell . end tell block.

When running from the Editor code#2 wasn’t issuing error -10004 but I was not sure when used as an application.
I guess that, all of us, we met some slightly different behaviors in both cases.

Yvan KOENIG (VALLAURIS, France) dimanche 9 septembre 2012 17:08:08