Open links sent in email using AppleScript

402 views Asked by At

I am trying to automate the process of opening specific links that are sent by email.

The email has multiple links in it including one to unsubscribe from the service, so opening the correct link and not all links is important.

The process flow would be:

  1. Email comes in and triggers rule
  2. Rule looks for two parameters: email from address and key phrase in email body (not the link)
  3. Rule moves email to a specific mailbox and then triggers AppleScript
  4. AppleScript searches for the start of a specific URL and opens it in browser (I manually review open browser tabs at my convenience during the day)
  5. Email marked as read

This is what I have that is not working correctly:

using terms from application "Mail"
    on perform mail action with messages theMessages for rule theRule
        repeat with eachMessage in theMessages
            tell application "Mail" to set t to paragraphs of the content of eachMessage
            repeat with i in t
                if i starts with "http://www.website.com/specific/link" then tell application "Safari" to open location i
            end repeat
        end repeat
    end perform mail action with messages
end using terms from

Any suggestion on what I can do to make this process work?

1

There are 1 answers

5
Robert Kniazidis On

You should get AppleScript paragraphs instead of Mail.app paragraphs:

using terms from application "Mail"
    on perform mail action with messages theMessages for rule theRule
        repeat with eachMessage in theMessages
            tell application "Mail" to set t to (content of eachMessage) as string -- EDITED
            set t to paragraphs of t -- ADDED
            repeat with i in t
                if i starts with "https://stackoverflow.com/questions/67909907/open-links-sent-in-email-using-applescript" then openLocation(i)
            end repeat
        end repeat
    end perform mail action with messages
end using terms from

on openLocation(theURL)
    tell application "Safari" to open location theURL
end openLocation