Getting DevonThink text item content (for Keyboard Maestro / Clipboard automation)


I suspect that what I want to do is a lot easier than I’m finding it to be, so any pointers to The Obvious would be most appreciated.

For the last few years I had been using a DevonThink database full of plain text items as the source of content for some email automation macros I wrote in Keyboard Maestro. That was working great, until DT changed the location of where files were kept on the filesystem – likely a part of the improved CloudKit synchronisation. I’m sure that what I was doing in the past was Wrong anyway, so, I’d like to learn how to do it Right.

I’ve read through both the KM and DT forums and haven’t found any recent articles that seemed to cover this trivial need, so apologies if I’m just missing it.

My goal: I want to grab the (always plain text) content from a DevonThink item in Keyboard Maestro and paste it in to an email. I have around 100 macros in KM, each of which would need to get the contents of a specific DevonThink item unique for that macro (yes, this was a pain to set up, but, once done, it’s done).

My approach: Until the most recent DT, I did this by using KM to “Read File to Named Clipboard” and specified (for each macro) the exact location on my macOS machine of the text content of the DT item. I would then Paste from that named clipboard, and I was good to go.

Why it “broke” for me: (Again, I am sure I was never doing this the right way) With a recent upgrade, the location of the files on the filesystem seemingly moved to a /.dtbase2/Files.noindex/txt//NameOfFile.txt location. Since there are no more than about 5 files per I realised that I probably shouldn’t count on this value being static. There’s also a likely bug with iOS such that finding DT items is now almost impossible, as the Shortcuts app seems to have a hard time searching for items using its native searching capability. (I was able to work around this and solve the problem more intelligently by using the new DT To Go shortcut items to Get the item with x-devonthink-item UUID links, I think, but, this is still work in progress. I have similar automation that I need to build to work with Preside on iOS that used to work fine, but, broke with the new DevonThink ToGo).

I tried to tell Keyboard Maestro to get the content of a DevonThink Item URL, but, that definitely did not work, so, my guess is that this will require some Apple Script if I’m going to do this.

Again, all I want to do is grab a DT Item and put it on a named clipboard, which really boils down to the right/best way to just grab the contents of a plain text DevonThink item in general.

If there’s a cunning way to do this with KM such that I can just point to the “link” of an item in DevonThink, that’d be great, but, if this requires AppleScript, then, if someone has an example they can point to of how to do what must be insanely easy, I’d appreciate it.

Thank you!

Welcome @dotsplatslash

until DT changed the location of where files were kept on the filesystem

DEVONthink didn’t make any change to the location of imported files in a database. They’ve always been stored internally in the Files.noindex folder. Do note you shouldn’t be messing about in the internals of a DEVONthink database.

I don’t use KM but this action will grab the plain text of a currently selected file and put it on the clipboard…

tell application id "DNtp"
	set theRecord to get record with uuid "E4462E63-D90D-4E36-A7B5-E96C7B9FD091"
	set thePlainText to plain text of theRecord
end tell

Identify record uuids by copying the link of the record
Standard Keyboard Maestro code to copy to a variable (sorry, I don’t use it)

1 Like

You could continue to use your current macros if you index the records. An indexed record’s path never changes.

Hovever, it’s easier to use the following setup.

Macro 1

  • Create a macro with an Execute AppleScript action and this script.
    Save the result to your named clipboard.
    This macro is used in the second macro.
-- Get Plain Text via Reference URL

tell application id "com.stairways.keyboardmaestro.engine"
	set theRefURL to getvariable "theRefURL"
end tell

tell application id "DNtp"
		set theUUID to (characters 21 thru -1 in theRefURL) as string
		set theRecord to (get record with uuid theUUID)
		set theText to plain text of theRecord
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
	end try
end tell

Macro 2 (thru X)

For each record whose plain text you need:

  • Create a macro with
    • an Set variable to text action
      Set this action to the copied Reference URL (menu Edit > Copy Item Link )

    • an Execute macro action
      Set this action to execute Macro 1

1 Like


If indeed you’re using specific item links, then @DTLow’s simple approach would certainly be useful (though it’s up to you to ensure it’s a proper format since there’s no error trapping. Minor detail).

My example was geared toward selected items. Apologies if I misunderstood your initial post.

Wow - outstanding all, thank you so much!

You’re quite right that I shouldn’t be mucking about with the internals @bluefrog and the “hack” I had used for the last few years was always bound to be an issue - it worked great for as long as it did, but, I really did need to do this correctly.

Both pete31 and DTLow have great suggestions and I’m experimenting with them both; happy to say, things are working just as I need.

Thank you again!

1 Like

Oh sure - you’re just ignoring my beautiful example…