AppleScript:“使用框架"在“预期的行尾等但找到标识符"中出现错误. [英] AppleScript: "use framework" errors out in "Expected end of line, etc. but found identifier."

查看:33
本文介绍了AppleScript:“使用框架"在“预期的行尾等但找到标识符"中出现错误.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 AppleScript.

I have the following piece of AppleScript.

use framework "Foundation"
display dialog "foo"

注意:本示例中并未实际使用Foundation框架,因为该部分代码与本示例无关.仅此代码就已产生错误.在现实生活中,我们显然只是导入一个框架来使用它,呃:-)

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. 

macOS 脚本编辑器停止的标识符是对话框".

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

当我将代码更改为:

display dialog "foo"

脚本按预期运行.

我有两个问题:

  1. 为什么最上面的例子会产生错误?
  2. 为什么会产生这个确切的错误?或者换句话说:为什么这个错误如此无益?AppleScript 的一般情况是这样吗?

推荐答案

!我的回答所依据的信息已过时,请参阅我的回答下方的更新.

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

脚本出错的原因是:

"通过use语句导入框架只支持脚本库,其他不支持脚本或小程序."

所以基本上:您不能被允许在常规 AppleScript 脚本中使用 use 语句.您可以在脚本库中使用它.

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

为了解决这个问题,我们创建了一个脚本库",它只是另一个 AppleScript 文件.假设我们称这个脚本库为 chewbacca.scpt.您需要将脚本放在 Mac 上的特定位置.(您有一个几个选项)地点).AppleScript 仅在尝试导入脚本库时查看这些位置.

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.

更新:

阅读一些答案并阅读更多文档后:

After reading some answers and reading some more documentation:

AppleScript 的扩展方式是通过导入库,这些库被称为 osax(或Scripting Additions"),因为它们的文件名以 .osax 结尾.OSAX 代表开放脚本架构扩展".要导入 osax,我们在脚本中编写 use library(我不是 100% 确定这一点).通过导入 osax,我们可以使用该 osax 中的命令.

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(该语言)没有以下命令:用户交互对话框(显示对话框)、读取和写入文件、文件系统命令、日期函数以及文本和数学运算.

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.

但是:Apple 确实提供了一个提供以下命令的 osax:StandardAdditions.osax,不难看出为什么这是最常用的 osax 之一.

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.

要导入此 osax:

use scripting additions

现在回到我的问题:

我发现 AppleScript 在某些条件下表现不同:

I see AppleScript behaving differently under certain conditions:

  1. 脚本不导入 osax
  2. 脚本导入 osax(但不是 StandardAdditions.osax)

在情况 1 中,AppleScript(运行时?)似乎在默默地自动导入 StandardAdditions.osax.我认为是这种情况,因为我可以使用 display dialog.

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.

在情况 2 AppleScript(运行时)不会自动导入 StandardAdditions.osax.

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

我可以对这种不同的行为进行理论分析:

I can theorize about this different behavior:

我怀疑在情况 1 中,他们想让人们更容易开始使用 AppleScript,因此他们会自动导入大多数人/初学者可能想要使用的基本命令.

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.

情况 2 背后的想法可能是这样的:开发人员明确地导入了 osax,因此他们可能没有需要 StandardAdditions.osax 所以我们不要自动导入它".

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

我在某处发现通过将 use scripting added 添加到您的脚本中,始终显式导入 StandardAdditions.osax 是个好主意.以后我会遵循这个建议.

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.

这篇关于AppleScript:“使用框架"在“预期的行尾等但找到标识符"中出现错误.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆