1

I have an Applescript that attaches a bunch of spams to a reporting email flawlessly ONCE. Then it stops attaching any spams in subsequent runs regardless of whether it's the same batch of spams or another new batch of spams. Even though it stops attaching any spams in subsequent runs, it otherwise runs fine in that it creates the reporting email, addresses it and tacks on the subject line which gives a tally of spams being reported. I have a gut feeling that it's likely an aliasing issue (in that it's looking for the wrong alias and thus can't attach the spam) but I can't quite see how to fix it. I enclose the Applescript below with personal info redacted. Running it under Applescript Editor provides me no errors except for the "--> missing value" (what does this even mean?) after each iteration of the attachment loop. What am I overlooking?

-- 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

Here is a sample run of 35 spams using the corrected Applescript provide by regulus6633:

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"}

2 Answers2

0

I see a mistake here:

set fullSpamCopFolderPath to (theOutputFolderPath & SPAMCOP_FOLDER_NAME & ":")

It should be:

set fullSpamCopFolderPath to (theOutputFolderPath as text & SPAMCOP_FOLDER_NAME & ":")

Notice "as text". You want to add strings and theOutputFolderPath is not a string so you need to make it one before you add the string SPAMCOP_FOLDER_NAME to it.

Here's another error:

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

It should be:

set subject to (spamsProcessed as text) & " spam(s) being submitted for processing"

Again, "spamsProcessed" is a number and you need to make it a string before adding another string to it. Plus you're inside "tell spamReport" and therefore don't need "of spamReport".

Finally, you should move all of your "Mail" code outside the Finder block of code. It doesn't make sense to tell the Finder to tell Mail to do something and it often results in hard-to-find errors when you embed application tell blocks inside of each other.

So try the following. I fixed a few other small errors and removed all of your "as string" stuff because once you fix those errors you don't need it. I didn't try this code but it should work.

-- 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
regulus6633
  • 18,848
  • 5
  • 41
  • 49
  • Unfortunately, the corrected Applescript that you provided above also exhibits the same behavior as my original Applescript: It runs perfectly *ONCE*, at most *TWICE* (with spams attaching to the reporting email), then while everything else continues to run perfectly, it stops attaching spams on the third try whether run under Applescript Editor or just from the Menubar. I attach the output in the Events/Replies window of Applescript Editor to my original question in the hope that it might ring some bells for you because I'm just about wrung dry myself staring at it. – Kok-Yong Tan Sep 08 '14 at 19:01
  • I've just discovered something truly arcane: If I change the Applescript within Applescript Editor (e.g., by adding a new line that is basically a NOP or changing the email address to and from "REDACTED") and then run it, then the Applescript will start attaching spams again once or twice before ceasing to do just the attachment operation (every other operation works fine). I don't want to have to keep changing my Applescript back and forth and sideways just to get it to work once or twice. WTF is happening here???? This is driving me insane! – Kok-Yong Tan Sep 09 '14 at 01:29
  • Wait! I think I have a solution. And it's so simple that it's truly a "doh!" moment. But I need to test it further to see if my solution is really viable or whether it was just another fluke result. Stand by…I should have an result in the next couple of days when the spam wave crests again... – Kok-Yong Tan Sep 09 '14 at 01:41
  • Drat! That wasn't it. I thought if I moved the line: `set visible to true` to after the repeat loop, this would fix the issue because it was a visibility problem but it wasn't. I continue to experience the problem where the Applescript will work fine once to thrice and then stop attaching spams. Sigh. – Kok-Yong Tan Sep 09 '14 at 17:38
0

I solved the problem myself. The solution was just to replace this line:

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

with this:

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

As to why the former just worked once or twice and then not again thereafter, I have no idea. Theoretically, the former shouldn't have worked at all. If anybody has any feasible explantion, I'm all ears. But my code now works as expected.