Customizing Hook integration with DEVONthink

  1. Home
  2. Docs
  3. Help & Support
  4. Integration
  5. Customizing Hook integration with DEVONthink

Note: Custom integration with DEVONthink requires DEVONthink Pro or DEVONthink Pro Office, basic DEVONthink does not have the required Applescript support.

DEVONthink stores its documents as ordinary files organized in folders. Hook is out-of-the-box compatible with DEVONthink, but it treats links to DEVONthink documents as links to regular files. That means if you link to a document in DEVONthink, Hook will open it with the default program for the document type (e.g. Preview for pdfs or TextEdit for rtfs) instead of opening it with DEVONthink.

It might make sense for links from DEVONthink to open in DEVONthink. We can modify how Hook handles DEVONthink to do this, by writing custom integration scripts.

DEVONthink doesn’t have any scripts, so they have to be written from scratch, not just modified. In Preferences>Script Editor, click the + button and add DEVONthink to apply custom scripts to it. Now we have to write 3 integration scripts, Get Name, Get Address, and Open Item.

Get Name

tell application id "DNtp"
    if class of window 1 is document window then
        return the name of window 1
    else if class of window 1 is in {viewer window, search window} then
        set selected_items to selection of window 1
        if (count of selected_items) = 1 then
            set the_item to item 1 in selected_items
            return name of the_item
        end if
    end if
end tell

Hook is always invoked on the frontmost window, which is window 1. This script has to handle two cases: If a document is open in a “document” window, it just has to get the name of the window, if the document is selected in a viewer or search window, it has to get the name of the selection.

Get Address

tell application id "DNtp"
    if class of window 1 is document window then
        return the reference URL of record of window 1
    else if class of window 1 is in {viewer window, search window} then
        set selected_items to selection of window 1
        if (count of selected_items) = 1 then
            return the reference URL of item 1 in selected_items
        end if
    end if
end tell

This is almost exactly alike the script for Get Name except it returns the reference URL instead of the name.

  • If more than one document is selected, or if no documents are, Get Address (and Get Name) won’t return anything, which means that no valid resource is detected

Open Item

tell application id "DNtp"
    open tab for URL "$0"
    activate
end tell

If the URL scheme of DEVONthink’s document reference URLs (x-devonthink-item) was assigned to DEVONthink in the , an Open Item script would not be needed, but it’s not, so we do. "$0" is replaced by the URL returned by Get Address when the script runs. We add the call to activate because we expect links to open in the foreground.

Scheme

Hook needs to know what the scheme of URLs produced by DEVONthink is so that it knows to handle them with DEVONthink’s Open Item script. The scheme is x-devonthink-item, so put that in the “Scheme” field under Open Item

And that’s all you need to customize the way that Hook integrates with DEVONthink.

But that’s an ugly scheme, and it will make ugly URLs and what’s the point of living if we don’t take the time to make things a bit nicer?

Making things nicer

Let’s modify our scripts to use devonthink for the scheme instead of x-devonthink-item. Since the URL is parsed by our Open Item script, it doesn’t matter that it won’t match the schema used by DEVONthink.

First, go back and change the Get Address script to return URLs formatted devonthink://document-identifier instead of x-devonthink-item://document-identifier

tell application id "DNtp"
    if class of window 1 is document window then
        set ref_url to the reference URL of record of window 1
    else if class of window 1 is in {viewer window, search window} then
        set selected_items to selection of window 1
        if (count of selected_items) = 1 then
            set ref_url to the reference URL of item 1 in selected_items
        end if
    end if
end tell

set ref_url to "devonthink" & text ((length of "x-devonthink-item") + 1) thru -1 of ref_url
return ref_url

Next, change the Open Item script to reformat the URL back again.

tell application id "DNtp"
    activate
    set ref_url to "x-devonthink-item" & text ((length of "devonthink") + 1) thru -1 of "$0"
    open tab for URL ref_url
end tell

And finally, set the scheme field, under the Open Item script, to devonthink

Was this article helpful to you? Yes No

How can we help?