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.
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.