Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Applescript仅附加一次,然后不再附加_Applescript_Apple Mail - Fatal编程技术网

Applescript仅附加一次,然后不再附加

Applescript仅附加一次,然后不再附加,applescript,apple-mail,Applescript,Apple Mail,我有一个Applescript,它在一封报告邮件中完美地附加了一堆垃圾邮件。然后,无论是同一批还是另一批新的垃圾邮件,它都会在后续运行中停止附加任何垃圾邮件。即使它在随后的运行中停止附加任何垃圾邮件,它也可以正常运行,因为它创建了报告电子邮件,对其进行了寻址,并在主题行上进行了定位,从而提供了报告垃圾邮件的记录。我有一种直觉,它可能是一个别名问题,因为它正在寻找错误的别名,因此无法附加垃圾邮件,但我不太明白如何修复它。我随信附上以下已编辑的个人信息。在Applescript编辑器下运行它不会给我

我有一个Applescript,它在一封报告邮件中完美地附加了一堆垃圾邮件。然后,无论是同一批还是另一批新的垃圾邮件,它都会在后续运行中停止附加任何垃圾邮件。即使它在随后的运行中停止附加任何垃圾邮件,它也可以正常运行,因为它创建了报告电子邮件,对其进行了寻址,并在主题行上进行了定位,从而提供了报告垃圾邮件的记录。我有一种直觉,它可能是一个别名问题,因为它正在寻找错误的别名,因此无法附加垃圾邮件,但我不太明白如何修复它。我随信附上以下已编辑的个人信息。在Applescript编辑器下运行它不会给我带来任何错误,除了->缺少值这到底意味着什么?在附件循环的每次迭代之后。我忽略了什么

-- User settable constants
set SPAMCOP_ACCOUNT to "REDACTED"
set SENDER_ADDRESS to "REDACTED"
set SPAMCOP_FOLDER_NAME to "SpamCop"

-- Variable initialization
set rawSpamFileList to {} -- List of names of spam files
set spamsProcessed to 0 -- Number of spams processed
set theOutputFolderPath to path to desktop folder -- Desktop folder path
set fullSpamCopFolderPath to (theOutputFolderPath & SPAMCOP_FOLDER_NAME & ":") -- Spam folder on Desktop

tell application "Finder"

    -- Create a SpamCop folder on Desktop if there isn't already one
    if (exists folder (fullSpamCopFolderPath as string)) = false then
        make new folder at theOutputFolderPath with properties {name:SPAMCOP_FOLDER_NAME}
    end if

    -- Count number of Spams to be processed
    set spamsProcessed to number of items of folder (fullSpamCopFolderPath as string)

    -- Set the list of names to the raw source folder to loop through
    set rawSpamFileList to name of every file of folder (fullSpamCopFolderPath as string)

    if (spamsProcessed > 0) then
        tell application "Mail"
            -- Create a blank spam-reporting email & set sender in it
            set spamReport to (make new outgoing message with properties {visible:true, content:" ", sender:SENDER_ADDRESS})

            -- Address it and add a tally of junk being reported    
            tell spamReport
                make new to recipient at end of to recipients with properties {address:SPAMCOP_ACCOUNT}
                set subject of spamReport to ((spamsProcessed) & " spam(s) being submitted for processing" as string)
                set visible to true

                -- Attach all the spams in SpamCopFolder as attachment(s) to spamReport.
                -- I SUSPECT THE PROBLEM IS IN THE REPEAT LOOP BELOW BUT I CAN'T SEE WHAT'S WRONG!

                repeat with thisSpamName in rawSpamFileList
                    try
                        set fullSpamPath to ((fullSpamCopFolderPath as string) & thisSpamName) -- Cast fullSpamCopFolderPath alias to string first!
                        make new attachment with properties {file name:(fullSpamPath as string)} at after the last word of the last paragraph
                    on error errmsg
                        display dialog ("Failed with errmsg: " & (errmsg as string)) buttons {"OK"} default button "OK"
                    end try
                end repeat
            end tell
        end tell

        set responseButton to button returned of (display dialog "Clean up SpamCop folder now?" buttons {"Yes", "No"} default button "No")
        if responseButton is "Yes" then
            delete every item of folder (fullSpamCopFolderPath as string) -- Send spams to trash
        else
            display dialog "Spam deletion aborted." buttons {"OK"} default button "OK"
        end if
    else
        display dialog "No spam to process!  Are you hallucinating?" buttons {"OK"} default button "OK"
    end if
end tell
以下是使用regulus6633提供的更正后的Applescript运行35个垃圾邮件的示例:

tell current application
    path to desktop
        --> alias "Macintosh HD:Users:username:Desktop:"
end tell
tell application "Finder"
    exists folder "Macintosh HD:Users:username:Desktop:SpamCop:"
        --> true
    count folder "Macintosh HD:Users:username:Desktop:SpamCop:"
        --> 35
    get name of every file of folder "Macintosh HD:Users:username:Desktop:SpamCop:"
        --> {"Spam-20140908064824.eml", "Spam-20140908081508.eml", "Spam-20140908082049.eml", "Spam-20140908082642.eml", "Spam-20140908083224.eml", "Spam-20140908091214.eml", "Spam-20140908091848.eml", "Spam-20140908092708.eml", "Spam-20140908093615.eml", "Spam-20140908093946.eml", "Spam-20140908101749.eml", "Spam-20140908101834.eml", "Spam-20140908102327.eml", "Spam-20140908102809.eml", "Spam-20140908102920.eml", "Spam-20140908103417.eml", "Spam-20140908104041.eml", "Spam-20140908104110.eml", "Spam-20140908112201.eml", "Spam-20140908113458.eml", "Spam-20140908124138.eml", "Spam-20140908124750.eml", "Spam-20140908125605.eml", "Spam-20140908130207.eml", "Spam-20140908130508.eml", "Spam-20140908132133.eml", "Spam-20140908132909.eml", "Spam-20140908134147.eml", "Spam-20140908134736.eml", "Spam-20140908143459.eml", "Spam-20140908143618.eml", "Spam-20140908160051.eml", "Spam-20140908160448.eml", "Spam-20140908173043.eml", "Spam-20140908191450.eml"}
end tell
tell application "Mail"
    make new outgoing message with properties {visible:true, content:" ", sender:"REDACTED"}
        --> outgoing message id 47
    make new to recipient at end of every to recipient of outgoing message id 47 with properties {address:"REDACTED"}
        --> to recipient 1 of outgoing message id 47
    set subject of outgoing message id 47 to "35 spam(s) being submitted for processing"
    set visible of outgoing message id 47 to true
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908064824.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908081508.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908082049.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908082642.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908083224.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908091214.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908091848.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908092708.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908093615.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908093946.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908101749.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908101834.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908102327.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908102809.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908102920.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908103417.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908104041.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908104110.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908112201.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908113458.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908124138.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908124750.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908125605.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908130207.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908130508.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908132133.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908132909.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908134147.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908134736.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908143459.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908143618.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908160051.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908160448.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908173043.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
    make new attachment with properties {file name:"Macintosh HD:Users:username:Desktop:SpamCop:Spam-20140908191450.eml"} at after last word of last paragraph of outgoing message id 47
        --> missing value
end tell
tell application "AppleScript Editor"
    display dialog "Clean up SpamCop folder now?" buttons {"Yes", "No"} default button "No"
        --> {button returned:"No"}
    display dialog "Spam deletion aborted." buttons {"OK"} default button "OK"
        --> {button returned:"OK"}
end tell
Result:
{button returned:"OK"}
我在这里看到一个错误:

set fullSpamCopFolderPath to (theOutputFolderPath & SPAMCOP_FOLDER_NAME & ":")
应该是:

set fullSpamCopFolderPath to (theOutputFolderPath as text & SPAMCOP_FOLDER_NAME & ":")
set subject to (spamsProcessed as text) & " spam(s) being submitted for processing"
以文本形式通知。您要添加字符串,但OutputFolderPath不是字符串,因此在向其添加字符串SPAMCOP_FOLDER_名称之前,需要将其设置为字符串

还有一个错误:

set subject of spamReport to ((spamsProcessed) & " spam(s) being submitted for processing" as string)
应该是:

set fullSpamCopFolderPath to (theOutputFolderPath as text & SPAMCOP_FOLDER_NAME & ":")
set subject to (spamsProcessed as text) & " spam(s) being submitted for processing"
同样,spamsProcessed是一个数字,在添加另一个字符串之前,需要将其设置为字符串。另外,你在里面告诉spamReport,因此不需要spamReport

最后,您应该将所有邮件代码移到Finder代码块之外。告诉查找程序让邮件做某事是没有意义的,当您将应用程序tell块嵌入到彼此内部时,通常会导致难以找到错误

因此,请尝试以下方法。我修复了其他一些小错误,并删除了所有as字符串内容,因为一旦修复了这些错误,就不需要它了。我没有尝试这个代码,但它应该可以工作

-- User settable constants
set SPAMCOP_ACCOUNT to "REDACTED"
set SENDER_ADDRESS to "REDACTED"
set SPAMCOP_FOLDER_NAME to "SpamCop"

-- Variable initialization
set rawSpamFileList to {} -- List of names of spam files
set spamsProcessed to 0 -- Number of spams processed
set theOutputFolderPath to path to desktop folder -- Desktop folder path
set fullSpamCopFolderPath to ((theOutputFolderPath as text) & SPAMCOP_FOLDER_NAME & ":") -- Spam folder on Desktop

tell application "Finder"

    -- Create a SpamCop folder on Desktop if there isn't already one
    if not (exists folder fullSpamCopFolderPath) then
        make new folder at theOutputFolderPath with properties {name:SPAMCOP_FOLDER_NAME}
    end if

    -- Count number of Spams to be processed
    set spamsProcessed to number of items of folder fullSpamCopFolderPath

    -- Set the list of names to the raw source folder to loop through
    set rawSpamFileList to name of every file of folder fullSpamCopFolderPath
end tell

if (spamsProcessed > 0) then
    tell application "Mail"
        -- Create a blank spam-reporting email & set sender in it
        set spamReport to (make new outgoing message with properties {visible:true, content:" ", sender:SENDER_ADDRESS})

        -- Address it and add a tally of junk being reported    
        tell spamReport
            make new to recipient at end of to recipients with properties {address:SPAMCOP_ACCOUNT}
            set subject to (spamsProcessed as string) & " spam(s) being submitted for processing"
            set visible to true

            -- Attach all the spams in SpamCopFolder as attachment(s) to spamReport.
            -- I SUSPECT THE PROBLEM IS IN THE REPEAT LOOP BELOW BUT I CAN'T SEE WHAT'S WRONG!

            repeat with thisSpamName in rawSpamFileList
                try
                    set fullSpamPath to fullSpamCopFolderPath & thisSpamName
                    make new attachment with properties {file name:fullSpamPath} at after the last word of the last paragraph
                on error errmsg
                    display dialog ("Failed with errmsg: " & errmsg) buttons {"OK"} default button "OK"
                end try
            end repeat
        end tell
    end tell

    set responseButton to button returned of (display dialog "Clean up SpamCop folder now?" buttons {"Yes", "No"} default button "No")
    if responseButton is "Yes" then
        tell application "Finder"
            delete every item of folder fullSpamCopFolderPath -- Send spams to trash
        end tell
    else
        display dialog "Spam deletion aborted." buttons {"OK"} default button "OK"
    end if
else
    display dialog "No spam to process!  Are you hallucinating?" buttons {"OK"} default button "OK"
end if

我自己解决了这个问题。解决方案就是替换这一行:

make new attachment with properties {file name:(fullSpamPath as string)} at after the last word of the last paragraph
为此:

make new attachment with properties {file name:fullSpamPath as alias} at after the last word of the last paragraph

至于为什么前者只工作了一两次,之后就不再工作了,我不知道。理论上,前者根本不应该起作用。如果有人有任何可行的解释,我洗耳恭听。但我的代码现在可以正常工作。

不幸的是,您上面提供的更正后的Applescript也显示出与我原来的Applescript相同的行为:它完美地运行了一次,最多两次,垃圾邮件附在报告电子邮件上,而其他一切都继续完美地运行,无论是在Applescript编辑器下运行还是仅从菜单栏运行,它都会在第三次尝试时停止附加垃圾邮件。我将Applescript编辑器的事件/回复窗口中的输出附加到我的原始问题上,希望它能为您敲响一些警钟,因为我盯着它看的时候几乎都快累死了。我刚刚发现了一件真正神秘的事情:如果我在Applescript编辑器中更改Applescript,例如。,通过添加一个基本上是NOP的新行,或者将电子邮件地址更改为“编辑”和“编辑”,然后运行它,Applescript将再次开始附加垃圾邮件一次或两次,然后停止仅执行附加操作,每隔一次操作即可。我不想为了让Applescript运行一两次而不断地来回和横向地更改它。WTF正在这里发生吗????这快把我逼疯了!等待我想我有一个解决办法。这是如此简单,这是一个真正的doh!片刻但我需要进一步测试,看看我的解决方案是否真的可行,或者它是否只是另一个侥幸的结果。待命…我应该在接下来的几天里有一个结果,当垃圾邮件再次达到高潮时…该死!不是这样的。我认为如果在重复循环之后将line:set visible移动到true,这将解决问题,因为这是一个可见性问题,但事实并非如此。我继续遇到这样的问题:Applescript可以正常工作一到三次,然后停止附加垃圾邮件。叹气