Search code examples
macosemailurlapplescriptautomator

Mac Automator/Applescript(?) to extract URL's from Mail messages


I'm trying to get a text document list of any links in a bunch of email messages that reside in the latest Mac Mail.app (OS X 10.10.2 Yosemite), and am simply stumped.

I thought I'd be able to just...

  1. Put a couple of Automator.app actions together in a Service/Workflow,
  2. Select/highlight all the email messages within the Mail.app,
  3. Select that Service or run that Workflow,
  4. And have a text document with every URL/link that could be found within them all.

However, this didn't work.

I figured out how to do this with one email message at a time, but that's not what I want. attached is a screenshot of 3 workflows. The first one is the one that works with just one email message & highlighting all the text in it & running the Service. the other two just simply don't work.

I also notice that the the first shows up in the Service Menu with a single email open; once I highlight more than one email message, the option goes away from the Service menu.

Any tips or tricks?

screenshot


Solution

  • I figured out how you could reach your goal, start with creating a new service within Automator (input: "No input", application: "Mail")

    The first action is Get Selected Mail Messages (Get selected: messages)

    The second action is Execute AppleScript with the following script:

    on run {input, parameters}
        set mailContentList to {}
        tell application "Mail"
            repeat with selectedMail in input
                set end of mailContentList to content of selectedMail
            end repeat
        end tell
        return mailContentList
    end run
    

    This script simply walks through the given messages, reads out the content and passes this list to the next action

    The third action is Extract URLs from Text. This is listed as "Extract Data from Text" and one of the types of data is "URLs".

    And the final action is New TextEdit Document

    Save it with a nice name like Extract URLs from selected mails After that the Service is available inside the Services menu inside the Mail app.

    In my test I found a few internal URLs without http:// from links to anchors, so maybe you want to delete all URLs that do not start with http. You can do so by using another action before creating the new TextEdit document:
    Filter Paragraphs with options "Paragraphs that start with http" (don't know how these parameters are called in English Automator, sorry)

    Feel good, Michael / Hamburg