Moving Text Between Drafts and DEVONthink To Go with Shortcuts

November 13, 2018 by Gabe | [mmd] |

I'm still looking for a convenient way to edit text files that I archive in DEVONthink To Go. While I love the organization and intelligent search in DEVONthink the built-in text editor is anemic. I developed a pair of Shortcuts that move text between Drafts and DEVONthink.

The first Shortcut is used when send text out of DEVONthink To Go and primarily deals with the mish-mash of data that comes along with the plain text on iOS. When DTTG shares a Markdown or text document it adds the file title that includes a unique ID. This ID is then used to update text when the file comes back into DEVONthink To Go.1

I've created to Shortcuts. One is simple and handles sending the file to Drafts from DEVONthink To Go. The other is longer and using the first line in Drafts as the title of a text file.

DTTG to Drafts

When DEVONthink To Go exports a file it has two different components it sends to the share-sheet. The first has the simple name of the text file. The second component has the name with an ID suffix added. We need this ID to synchronize the file when we import changes later.

Here's the Shortcut.

DEVONthink to Drafts

The first step is to grab the text objects exported from DEVONthink To Go. The file name with the ID suffix is (so far) always contained in the second item in the object list. The Shortcut grabs this title, the file extension, and the body of the text file and creates a new text object that is then sent to Drafts. This ends the Shortcut.

File to Drafts

Drafts to DTTG

After working on this text in Drafts, I want to update the file in DEVONthink To Go. iOS already has some major limitations for text editors accessing data stored in other apps. Drafts makes it even harder because it's not a file-based app. Drafts uses a database of text, so to get data out as a file is a bit trickier.

We're going to take advantage of a feature in DEVONthink To Go which is quite clever but really non-obvious. If you import a file using the "Copy to DEVONthink" sharing extension and that file is named the same and include the ID suffix mentioned above, the contents of the file will be updated. This avoids duplicate files scattered all around DEVONthink. It doesn't matter what database or group the file is in either, which also avoids tedious browsing.

Here's that Shortcut.

Drafts to DEVONthink

This Shortcut assumes that the original unique text file ID is on the first line of the Drafts text. We extract that by splitting all of the text in Drafts and then grabbing the first line (Item At Index 1) and assigning that to a variable named "FileName" which we will use a little later.

Next we grab the Shortcut input again and count the lines of text. This will come in handy in the very next section.

At this point we have three important pieces of data:

  1. The FileName in DEVONthink To Go that will be updated with the Drafts text
  2. The number of lines in the text coming from Drafts
  3. All of the other lines in the text coming from Drafts

In pseudo-code we need to now extract the lines of text that aren't the first line. That's why we counted up the total number of lines. We simply use all of the lines of text from line 2 to the last line (Count) of the Drafts text to make the text file to send to the sharing extension.

Let's continue...

The last block of actions starts by grabbing the Shortcut input again.

Finish Him

We split it into text lines again and then use the "Get Items from List" action to grab the text from line 2 until the end (which we stored as the "Count" in a previous step).

Now that we have a list of lines we combine them back into a single block of text.

Shortcuts has a funny little feature where you can treat an incoming block of text as if it were a file. This is not obvious. To do this you must assign that block of text a name using the "Set Name" action. The name we'll use is the FileName we extracted way back at the beginning. I guess in the Shortcuts philosophy, you don't have a file unless you name it.

The final step is to send this file to the share extension.

After triggering the share sheet in Drafts, we send it to Shortcuts, then select the "Drafts to DTTG" Shortcut. Finally, we end up back at the share extension draw and use the "Copy to DEVONthink" extension to update the document in DTTG.

Export from Drafts

Text Files

I'm both fascinated and frustrated by the way iOS and apps treat text files. These are the oldest file types there are and yet Shortcuts has very few useful actions for text files. The only real first class text file editor I have on iOS is 1Writer and it does not allow editing text files in place in DEVONthink To Go.

I think iOS is missing a TextEdit equivalent that works seamlessly with the Files app and the Notes app. Heck, just give the Notes app the ability to work with text files. By abstracting away the file system, iOS has thrown away some of the foundational benefits of most other operating systems: separating our data from the applications that work with them. This is not an original complaint but it is a persistent one.

iOS enforces the concept of applications as owners of data. In the case of plain text this comes with huge trade-offs. We must rely on either Shortcut acrobatics or the kindness of developers to provide an exit. Both of these are risks create for no obvious benefit.

If you like the complexity and time consumption of working with the Shortcuts app, you'll love working with text files on iOS.


  1. Kudos to DEVONthink for treating a file object a first class citizen on iOS. It's one of the reasons I stay with the app and pretty much have it open on one device or another most of the day. 

tags