Extract links from email based on the anchor text, not the href I am putting together an Automator workflow that will allow me to bind a hotkey to a Smart Mailbox listing and effectively automate the task of clicking "Unsubscribe" in emails which have that text string.

So far, I've been marginally successful if the actual URL has the word "unsubscribe" in it. What I would like to do, instead, is find where the word Unsubscribe appears and instead grab only those URLs. So in HTML terms, imagine I am finding a link by its text and then acting on the href

When I use content I get only the contents of the email but when I use source I get links. Basically I need a way to map the content to the source.

I hope that makes sense. This is what I have so far:

on run {input, parameters}
set theSource to {}
tell application "Mail"
    repeat with aMessage in input
        -- source or content
        set end of theSource to aMessage's source &amp; return
    end repeat
end tell

return theSource as text
end run
Automatically execute html in an email.

I'm not familiar with Smart Mailbox and what language it uses but below is a VBA script for Outlook that searches for the text "Click" which is a href to a URL, extracts the URL and executes it in Internet Explorer as a weblink.

The code is very messy with very little documentation but it uses the Split Array functionality of VBA to break out the lines of the email message and then extract the correct string after double quotes ""(Chr(34)) using array positioning ie. array urltag(1) - first element in array urltag.

Hope this helps someone.

LaunchURL(itm As MailItem)

Dim bodyString As String
Dim bodyStringSplitLine
Dim bodyStringSplitWord
Dim targetline
Dim urltag
Dim splitLine
Dim splitWord
Dim intCount As Integer
Dim Fail As Integer
Dim Beforecurrent As Integer
Dim Aftercurrent As Integer
Dim Found As Integer


Fail = 0
Found = 0

bodyString = itm.HTMLBody ' Mailitem.body vs Mailitem.HTMLBody  - oMailitem.body
bodyStringSplitLine = Split(bodyString, vbCrLf) 'vbCrLf has no meaning in HTML?

For Each splitLine In bodyStringSplitLine
    bodyStringSplitWord = Split(splitLine, " ")


    For intCount = LBound(bodyStringSplitWord) To UBound(bodyStringSplitWord)
    'For Each splitWord In bodyStringSplitWord ie find out how many iterations to do.

    If intCount &gt; 2 Then
    Beforecurrent = intCount - 1
    Aftercurrent = intCount + 2
    Else
    Beforecurrent = 1
    End If

        If bodyStringSplitWord(intCount) = "click" Then

            targetline = bodyStringSplitWord(Aftercurrent)
            urltag = Split(targetline, Chr(34))
           'i = MsgBox(urltag(1), vbOKOny) test box remove apostrophe to uncomment

            Shell ("C:\Program Files\Internet Explorer\IEXPLORE.EXE" &amp; " " &amp; urltag(1))
           'Shell ("cmd" &amp; " " &amp; "/k" &amp; " " &amp; "echo" &amp; " " &amp; bodyStringSplitWord(Aftercurrent) &amp; " " &amp; Fail &amp; " " &amp; Beforecurrent)   test box remove apostrophe to uncomment

        Found = 1

        Exit Sub
        Else:

        Fail = Fail + 1

        'Shell ("cmd" &amp; " " &amp; "/c" &amp; " " &amp; "echo boo hoo") Test box remove apostrophe to uncomment

        End If
    Next

Next

Set itm = Nothing

End Sub