Cocktails and Coffee Logo |||
Get Apple Mail Into Omnifocus Banner Image

Get Apple Mail Into Omnifocus

Yes, Omnifocus has a maildrop service where you can forward an email to a super-secret address and that email will show up as a task in your Omnifocus database. I used that for years, and still do on mobile sometimes.

But, when I’m sitting at my Mac, it seems silly to send a task over the internet back to myself. What I really want is a proper Share Sheet in the Mail app. It’s really dumb that after 16 years of iOS and 40 years of the Mac that there is no way to send parts of an email to other places in the computer. But I digress…

Luckily, there’s AppleScript.

To be absolutely clear, I am not good at AppleScript. This shortcut was built using a script I found and modified. I am good at Shortcuts, but still need to run the script twice in order to get the subject of the email and the URL of the email returned as separate variables. It can probably be done in one go, but again I’m not good at AppleScript.

The Shortcut

The Shortcut is super simple, only 5 steps. Keep in mind it only works on Mac because iOS doesn’t run AppleScript.

Step Image Description
1 Run AppleScript ActionRun AppleScript Action Step 1: Run Applescript. This will, uh, run an AppleScript.
2 Set Variable ActionSet Variable Action Step 2: Set Variable to the result of the script.
3 Run AppleScript ActionRun AppleScript Action Step 3: Run Applescript again. Same script, but returning the URL this time.
4 Set Variable ActionSet Variable Action Step 4: Set Variable to the URL
5 Add to Omnifocus ActionAdd to Omnifocus Action Step 5: Add Item to Omnifocus. Set the name of the task to the title variable and the note to the url variable.

That’s it. Running this with a message selected in Mail will create a task in Omnifocus that includes a link back to the message in the notes field. This message:// URL will find the message wherever it is (as long as it isn’t deleted), so you can move to a folder or archive it without worry.

The Applescript

There are two scripts to run that basically do the same thing. One grabs the message details and returns the Title, the other returns the URL.

tell application "Mail"
    set msgs to selected messages of front message viewer
    if (count of msgs) is 1 then
        set selMsg to item 1 of msgs
        set msgID to message id of selMsg
        set msgURL to "message:%3C" & msgID & "%3E"
        set msgTitle to subject of selMsg
        return "📧 " & msgTitle & ""
    else
        activate
        display alert "Selection" message "Please select a single Mail message." buttons {"OK"}
        error number -128
    end if
end tell

AppleScript is great in that it’s pretty readable, so lets step through this one quickly.

  1. tell” the application Mail to
  2. Set the variable msgs to the selected message of the frontmost window
  3. Count the number of selected messages. If it’s 1, Then set some variables based on the message metadata (title, ID, URL, etc)
  4. Reurn the text ✉️ + msgTitle
  5. If message count is not 1 (meaning you have multiple messages selected), it’ll error out.

Then repeat again to return the URL:

tell application "Mail"
    set msgs to selected messages of front message viewer
    if (count of msgs) is 1 then
        set selMsg to item 1 of msgs
        set msgID to message id of selMsg
        set msgURL to "message:%3C" & msgID & "%3E"
        set msgTitle to subject of selMsg
        return "" & msgURL & ""
    else
        activate
        display alert "Selection" message "Please select a single Mail message." buttons {"OK"}
        error number -128
    end if
end tell

I use this shortcut every day to send emails into my Omnifocus so I can deal with them later. I use the stock Apple Mail app on Mac and iOS, so these URLs work anywhere. On Mac, I have it mapped to a Streamdeck button and it works flawlessly in the background.

You can get the Shortcut here

UPDATE

I posted this just a couple hours ago and have already improved it. By getting the script to return the email’s title and URL in a single string, I can then use the Split Text” action to break that into two variables, saving running the AppleScript a second time. Not sure why I didn’t think of that before, I use Split Text all the time! ¯_(ツ)_/¯

The new AppleScript is:

tell application "Mail"
    set msgs to selected messages of front message viewer
    if (count of msgs) is 1 then
        set selMsg to item 1 of msgs
        set msgID to message id of selMsg
        set msgURL to "message:%3C" & msgID & "%3E"
        set msgTitle to subject of selMsg
        return "📧 " & msgTitle & "^"& msgURL &""
    else
        activate
        display alert "Selection" message "Please select a single Mail message." buttons {"OK"}
        error number -128
    end if
end tell

I use the ^ character to merge the title+URL string, then set that in the Split Text action to break it back apart. The new Shortcut technically has more steps, but is cleaner. I can’t tell any difference in the speed, it’s always instant in both cases.

The New ShortcutThe New Shortcut

You can get the new version here

🗓️ January 27, 2023 🏷️ Omnifocus 🏷️ Shortcuts 🏷️ Automation
CC BY-NC-SA 4.0 © 2007-2023 Jimmy Little Support me on Ko-fi