如何访问与类同名但事件代码不同的属性?
当应用程序对类和属性使用相同的名称但使用不同的事件代码时,并且我尝试在对象说明符中使用该属性,AppleScript 会将标识符解释为类而不是属性。例如:
tell application "Mail"
header of first rule condition of first rule
end
这会导致错误:
邮件出现错误:无法获取规则 1 的规则条件 1 的标头。
编辑器中的 header
样式(蓝色斜体)表明它是类名而不是属性。如何指定标识符是一个属性并显式解决此命名冲突?
我正在运行 OS X 10.6.8 和 Mail.app 4.5。
非工作解决方案
在“Applescript:权威指南”中,Matt Neuberg 建议可以使用its
:
当应用程序定义了与类同名的属性时,需要关键字
it
。 [...]说its
可以消除歧义。
但是,这并不能解决我上面的示例代码中的问题。添加 its
后,header
的样式仍为类,并且脚本会导致相同的错误。
tell application "Mail"
its header of first rule condition of first rule
end
应用第 20.8.3 节中的示例。 “具有同名类的属性”具有相同的结果。
tell application "Mail"
tell first rule
tell first rule condition
get its header
end tell
end tell
end tell
背景
我正在尝试编写一个 AppleScript 来扩展 Mail.app 的规则条件以支持模式匹配。这些扩展规则之一中的某些规则条件将包含脚本的信息,例如要匹配的模式以及模式匹配时要采取的操作,而不是邮件应匹配的条件。我想将 header
属性用于这些规则条件。
扩展规则以允许模式匹配的替代方法也不错,但不被要求。我仍然希望回答这个特定问题,因为在除此特定用法之外的情况下也可能会出现该问题。
When an application uses the same name for a class and a property but different event codes, and I try to use the property in an object specifier, AppleScript interprets the identifier as the class rather than the property. For example:
tell application "Mail"
header of first rule condition of first rule
end
This results in the error:
Mail got an error: Can’t get header of rule condition 1 of rule 1.
The styling of header
in AppleScript Editor (blue italics) suggests it's a class name rather than a property. How can I specify the identifier is a property and resolve this naming collision explicitly?
I'm running OS X 10.6.8 and Mail.app 4.5.
Non-working solution
In "Applescript: The Definitive Guide", Matt Neuberg suggests that its
may be used:
The keyword
it
is needed when an application has defined a property with the same name as a class. [...] Sayingits
disambiguates.
However, this doesn't resolve the issue in my sample code above. After adding its
, header
is still styled as a class and the script results in the same error.
tell application "Mail"
its header of first rule condition of first rule
end
Applying the example from § 20.8.3. "Properties with Eponymous Classes" has the same result.
tell application "Mail"
tell first rule
tell first rule condition
get its header
end tell
end tell
end tell
Background
I'm attempting to write an AppleScript to extend Mail.app's rule conditions to support pattern matching. Some rule conditions in one of these extended rules are to contain information for the script, such as the pattern to match and the action to take if the pattern matches, rather than being conditions that Mail should match. I'd like to make use the header
property for these rule conditions.
Alternative ways of extending rules to allow for pattern matching are fine but not requested. I'd still like the particular question answered, since the issue can arise in cases other than this particular usage.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑:通过进行一些重新安排,我能够按照您的描述使其失败。脚本编辑器似乎确实锁定了它的第一个猜测,因此一个解决方案是使用 run script 在运行时使用原始类,例如:
Edit: I was able to get it to fail as you described by doing some rearranging. The Script Editor does seem to lock in to whatever its first guess was, so one solution would be to use run script to use the raw class at run time, for example:
来自对 "Applescript et Mail.app, bug ou c'est moi",
header
属性具有与类不同的事件代码(“rhed”表示属性,“mhdr”代表该类)。这似乎是错误的实际原因(header
编译为
),并提供了一个潜在的解决方案:您可以使用原始事件代码指示符来获取在这种特殊情况下的财产。但是,第一次保存脚本时,原始代码会被名称替换,第二次保存名称时会被重新解释为类而不是属性,从而要求您更正每次出现的属性名称你节省的时间。通过定义一个处理程序来获取使用 Mail.app 术语的块之外的属性,可以减少需要完成的工作量。
通过在未使用 Mail.app 术语的位置定义处理程序,原始代码将不会被标识符替换。
这只能部分解决保存编辑问题,因为每个单独的属性都需要自己的处理程序,并且(更糟糕的是)处理程序似乎无法在 过滤表单(尽管可以使用循环代替)。因此,我无法接受这个答案,如果有人能找到一个完整的解决方案,我将不胜感激。例如,以下
结果:
From a comment on "Applescript et Mail.app, bug ou c'est moi", the
header
property has a different event code than the class ("rhed" for the property, "mhdr" for the class). This appears to be both the actual cause of the error (header
compiles to«class mhdr»
) and offers a potential solution: you can use a raw event code designator to get the property in this particular case.However, the first time you save the script, the raw code gets replaced by the name, and the second time you save the name is re-interpreted as a class rather than a property, requiring you to correct every occurrence of the property name each time you save. The amount of work to be done can be reduced by defining a handler to get the property outside of a block where Mail.app's terms are being used.
By defining the handler in a location where Mail.app's terms aren't being used, the raw code won't be replaced by an identifier.
This only partly solves the save-edit issue, as each separate property requires its own handler and (what's worse) handlers don't appear to work in filter forms (though loops could be used instead). As such, I cannot accept this answer and would appreciate a full solution, if someone can find one. As an example, the following
results in:
使用规则条件告诉块和关键字its来获取标头。
Use rule condition tell block and the keyword its to get the header.