AppleScript: “use framework” errors out in “Expected end of line, etc. but found identifier.”

≯℡__Kan透↙ 提交于 2019-12-11 05:16:54

问题


I have the following piece of AppleScript.

use framework "Foundation"
display dialog "foo"

Note: the Foundation framework is not actually used in this example, because that part of the code is irrelevant for this example. This code alone already produces the error. In real life, we obviously only import a framework to use it, duh :-)

When I run this I get the unhelpful error:

Expected end of line, etc. but found identifier. 

The identifier on which the macOS Script Editor stops is "dialog".

When I change the code to:

display dialog "foo"

The script runs as I expect it.

I have two questions:

  1. Why does the top example produce an error?
  2. Why does it produce this exact error? Or in other words: why is this error so unhelpful? Is this the case for AppleScript in general?

回答1:


The predication in your answer is outdated. You can import frameworks in standard scripts nowadays (AFAIR since Yosemite).

If you apply an use framework statement you have to add

use scripting additions

to be able to access display dialog




回答2:


! The information I based my answer on was out of date, see under my answer for the update.


The reason why the script errors is because:

"The importation of frameworks via the use statement is only supported in script libraries, not in other scripts or applets."

So basically: you're not allowed to use the use statement in a regular AppleScript script. You can only use it in script libraries.

To fix this we create a "script library", which is just another AppleScript file. Let's say we call this script library chewbacca.scpt. You need to place the script in a specific location on your Mac. (You have a few options for this location). AppleScript only looks in those locations when trying to import script libraries.

Then, to use the script library do something like:

tell script "chewbacca"
  display dialog "foo"
end tell

That should give the desired result.


Update:

After reading some answers and reading some more documentation:

The way AppleScript is extended is by importing a library, these libraries are called osax (or "Scripting Additions") because their file names end in .osax. OSAX stands for "Open Scripting Architecture eXtension". To import an osax we write use library in our script (I'm not 100% sure about this). By importing an osax we can use the commands in that osax.

AppleScript (the language) does not have commands for things like: user interaction dialogs (display dialog), reading and writing files, file system commands, date functions, and text and mathematical operations.

But: Apple does provide an osax that offers these commands: StandardAdditions.osax, it's not hard to see why this is one of the most commonly used osax.

To import this osax:

use scripting additions

Now back to my question:

I see AppleScript behaving differently under certain conditions:

  1. a script does not import an osax
  2. a script imports an osax (but not StandardAdditions.osax)

In situation 1 it seems like AppleScript (the runtime?) silently auto-imports StandardAdditions.osax. I think this is the case because I can use display dialog.

In situation 2 AppleScript (the runtime) does not auto-import StandardAdditions.osax.

I can theorize about this different behavior:

I suspect for situation 1 they want to make it easier for people to get started with AppleScript so they auto-import the basic commands most people/beginners probably want to use.

The thinking behind situation 2 might have been something like: "the developer is explicitly importing an osax so they may not have a need for StandardAdditions.osax so let's not auto-import it".

I've found somewhere that it's a good idea to always explicitly import StandardAdditions.osax by adding use scripting additions to your script. I'll follow this advice in the future.



来源:https://stackoverflow.com/questions/52806520/applescript-use-framework-errors-out-in-expected-end-of-line-etc-but-found

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!