Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open an email message using applescript?

I am writing a small applescript which retrieves all "unread" messages in the viewer and loops them.

I have two goals to complete:

  1. I need to get the subject of each message and perform a regular expression to see if it's suitable for step 2 (ex: get emails with subject {.*})

  2. I need to open each message on a separate window and after 4 seconds, I need to close that window and proceed with the next message

Do you know how to do these?

Thanks in advance.

like image 581
user48780 Avatar asked Oct 15 '22 17:10

user48780


2 Answers

The following applescript works for me, but I'm not sure how to do the regex matching. You can use the unix 'grep' function with applescript's 'do shell script' command, but I'm no expert in how to use grep properly. I'll leave that for someone else to answer.


on run
    tell application "Mail"
        set myInbox to mailbox "INBOX" of account 1
        set myMessages to every message of myInbox

        repeat with theMessage in myMessages
            if read status of theMessage is false then

                if my subjectIsInteresting(subject of theMessage) then
                    open theMessage
                    delay 4
                    close window 1
                end if

            end if
        end repeat

    end tell
end run

on subjectIsInteresting(subject)

    -- do some regex magic here

    return true -- for now

end subjectIsInteresting
like image 51
e.James Avatar answered Oct 19 '22 01:10

e.James


For regexes -- If you're running the script on your own machine, or can distribute it bundled, you could use Satimage's Smile extension (http://www.satimage.fr/software/en/downloads/index.html) which adds regexes to Applescript.

like image 38
iayork Avatar answered Oct 19 '22 03:10

iayork