osacompile: wrong Event code for path of document generated

macOS BigSur 11.4, I do not know whether the issue described here is new with BigSur.

Demo AppleScript:

on run argv
	set the_app to "Preview"
	tell application "Preview" -- Application is hard-coded
		set the_doc to front document
		try
			set the_result_1 to the path of the_doc
		on error theError number errorNumber
			set the_result_1 to (errorNumber as string) & ": " & theError
		end try
	end tell

	tell application the_app -- Application is variable
        set the_doc to front document
		try
			set the_result_2 to the path of the_doc
		on error theError number errorNumber
			set the_result_2 to (errorNumber as string) & ": " & theError
		end try
	end tell
	the_result_1 & linefeed & the_result_2

end run

When compiled with ScriptEditor, this script returns two times the path of the active document of Preview and the scpt-file can be executed with osascript as well:

~/Desktop % osascript demo-with-preview.scpt 
/Users/mb/Downloads/Stones/fract1.jpg
/Users/mb/Downloads/Stones/fract1.jpg

But when I osacompile the script's text to a.scpt, the second tell-block throws error -1728 because event code FTPc (path of URL) is used instead of ppth (path of Preview's document).

~/Desktop % osascript a.scpt                
/Users/mb/Downloads/Stones/fract1.jpg
-1728: „Preview“ hat einen Fehler erhalten: „«class FTPc» of document "fract1.jpg"“ kann nicht gelesen werden.
Answered by red_menace in 682129022

Other than a few basic terms from the default Standard Suite (which can be trimmed or edited), the same terms and/or event codes in different applications may not exist or perform the same function, and even if they did would be entirely accidental/coincidental (there is no standard usage or practice). Things like path vary even among Apple applications.

The scripting terminology for any given application should be declared at compile time. The application reference needs to be an absolute object specifier (instead of something ambiguous such as a property or variable) so those terms can be looked up from the appropriate dictionary.

A script does compile when not declaring the application, but there aren’t many terms provided by the Standard Suite. The system is providing a default Standard Suite for the unknown application; who knows what it is using to guess which term (URL or file path) to put in there, but apparently it can guess wrong.

You can also use the chevron syntax to fix where it guesses wrong (Script Debugger is handy to show the raw syntax). When I changed the second one to use a path, the compiler didn’t change it back, so that may be an option.

Preview doesn’t have a scripting dictionary of its own, the system just provides a Standard Suite since it was specified as scriptable.  The application needs to be specified at compile time though, so what are you hoping to accomplish by not doing that?

red_menace, according it's info.plist Preview is scriptable and has no scripting dictionary. But I think, a missing dictionary is not relevant here. You can exchange Preview with TextEdit and you will get the same result: Both tell blocks work when executed from ScriptEditor or run with osascript using the scpt-file. But when I osacompile the script's text, the second tell block throws error -1728.

You wrote "application needs to be specified at compile time though", I do not understand "though" in this context (sorry, I am not a native english speaker).

Not hard-coding the application and using a string variable with tell application works and is accepted by both ScriptEditor and osacompile. Accessing the front document succeeds in both tell blocks.

You asked what I am hoping to accomplish: Well, the real script is part of an Alfred work flow (see https://www.alfredapp.com/help/workflows/actions/run-nsapplescript/) and asks System Event for the currently active application and from there it tries to get the file path of document 1 via property path oder file.

Accepted Answer

Other than a few basic terms from the default Standard Suite (which can be trimmed or edited), the same terms and/or event codes in different applications may not exist or perform the same function, and even if they did would be entirely accidental/coincidental (there is no standard usage or practice). Things like path vary even among Apple applications.

The scripting terminology for any given application should be declared at compile time. The application reference needs to be an absolute object specifier (instead of something ambiguous such as a property or variable) so those terms can be looked up from the appropriate dictionary.

A script does compile when not declaring the application, but there aren’t many terms provided by the Standard Suite. The system is providing a default Standard Suite for the unknown application; who knows what it is using to guess which term (URL or file path) to put in there, but apparently it can guess wrong.

You can also use the chevron syntax to fix where it guesses wrong (Script Debugger is handy to show the raw syntax). When I changed the second one to use a path, the compiler didn’t change it back, so that may be an option.

osacompile: wrong Event code for path of document generated
 
 
Q