A Siri Shortcut to Share as a Markdown File

October 24, 2018 by Gabe | [mmd] |

The Problem

I'm a big fan of Drafts for iOS. It's a terrific writing environment and it can integrate with just about anything through URL schemes. But, URL schemes have a major limitation when I write long blocks of text. A URL can only support a finite number of characters (around 2000).

I want to export text from Drafts and send it to other apps as a Markdown file. If I have a markdown header line with a "title" defined I want the file to be named with that title.

I do this often and it has become a time suck to use the clipboard as the only way to share more than 2000 characters.

Here's a typical example in Drafts:

Drafts Export

When I use a Shortcut like the excellent DEVONmenu by Federico, my text is truncated and there's no indication of what I've lost. I hate losing text so I decided to write a Shortcut that converts a text block to a Markdown file and attempts to auto-title it based on Markdown front-matter.

Regular Expressions in Shortcuts

Shortcuts has a couple of actions that are poorly documented but extremely powerful. They are the "Match Text", "Replace Text", and "Get Group from Matched Text." They give the user super powers when working with text.

Regex

But much like the suit from Greatest American Hero, they come with so little instructions that it's almost hazardous to work with these actions. Let's break these three actions down very quickly.

The Match Text action is the star. It does exactly what you think. It matches incoming text against a regular expression. It passes the match groups through. The Match Text action does not respond to the '/m' flag to treat the incoming text as multi-lines.1 I'll show a work around for this later.

The Replace Text action uses a regular expression to do a find and replace. I'm not using this action for today's post but it works much like the Match Text.

The final action is strange. The Get Group from Matched Text has access to the groups found with the Match Text action. If you don't know what match groups are, there are plenty of primers. Regular expressions aren't just for pulling out the exact match. Sometimes you want to extract just a portion of a match. I'll show an example of where this helps. The Shortcuts action can retrieve a match group by index which is nice.

I have a Shortcut that let's me play around with regular expressions. I paste text into a text action and then tweak the regex to see the results.

Regex Tester

For actual regex testing I highly recommend Regexr.com.

Regexer

The Shortcut

The Shortcut starts by splitting the incoming text into lines. I do this because the Match Text action doesn't support the multi-line option. By splitting the text I can operate over each line in the text file and look for anything that looks like a "title" header line.

Shortcut

I then loop over every line and use the following regular expression:

(?:^\btitle: \b)(.*)$

In simple pseudo code this says "find a line that starts with the word 'title: ' and then extract everything after that."

The first set of parentheses starts with a '?' which makes it a non-capturing group. That part of the match is thrown away. The second set of parentheses says to capture everything after the first group up to the end of the line.

At this point the Shortcut as either found a match or it hasn't. We try to grab the captured group text with the Get Group from Matched Text and then count how many items we have.

If we have less than one item then the text didn't have a title header line and it asks us to enter one with a popup text box (Ask for Input). We set a variable named Title that we can use to name this new file.

If the Matched Text count is greater than zero then we fall into the Otherwise statement. Here we can grab the string returned by the Group from Matched Text and use that to set the Title variable.

So now we should have a name for our file. We use the "magic variable" to recall the original input for the Shortcut (you remember, that big long block of text we shared from Drafts). The Set Name action adds a file name to that block of text and appends the ".md" extension.

To get an actual file, we trigger an "Open In" action which we can now use to send the file object into another app like DEVONthink To Go.

Clip to DTTG

Retrospective

I tried a heck of a lot of other things to accomplish this transformation. I tried using JavaScript to encode the text as a base64 string I could handle like any other text file. That failed when sending to DEVONthink's URL scheme.

I also tried to convert the text block into a base64 string with a Shortcuts action. That works but then I couldn't use it as a file object with any other apps.

It wasn't until I stumbled on the unique ability of Shortcut's "Open In" action to push out a file object did I realize I had a solution. The documentation for Shortcuts is bad. As with many of Apple's most powerful tools, Shortcuts relies on accidental discovery and many hours of trial and error. I don't think Shortcuts will be approachable until it has better discoverability and better documentation.

Thanks Siri


  1. It doesn't seem to respond to any flags at all. 

Older articles

  1. Lutron Switches, HomeKit Automation, and the Long Road Ahead

    October 07, 2018 by Gabe | [mmd] |

    With two HomePods in my house (so far) I have moved on to giving them the keys to as much as possible. My second step was to deploy Lutron smart switches to replace some Hue bulbs.

    Why Not Hue

    Hue bulbs are excellent. They are responsive, last a very long ...

  2. HomePod Multi-room Control

    August 02, 2018 by Gabe | [mmd] |

    The rough edges of HomeKit, Siri, and iOS show themselves a lot more now that I have multiple HomePods in the house. I have a strong suspicion that the teams behind these products aren’t designing them for a fully integrated and automated home. Let’s just take the case ...

  3. Troubleshooting a Stubborn Hue Hub

    June 25, 2018 by Gabe | [mmd] |

    Now that I've added a HomePod I'm finally getting around to configuring HomeKit with all of my devices. One Hue Bridge was no sweat but I have a second Bridge at a far end of the house. This Bridge refused to associate with HomeKit. I’ve left it ...

  4. Run Scripts on Your Mac from iOS [Link]

    May 20, 2014 by Gabe | [mmd] | ℳ↫

    From Moving Electrons comes a nice tip about executing remote commands over SSH from Pythonista. It's simple in design:

    1. Connects to the server.
    2. Executes the command passed as an argument to the script in Pythonista in the remote server (more info in the following section).
    3. Once the command is ...
  5. Auto-Mount Disks with ControlPlane

    March 10, 2014 by Gabe | [mmd] |

    That little green airplane in my menu bar means something important. It means my Macbook thinks it's sitting at my desk plugged into my Thunderbolt display and on Ethernet. It also means that my NAS remote disks are mounted and ready for action.

    Whenever I plug in my Thunderbolt ...

  6. IFTTT on iOS

    July 11, 2013 by Gabe | [mmd] |

    I've been beta testing the IFTTT iOS app for the past month and it really is great. I like it better than using the web interface for managing rules. The Macstories review is pretty definitive but I'll add one thing: The calendar view lets you see what rules ...

  7. Hazel Tutorial Screencast

    October 24, 2012 by Gabe | [mmd] |

    I've been meaning to highlight the Hazel tutorial over at MacOSXScreencasts.com for awhile. I wanted to make it all the way through the tutorial first. I have, and it's worth the $13 price tag.

    If you are new to Hazel or consider yourself a novice at automation ...