I need a "Open with" command when viewing documents in Devonthink

I have a markdown that links to a bunch of PDFs located elsewhere in my devonthink library.

These PDFs are stored in a (by journal name, by year, by issue) hierarchy. (This lets me have a searchable fulltext database of publications that aren’t indexed by commercial database providers)

(This sort of thing).

I’m currently trying to track the articles written by one columnist, over a number of years, in many different venues, in two languages.

So, I’ve written up a markdown document, in a totally different section of my database that links to the pdfs (using x-devonthink-item: )

My problem is that the markdown links open up the pdfs in Devonthink, rather than in preview (or any of the other tools I use to process pdfs). Is there any way to override this, or to quickly open up a Devonthink pane view in another program?

No, this wouldn’t work as DEVONthink’s item links are for use with DEVONthink.

This script opens selected text’s Markdown Link formatted Reference URL in the record’s file type’s default app.

What a description :sweat_smile:

-- Open selected text's Markdown formatted Reference URL in default app

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

tell application id "DNtp"
	try
		set theWindow to think window 1
		
		try
			set theSelectedText to selected text of theWindow & "" as string
		on error
			error "No text selected."
		end try
		
		set theUUIDs to my regexFind(theSelectedText, "(?<=\\]\\(x-devonthink-item://)(.*?)(?=\\))")
		
		repeat with thisUUID in theUUIDs
			set thisPath to path of (get record with uuid (thisUUID as string))
			tell application "Finder" to open file (POSIX file thisPath as alias)
		end repeat
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
		return
	end try
end tell

on regexFind(theText, thePattern)
	try
		set theString to current application's NSString's stringWithString:theText
		set {theExpr, theError} to current application's NSRegularExpression's regularExpressionWithPattern:(thePattern) options:0 |error|:(reference)
		set theMatches to theExpr's matchesInString:theString options:0 range:{0, theString's |length|()}
		set theResults to {}
		repeat with thisMatch in theMatches
			set thisMatchRange to (thisMatch's rangeAtIndex:0)
			set thisMatchString to (theString's substringWithRange:thisMatchRange) as text
			set end of theResults to thisMatchString
		end repeat
		return theResults
	on error error_message number error_number
		activate
		display alert "Error: Handler \"regexFind\"" message error_message as warning
		error number -128
	end try
end regexFind
2 Likes

Perhaps I was unclear.

When I right click on a deventhink document title, I can get a nice contextual menu with (Open With)

When I click on a pdf in Devonthink, I get this:

A “open with” command would be a useful addition to the contextual menu.

I’ll try the script.

That is correct. There menus are contextual after all :wink:

Also, you were talking about item links in Markdown, not links in a PDF.

1 Like

Hey @pete31, do you think you could help me debug this? I modified your code slightly so that instead of selecting text, it starts by prompting the user to enter an Item Link. It seems like I broke it…

-- Open selected item from Item Link

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

tell application id "DNtp"
	try
		--set theWindow to think window 1
		try
			--set theSelectedText to selected text of theWindow & "" as string
			set theResponse to display dialog "Go to the item:" default answer "x-devonthink-item://" buttons {"Cancel", "Go"} default button "Go"
			set theSelectedText to (text returned of theResponse) as string
			return theSelectedText
		on error
			error "Invalid URL."
		end try
		
		set theUUIDs to my regexFind(theSelectedText, "(?<=\\]\\(x-devonthink-item://)(.*?)(?=\\))")
		
		repeat with thisUUID in theUUIDs
			set thisPath to path of (get record with uuid (thisUUID as string))
			tell application "Finder" to open file (POSIX file thisPath as alias)
		end repeat
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
		return
	end try
end tell

on regexFind(theText, thePattern)
	try
		set theString to current application's NSString's stringWithString:theText
		set {theExpr, theError} to current application's NSRegularExpression's regularExpressionWithPattern:(thePattern) options:0 |error|:(reference)
		set theMatches to theExpr's matchesInString:theString options:0 range:{0, theString's |length|()}
		set theResults to {}
		repeat with thisMatch in theMatches
			set thisMatchRange to (thisMatch's rangeAtIndex:0)
			set thisMatchString to (theString's substringWithRange:thisMatchRange) as text
			set end of theResults to thisMatchString
		end repeat
		return theResults
	on error error_message number error_number
		activate
		display alert "Error: Handler \"regexFind\"" message error_message as warning
		error number -128
	end try
end regexFind

The original script expects a Markdown link. In your modified version you changed the input from a Markdown link to a reference URL. You need to change the regex to match your changes (in your case you can simply remove the regex part as it’s not needed if you provide a reference URL as input).