Apple Script not running as expected

applescriptautomatormail.app

The following is an applescript which is meant to take the body of the latest email and extract text out of it. As you can see the result is shown below. This result is show as the script was running correctly before. Now it is not.

The following is an applescript which is meant to take the body of the latest email and extract text out of it. As you can see the result is shown below. This result is show as the script was running correctly before. Now it is not.

The email I sent later on contained the message "#tech sxi" but the script is not showing the correct output for this email. This is not the only one. Every time, I am sending a new mail and running the script again, i get the same problem, the result just does not change.

Best Answer

As one of the commenters alluded to, "get first message of inbox" probably won't return what you expect.

When retrieving messages using AppleScript, Mail sorts your mailbox accounts alphabetically. Within each account's inbox, messages are sorted newest first. What you have selected in Mail doesn't change the order in which they're returned to AppleScript.

So if you have the following accounts and inbox messages appearing in Mail in this order:

  • My iCloud
    • Message dated 1/5/16
    • Message dated 1/10/16
  • My Gmail
    • Message dated 1/10/16
    • Message dated 1/5/16

"get first message of inbox" will return the Message dated 1/5/16 from the "My Gmail" account (G comes before i, 1/5/16 comes before 1/10/16).

My guess is that you have more than one account in Mail.

  • Name the specific mailbox. This will always set theMessage to the newest message in "My Gmail". (The account name is the same name you see in Mail in the list under Inbox).

    set theMessage to first message in mailbox "INBOX" of account "My Gmail"
    

This is an unusual approach, however, as mail moves, so the first message in your mailbox could become your second message unexpectedly.

Here are a couple of reliable ways to extract data from messages:

  • Use selected messages (messages you've selected by clicking on them in Mail):

    tell application "Mail"
        set theMessages to selection
    
        -- This block extracts the topics from the messages
        repeat with theMessage in theMessages
            set theMessageText to content of theMessage
            try
                -- Set topic to the string starting just after the hash
                set topic to rich text ((offset of "#" in theMessageText) + 1) thru end of theMessageText
                -- Truncate the topic at the first space
                set topic to rich text 1 thru ((offset of " " in topic) - 1) of topic
            on error
                -- No topic found in message
                set topic to ""
            end try
            log topic
    
        end repeat
    end tell
    
  • Mail Rules (run a script on messages that match certain criteria). In Mail, open Preferences > Rules. Create a new rule with "Run AppleScript" as the action. Select "Open in Finder" from the menu that lets you select which script to run. Open Script Editor, and save the code below as a script in the folder Mail opened. Back in Mail, select your saved script as the script to run:

    using terms from application "Mail"
        on perform mail action with messages theMessages for rule thisRule
            -- This block extracts the topics from the messages
            repeat with theMessage in theMessages
                set theMessageText to content of theMessage
                try
                    -- Set topic to the string starting just after the hash
                    set topic to rich text ((offset of "#" in theMessageText) + 1) thru end of theMessageText
                    -- Truncate the topic at the first space
                    set topic to rich text 1 thru ((offset of " " in topic) - 1) of topic
                on error
                    -- No topic found in message
                    set topic to ""
                end try
                log topic
            end repeat
    
        end perform mail action with messages
    end using terms from