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

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

问题描述

我有以下AppleScript.

I have the following piece of AppleScript.

use framework "Foundation"
display dialog "foo"

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

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(或脚本添加"),因为它们的文件名以.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(该语言)没有用于以下操作的命令:用户交互对话框(display dialog),读写文件,文件系统命令,日期函数以及文本和数学运算.

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 additions来显式导入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天全站免登陆