繁体   English   中英

使用 Applescript 接收规则条件电子邮件

[英]Receive Rule Conditions Email Using Applescript

我使用applescript 将收到的邮件的一部分存储在数据库中。 这个脚本是通过特殊的邮件规则调用的。 它自几个月以来一直运行良好,但有一个例外:如果收件箱中的选择还包含不符合邮件规则标准的邮件,这些邮件也会传递给脚本(在我看来这是来自 Apple High Sierra 的错误) ) 因此,我必须自己将传输的数据记录与关联的规则进行比较。 测试脚本下方

using terms from application "Mail"
    on perform mail action with messages theSelectedMessages for rule theRule
            tell application "Mail"
                ...

                set ruleName to name of theRule
                set ruleScriptName to name of me

                repeat with theCondition in rule conditions of theRule

                    set {expression:ruleExpr, header:ruleHeader, rule type:ruleType, qualifier:ruleQualifier} to theCondition
                    log ...
                end repeat
            end tell
        end perform mail action with messages
    end using terms from

用户 SyncedRules.plist 中来自 Apple 的相关代码:

<dict>
    <key>CriterionUniqueId</key>
    <string>XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXXX</string>
    <key>Expression</key>
    <string>noreply@email.com</string>
    <key>Header</key>
    <string>From</string>
    <key>Qualifier</key>
    <string>EndsWith</string>
</dict>

问题:我总是收到以下规则条件的数据值:

ruleExpr: noreply@email.com
ruleType: 束constant ****tfro損
ruleHeader:
ruleQualifier: 束constant ****rqbw損

变量“ruleHeader”实际上应该包含值“from”但为空。 此外,“ruleType”和“ruleQualifier”的内容也不可读。

脚本编辑器的“函数库”没有帮助。

互联网上有很多提示可以添加新规则,但我没有找到任何文档或提示来接收规则条件的内容。 任何帮助表示赞赏!

这是我的问题的“ruleHeader”部分的解决方案:

            ...
                #set {expression:ruleExpr, header:ruleHeader, rule type:ruleType, qualifier:ruleQualifier} to theCondition

                tell theCondition
                    set ruleExpr to the expression
                    set ruleHeader to the header key
                    set ruleType to the rule type
                    set ruleQualifier to the qualifier
                end tell
            ...

返回参数的结果现在是正确的:

Type: 束constant ****tfro損
Header: 束constant erutthdk損
Qualifier: 束constant ****rqew損

分隔我使用的值(类型常量)

set commandType to rich text 15 thru 18 of (ruleType as string)
set commandHeader to rich text 11 thru 18 of (ruleHeader as string) 
set commandQualifier to rich text 15 thru 18 of (ruleQualifier as string)

并得到

commandType: tfro
commandHeader: erutthdk
commandQualifier: rqew

你可以在这里找到这些常量的解释: http : //ftp.icm.edu.pl/packages/Hacked%20Team.git/core-macos/core/RCSNativeMail.h

有几种方法可以获取应用程序常量的值。 一种是从脚本字典中获取常量和值并提供一种查找它们的方法,另一种是使用秘密(好吧,不是那个秘密)命令来加载用户术语。

下面的示例使用不同的方法显示常量 - 我将它们分开,以便您可以查看加载项时是否有任何延迟。 将其另存为应用程序,因为脚本编辑器已经加载了应用程序术语并且通常不使用原始常量:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

tell application "Mail"
    set ruleQualifier to qualifier of some rule condition of rule "News from Apple" -- example rule
    set ruleExpr to (get sender of some message of some mailbox) -- example sender from random message
    set match to "noreply" -- example text to compare

    set method to button returned of (display dialog "Choose method to look up terminology" buttons {"Load via 'ascrgdut'", "Expand Constant", "Raw constant"} default button 3)
    if method is "Expand Constant" then -- manually look up constant name
        display dialog "The supplied term:" & return & my expandQualifier(ruleQualifier) with title method buttons {"OK"}
    else
        if method is "Load via 'ascrgdut'" then -- load terminology to get the name of the enumerator
            try
                «event ascrgdut» -- may be a slight delay while loading
            end try
        end if
        display dialog "The supplied term:" & return & ruleQualifier as string with title method buttons {"OK"}
    end if
end tell

# You can also use something like 'run script' to perform operations based on the qualifier:
tell me to activate
set comparison to quote & ruleExpr & quote & space & expandQualifier(ruleQualifier) & space & quote & match & quote
display dialog "Attempted comparison:" & return & comparison & return & return & "The result:" & return & (my performComparison(ruleExpr, ruleQualifier, match)) with title "Comparison Result" buttons {"OK"}


on expandQualifier(theItem) -- look up comparison name/operator from constant - returns 'missing value' if not found
    set ruleQualifiers to {rqbw:"begins with", rqco:"contains", rqdn:"does not contain", rqew:"ends with", rqie:"equal to", rqlt:"less than", rqgt:"greater than"}
    set dict to current application's NSDictionary's dictionaryWithDictionary:ruleQualifiers

    set theItem to theItem as text
    if theItem ends with "»" then -- look up constant from raw chevron syntax
        set lookup to dict's objectForKey:(text -5 thru -2 of theItem)
    else
        set lookup to dict's objectForKey:theItem
    end if
    if lookup is not missing value then set lookup to lookup as text
    return lookup
end expandQualifier

to performComparison(theItem, operation, theValue)
    set lookup to expandQualifier(operation)
    if lookup is not missing value then
        try
            return (run script quote & theItem & quote & space & (lookup as text) & space & quote & theValue & quote)
        on error errmess number errnum
            return "Error:  " & errmess
        end try
    else
        return "Error:  the operation " & quoted form of (operation as text) & " was not found."
    end if
end performComparison

请注意,处理程序使用应用程序将获得的原始常量。 如果加载术语,您需要将查找记录/字典调整为您正在使用的术语。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM