Looking for clarification on linking

I have found myself confused about linking within DTP for some time, and just now have been trying to read about it on various internet sources.

Now I feel as though I’ve lost the forest for the trees, and thought I would seek guidance from some of our resident experts.

  1. I would like to be able to create notes within DTP, or add notes to one pdf within DTP, and to link to another document within DTP (usually another pdf).

  2. At times, I would like to be able to create some of those links in 1. to a specific passage within the other document.

  3. I use Scrivener to do a lot of writing, and would like to be able to create a link in a Scrivener document to a document within DTP.

  4. I have recently started using NValt to write plain text in markdown, and would like to be able to link to a file within DTP.

Numbers 1 and 2 have been the most important to me. Number 2 has given me the most challenges. 4 is the least important.

If anybody can step back from the details and give me a basic outline, I would greatly appreciate it!

Not addressing your specific cases, but describing links in general. Three main types of links are possible:

  1. A link to a document (or a group) as a whole in the form ```

x-devonthink-item://3B53094D-B316-4060-AB65-6008F40BC849



1. A link to a specific page in a PDF in the form ```

x-devonthink-item://3B53094D-B316-4060-AB65-6008F40BC849?page=1

``` can be made by selecting a PDF page in the sidebar of a PDF document window and choosing [b]Copy Page Link[/b] from the contextual menu.  (Page numbers begin with "0" so what you think is page 10 the link will show "?page=9"


1. A link to specific text in a document in the form ```

x-devonthink-item://3B53094D-B316-4060-AB65-6008F40BC849?search=some%20interesting%20text

``` is not a built-in feature of DEVONthink, but can be created using (for example) the following script.


set MaxLen to 1000

tell application id “com.devon-technologies.thinkpro2”
set theSelection to selection
if theSelection is {} then error “Select an item, please”
repeat with thisItem in theSelection
set theCitation to selected text of think window 1
if theCitation is “” then error “Select some text, please”
if length of theCitation is greater than MaxLen then error “That selection is longer than " & MaxLen
set theCitationEncoded to (do shell script “php -r 'echo trim(urlencode(” & “”” & theCitation & “” & “”));’" as string)
set the clipboard to the reference URL of thisItem & “?search=” & theCitationEncoded
end repeat
end tell

– example
– x-devonthink-item://B03F511C-E9EA-4228-A4B3-DB047E269CD8?search=%E2%80%9CGoldie.%E2%80%9D+The+other+fish+you+name+%E2%80%9CGreenie.


The script will work well with PDFs, sometimes with RTFs, not well with Formatted Text, and not with HTML or Web Archive documents.  I wrote this just to use with PDFs and don't have plans to make it work well with other document types.

Elsewhere in the forum I've posted scripts for making Markdown links, so I won't cross post here.

These tools should get you most if not all of what you wanted to accomplish.  Not sure about the case where links are put into PDFs that link to other PDFs -- I don't need that feature; haven't tested for it.

Thanks for the organized explanation, Korm.

I can work with the first two examples, just by copying as you noted, and then pasting into an .rtf doc.

I cannot make the last example work. I created and then invoked an AppleScript to create the link, but when I paste into a .rtf or plaintext doc, I get a long string of text starting with ‘x-devonthink-item://D7AB018C-1A45-49E5-971E-13113A75FBEA?search=…’

How would I paste this into a document to create a clickable link?

Also, is there a way to edit the link’s appearance/text, so I can make it a simple descriptor of what I am linking to? In other words, instead of the long text string, I could just change to ‘Mackinnon-evolution of nerve changes?’

Thanks!

The long string is the link – the intended use is to select text in a rich text document, choose Format > Add Link… from the menu, and paste the link string from the clipboard into the dialog that opens after Add Link…. Sure the script could be rewritten to put an RTF-link on the clipboard.

set MaxLen to 1000

tell application id "com.devon-technologies.thinkpro2"
	set theSelection to selection
	if theSelection is {} then error "Select an item, please"
	repeat with thisItem in theSelection
		set theCitation to selected text of think window 1 as Unicode text
		if theCitation is "" then error "Select some text, please"
		if length of theCitation is greater than MaxLen then error "That selection is longer than " & MaxLen
		set theCitationEncoded to (do shell script "php -r 'echo trim(urlencode(" & "\"" & theCitation & "" & "\"));'" as string)
		-- set the clipboard to the reference URL of thisItem & "?search=" & theCitationEncoded
		set theLink to (the reference URL of thisItem) & "?search=" & theCitationEncoded
		set theText to text returned of (display dialog "Enter the descriptor text" default answer theCitation with title "Make a link to this text")
		set o_HTML to quoted form of ("<font face=\"helvetica\"><a href=\"" & theLink & "\">" & theText & "</a></font>")
		set didIT to do shell script "echo " & o_HTML & " | textutil -format html -convert rtf -stdin -stdout | pbcopy -Prefer rtf"
	end repeat
end tell

That’s a pretty rough example and has encoding problems.

That’s it, Korm!

It wasn’t intuitive for me. Now I get it.

For those reading, here is what I do:

  1. Select text in a pdf to which I want to link. Run Korm’s script, which copies that text as a link.

  2. Open a document in which I want to place the link back to that pdf.

  3. Type the text that describes the linked material (the selected text in the pdf).

  4. Highlight the text that describes the linked material (without switching back to the pdf).

  5. Format->Add link, which opens a small box.

  6. Paste in the link from Korm’s script (already on the clipboard from step 1)

  7. Press ‘OK’

Now you should see a hypertext link in your open document. When you click on it, the referenced document will open, and the selected text will be highlighted.

I have used this to refer to text in a pdf, with the link placed in a .rtf doc. I have also directly pasted into a Scrivener doc, which creates a link that opens the DTP doc when requested.

Thanks again, Korm!

The second version I posted eliminates steps 3 through 7 inclusive

To clarify my last post, I used Korm’s first script in this thread.


I have now used Korm’s updated script, and it works great for me. Thanks, Korm, for getting rid of all those intermediate steps!

I can now paste the link into a .rtf file in DTP, or into a Scrivener doc (just paste directly).

Korm, I don’t know what ‘encoding problems’ are, but your script works great for me. Its an awesome tool for anyone who is writing and looking for a way to refer to more dense material without cluttering up the text.

I continue to be impressed (and sometimes overwhelmed) by the power of DT, and by the skillsets and willingness to help of the community. My work is made easier and more powerful thanks to these efforts by Korm and others.

Korm, I owe you (at least) one big drink!

1 Like

I have just discovered this thread and have tried out Korm’s script and just want to thank him for it. I am still experimenting with usages but so far it is doing all that I want of it. Cheers Korm :smiley:

1 Like