0

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?

  • I don't know anything about Applescript, but I do know that indiscriminately opening links in email messages is a dangerous thing. I hope Apple doesn't let you do this. – RobJarvis Jun 09 '21 at 18:37
  • I am not indiscriminately opening links in email, I am specifying the link by using "starts with". If you note, I mentioned the "unsubscribe link" as a link I do not want to open. Otherwise I am selecting a specific email from a vetted service. Inside those emails is the link to service's page that contains the content I need to review. – Richard Osborne Jun 09 '21 at 20:44

1 Answers1

0

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
Robert Kniazidis
  • 1,760
  • 1
  • 7
  • 8
  • Thank you for your input Robert. – Richard Osborne Jun 09 '21 at 21:12
  • I have tested your script (with my URL specified) by adding it into my Mail rule, but It is not launching the web page in Safari when new emails come in. Mail's rules are fairly straight forward, but would there be something different for an applescript being called from a rule? – Richard Osborne Jun 09 '21 at 21:22
  • My fault. Comment, or delete code line **tell application "Mail" to set theMessages to (get selection)**. I used it to debug the rule, but as rule it may cause problems. Other problem may be **open location** command which is inside **using terms from application "Mail"** block. Try to put **tell application "Safari" to open location i** inside separate handler. You can also try in form **tell scripting additions to open location i**. Of course, I have not your messages, so check yourself the i is valid URL address. – Robert Kniazidis Jun 10 '21 at 03:23
  • I was right. Just now I tested script with **tell application "Safari" to open location i** as handler and script worked. See my UPDATED original answer. – Robert Kniazidis Jun 10 '21 at 03:48
  • Thank you again Robert. I have been testing the script and been frustrated by the fact that it only works some of the time. Within Mail I have the rule perform that following actions: • move message to mailbox • set color of background • then run the applescript The result is that all emails look like they have been processed (in folder and coloured background) but only seemingly random ones are. Any suggestions on how to trouble shoot the applescript? – Richard Osborne Jun 16 '21 at 16:13