Devonthink - Hook to New...RTF?

Hi - I’m interested in Hook primarily to create links between Devonthink files and notes relating to those files. The “Hook to New…Devonthink” command is fantastic, but I would prefer my files to be RTF, rather than TXT. Would it be possible to add this functionality? Thanks!

1 Like

welcome to the Hook Productivity Forum, @jenniferc

In Hook > Preferences > Scripts > DEVONthink > you can replace the New Item DEVONthink script with:

tell application id “DNtp”
set newItem to create record with {name:“$title”, rich text:" ", type:rtf}
set refURL to reference URL of newItem
set itemPath to path of newItem
end tell
open location refURL
get refURL

i.e., change the “type” field to rtf, and add rich text:" " because DEVONthink requires that per: Error calling create record with {type:rtf} or {type:rtfd} - DEVONthink / Automation - DEVONtechnologies Community.

At some point, Hook should allow users to specify multiple “New Item” scripts per app, so that for a given app, you can choose using a pulldown menu which Hook to New script to use.

1 Like

Thank you, @LucB, for your swift answer.
I am in serious doubt, if my closely related suggestion deserves to become an independent thread.
I was thinking of exactly the same minor improvement as @jenniferc, but, apart from the RTF format, surely useful for many, I was thinking of having the additional option also of the MD format (if at all possible expanded), to be used with the resurected MultMarkDown Composer, which I really like.
Of course, the user majority might very well continue to prefer the TXT format, but why not have more choice?
So, my question, @LucB, is the following: would it be possible in
Hook → Preferences → DEVONthink either

[ol]
[li]to set a default format for (any) newItem; or[/li]
[li] (if no such default format is set) then ask, whenever a newItem is about to be created, whether one wants for this particular new item: [ul]
[li]the TXT format; or[/li]
[li]the RTF format; or[/li]
[li]the MD format (hopefully expanded)[/li]
[/ul][/li]
[/ol]
Do you think, @LucB, that such a script would be too difficult to create? Many thanks for considering this.

Thanks for asking, @adrian. Yes that should be possible and not too difficult. Hook currently offers an interactive script for Apple Notes. It presents a dialog box.

Internally, I recently requested an interactive script for Apple News. The reason we don’t have a built-in script for Apple News is that Apple News (sadly) does not have an API. We can get the URL of the ‘news’ item via UI Scripting, but Apple does not expose the title. (Honestly, Apple disappoints.) So we would prompt the user to enter a title for the news article, which they might have pasted in the clipboard. We’re not sure if that is what we should do. But I thought I should mention it in this context. (Also to get a bit of feedback.)

Why does none of the BBCode alternatives for lists writing function here? A bit frustrating.

Yes, @LucB, I think prompting the user to give a title to an AppleNews article, is, in fact, not a bad idea at all, as one could already then organise one’s material ans one pleases; what sums up the article best for the initial author (or for the editor, or for the the publisher) and which becomes the original title, might not at all be the most truthful way to label the same article in one’s personal view.
Talking about AppleNews, @LucB: do you know NetNewsWire? Brent Simmons and his collaborators are wonderful people, and, I guess, you would very much einjoy working with them. Just an idea.

1 Like

This is fantastic! Thank you, @LucB - and doubly so for responding so quickly. If I may make one more request - would it be possible to add a line to the script so that the new RTF (or text, or markdown) file is created in the same Devonthink group as the file to which it is linked? Thanks in advance for any assistance you can offer this non-script-savvy user.

1 Like

We would need to dig into the DEVONthink API for that. It would be doable if DEVONthink’s automation interface supports it. The trick would be to use the Hook’s $user_link variable in the New Item script for DEVONthink (in Hook > Preferences > Scripts > DEVONthink) — that variable resolves to the URL of the source resource that the new item will be linked to. (Hook to New creates a bidirectional link between a source and a new item). I suppose the script would need to

  1. check that the source scheme (i.e., of $user_link) is DEVONthink://
  2. ask DEVONthink (via API) what the group of the item at that URL is.
  3. after or while creating the new record in DEVONthink, ask (or specify to) DEVONthink that the new record should be in the same group.

Our DEVONthink dev is not currently available. Someone else on the forum who is more familiar with DEVONthink’s automation might be able to fill in the blanks or suggest an overall improvement — perhaps someone here or at DEVONthink.

I don’t know if we would want this to be the standard DEVONthink strict. I.e., I don’t know how widely people would want this, and we’d need to ensure it’s not brittle. But we can share it here on the forum for use. (Ideally alternative behavior would be user selectable without users needing to paste in code from the forum.)

Thanks again, @LucB! I will try posting in the Devonthink forum to see if someone there can help, too.

1 Like

version 132 of Hook’s integration scripts make RTF the new default for DEVONthink, as we now think this is the more commonly desired option. The other features, auto setting the Group and supporting multiple ‘hook to new’ options are still pending as they require updates to the Hook app, and we’re working on a collection of other features.

It’s nice to have both txt and rtf as options, but what about markdown? Do I understand correctly from the additional posts below that it’s not possible to generate a markdown note with “Hook to New”? Even if this isn’t possible, perhaps I can use “hook to new” to create a new markdown document using another application and a folder that’s indexed by DEVONthink (and then I could move the file whereever I wish in DT)?

First, currently, Hook supports only one “New Item” script per app. In the case of Hook to New > DEVONthink, the default script currently includes:

set newItem to create record with {name:"$title", rich text:"", type:rtf}

Pro users can edit the script, choosing whatever type they want. If Markdown is a type, that can be used.

Secondly, you could do that. But the URL scheme of the new item that is used by Hook to identify the file will be hook://file/. After moving the file from the indexed folder (essentially Finder) into DT (its database, which uses files but cannot strictly be thought of as Finder, unlike, say EagleFiler), when you invoke Hook on it, the “Get Address” script (and thus internal indexing) will use an x-devonthink-item URL.

Thirdly, if you then open the file externally from DEVONthink (e.g., in BBEdit) you’ll get the external URL scheme ( hook://file/). That might match the original hook://file/ URL, unless DEVONthink has done something to change the file in a way that is opaque to Hook (and macOS for that matter). E.g., it may have effectively copied the original file to a new name, and present the new file as if it were the original (because once a file has entered the DT realm, it is managed by DT; the macOS concept of “file” is no longer authoritative; DT owns its “reality”).

We have mentioned here the possibility of Hook supporting multiple IDs (or URLs) for the same resource which may be of use for some of these cases in the future. In fact, under the hood Hook some such functionality; but it’s not explicitly exposed to the user (e.g., there is no “get addresses” (plural) for a given item.)

Credit for this belongs here, but this is the script I use to create a new DT3 markdown item by placing it in the “new Item” tab:

tell application id "DNtp"
	set newItem to create record with {name:"$title", type:markdown, plain text:"[$title]($user_link)"}
	set refURL to reference URL of newItem
	set itemPath to path of newItem
end tell
open location refURL
get refURL
2 Likes

Thanks, @LucB, for this detailed response. I have tried to set the “New Item” script up to create a markdown document, but I haven’t been successful. I have tried various substitutions of “markdown” and “md” in this line of the script in place of “rich text” and “rtf” – but they don’t work. No new note is created.

set newItem to create record with {name:"$title", rich text:"", type:rtf}

If anyone has actually done this, I would appreciate suggestions!

?? Does the post above this Devonthink - Hook to New...RTF? - #12 by Rob not work for you?

1 Like

@Rob – Sorry I missed this post. I had read Luc’s response and fiddled around but didn’t see yours when I got back to the forum to respond.

Thanks @Rob! This script language worked perfectly!

As a friendly amendment, I added "# " ahead of the title information, so that the title within the note is styled as a level 1-header.

tell application id “DNtp”
set newItem to create record with {name:"$title", type:markdown, plain text:"# $title"}
set refURL to reference URL of newItem
set itemPath to path of newItem
end tell
open location refURL
get refURL

2 Likes

Is it possible to adjust the script to allow for “hook to new…. GROUP”?

Or does anyone have an alternative solution for creating a client file from an omnifocus project?