如何访问与类同名但事件代码不同的属性? [英] How can I access a property that has the same name as class but different event code?

查看:17
本文介绍了如何访问与类同名但事件代码不同的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当应用程序对类和属性使用相同的名称但事件代码不同,并且我尝试在对象说明符中使用该属性时,AppleScript 将标识符解释为类而不是属性.例如:

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

这导致错误:

邮件出错:无法获取规则 1 的规则条件 1 的标头.

Mail got an error: Can’t get header of rule condition 1 of rule 1.

AppleScript 编辑器中 header 的样式(蓝色斜体)表明它是一个类名而不是一个属性.如何指定标识符是一个属性并显式解决此命名冲突?

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?

我运行的是 OS X 10.6.8 和 Mail.app 4.5.

I'm running OS X 10.6.8 and Mail.app 4.5.

Applescript:权威指南",Matt Neuberg 建议可以使用 its:

In "Applescript: The Definitive Guide", Matt Neuberg suggests that its may be used:

当应用程序定义了与类同名的属性时,需要关键字it.[...] 说 its 可以消除歧义.

The keyword it is needed when an application has defined a property with the same name as a class. [...] Saying its disambiguates.

但是,这并不能解决我上面示例代码中的问题.添加 its 后,header 的样式仍然是一个类,脚本会导致同样的错误.

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

应用第 20.8.3 节中的示例.具有同名类的属性"具有相同的结果.

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

背景

我正在尝试编写一个 AppleScript 来扩展 Mail.app 的规则条件以支持模式匹配.这些扩展规则之一中的一些规则条件将包含脚本的信息,例如要匹配的模式和模式匹配时要采取的操作,而不是 Mail 应匹配的条件.我想对这些规则条件使用 header 属性.

扩展规则以允许模式匹配的替代方法很好,但不是必需的.我仍然希望回答特定问题,因为在此特定用法以外的情况下可能会出现问题.

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.

推荐答案

通过重新安排,我能够使它失败,正如您所描述的那样.脚本编辑器似乎会锁定它的第一个猜测,因此一个解决方案是使用 run script 在运行时使用原始类,例如:

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:

tell application "Mail"
    header of (get properties of first rule condition of first rule) -- fails
end tell

set myHeader to (run script "tell application \"Mail\"
    return «class rhed» of (get properties of first rule condition of first rule)
end tell")
myHeader --> success

这篇关于如何访问与类同名但事件代码不同的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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