如何访问与类同名但事件代码不同的属性?

问题描述 投票:0回答:3

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

tell application "Mail"
    header of first rule condition of first rule
end

这导致错误:

邮件出错:无法获取规则 1 的规则条件 1 的标题。

AppleScript 编辑器中

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

我正在运行 OS X 10.6.8 和 Mail.app 4.5.

非工作解决方案

在“Applescript: The Definitive Guide”中,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 的规则条件以支持模式匹配。这些扩展规则之一中的某些规则条件将包含脚本的信息,例如要匹配的模式和模式匹配时要采取的操作,而不是 Mail 应该匹配的条件。我想为这些规则条件使用

header
属性。

扩展规则以允许模式匹配的替代方法很好但没有要求。我仍然希望回答特定问题,因为在这种特定用法以外的情况下可能会出现问题。

applescript
3个回答
3
投票

编辑:通过重新排列,我能够按照您的描述使它失败。脚本编辑器似乎确实锁定了它的第一个猜测是什么,所以一个解决方案是使用 run script 在运行时使用原始类,例如:

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

0
投票

根据对“Applescript et Mail.app, bug ou c'est moi”的评论,

header
属性具有与类不同的事件代码(属性为“rhed”,类为“mhdr”) .这似乎是错误的实际原因(
header
编译为
«class mhdr»
)并提供了一个潜在的解决方案:您可以使用原始事件代码指示符来获取此特定情况下的属性。

tell application "Mail"
    «property rhed» of first rule condition of first rule
end

但是,第一次保存脚本时,原始代码被名称替换,第二次保存时名称被重新解释为类而不是属性,要求您更正每次出现的属性名称每次保存。可以通过定义处理程序以在使用 Mail.app 条款的块之外获取属性来减少要完成的工作量。

on hdr(rc)
    return «property rhed» of rc
end hdr
tell application "Mail"
    my hdr(first rule condition of first rule)
end

通过在不使用 Mail.app 条款的位置定义处理程序,原始代码不会被标识符替换。

这仅部分解决了保存-编辑问题,因为每个单独的属性都需要自己的处理程序,而且(更糟糕的是)处理程序似乎无法在 过滤器形式 中工作(尽管可以使用循环代替)。因此,我不能接受这个答案,如果有人能找到一个完整的解决方案,我将不胜感激。例如,以下

on hdr(rc)
    return «property rhed» of rc
end hdr
tell application "Mail"
    every rule condition of first rule where my hdr(it) is not ""
end

结果:

error “邮件出错:无法获取标题。”标题中的数字 -1728


0
投票

使用rule condition tell block和里面的关键字its,得到header.

tell application "Mail"
    tell rule condition 1 of rule 4
        set arbitraryHeader to rule type --> header key (constant)
        try
            set theHeader to its header --> "Reply-To" (string)
        on error
            set theHeader to missing value
        end try
        set theExpression to expression --> "Adobe Creative Cloud" (string)
        set theQualifier to qualifier --> does contain value (constant)
    end tell
end tell
© www.soinside.com 2019 - 2024. All rights reserved.