简体   繁体   English

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

[英]Receive Rule Conditions Email Using Applescript

I use an applescript to store parts of incoming mails in a database.我使用applescript 将收到的邮件的一部分存储在数据库中。 This script is called via special mail rules.这个脚本是通过特殊的邮件规则调用的。 It works perfectly since months with one exception: If the selection in the INBOX contains additionally mails which doesn't fit the criteria of the mail rule, these mails are also passed to the script (which in my eyes is a bug from Apple High Sierra) Therefore, I have to compare the transferred data records with the associated rule myself.它自几个月以来一直运行良好,但有一个例外:如果收件箱中的选择还包含不符合邮件规则标准的邮件,这些邮件也会传递给脚本(在我看来这是来自 Apple High Sierra 的错误) ) 因此,我必须自己将传输的数据记录与关联的规则进行比较。 Below the test script测试脚本下方

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

The associated code from Apple in the Users SyncedRules.plist:用户 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>

Problem: I always receive following data values for the rule conditions:问题:我总是收到以下规则条件的数据值:

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

The variable "ruleHeader" should actually contain the value "from" but is empty.变量“ruleHeader”实际上应该包含值“from”但为空。 In addition, the contents of "ruleType" and "ruleQualifier" are also not readable.此外,“ruleType”和“ruleQualifier”的内容也不可读。

The "function library" of the Script editor doesn't help.脚本编辑器的“函数库”没有帮助。

There are a lot of hints in the internet to add a new Rule but I didn't find any docs nor tipps to receive the content of a rule condition.互联网上有很多提示可以添加新规则,但我没有找到任何文档或提示来接收规则条件的内容。 Any help is appreciated !任何帮助表示赞赏!

Here is the solution for the part "ruleHeader" of my question:这是我的问题的“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
            ...

The result of the return parameters are now correct:返回参数的结果现在是正确的:

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

To separate the values (type constants) I use分隔我使用的值(类型常量)

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)

and get并得到

commandType: tfro
commandHeader: erutthdk
commandQualifier: rqew

You can find the explanation of these constants here: http://ftp.icm.edu.pl/packages/Hacked%20Team.git/core-macos/core/RCSNativeMail.h你可以在这里找到这些常量的解释: http : //ftp.icm.edu.pl/packages/Hacked%20Team.git/core-macos/core/RCSNativeMail.h

There are a couple of ways to get the value of an application constant.有几种方法可以获取应用程序常量的值。 One is to get the constants and values from the scripting dictionary and provide a way to look them up, and another is to use a secret (well, not that secret) command to load user terminology.一种是从脚本字典中获取常量和值并提供一种查找它们的方法,另一种是使用秘密(好吧,不是那个秘密)命令来加载用户术语。

The following example displays the constants using different methods - I kept them separate so you could see if there is any delay when loading terms.下面的示例使用不同的方法显示常量 - 我将它们分开,以便您可以查看加载项时是否有任何延迟。 Save it as an application, since the Script Editor already loads the application terminology and normally doesn't use the raw constants:将其另存为应用程序,因为脚本编辑器已经加载了应用程序术语并且通常不使用原始常量:

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

Note that the handlers use the raw constants that an application would be getting.请注意,处理程序使用应用程序将获得的原始常量。 If loading the terminology you will need to adjust the lookup record/dictionary to the terms you are using.如果加载术语,您需要将查找记录/字典调整为您正在使用的术语。

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

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