Item links in DT3

It would be great is the “?search=” command launches the search inspector as well.

I’m trying to revise the above script above and could use a little help.

The script creates an item link that will do a search for the selected text. I want to replace any spaces in the selected text with “%20” but I keep getting error number -1708. Could someone help point out the (probably super obvious) thing I’m messing up? Thanks!


tell application id "DNtp"
	try
		if not (exists think window 1) then error "No window is open."
		if not (exists content record) then error "Please open a document."
		set find_this to the selected text of think window 1 as string
		set find_this to replaceText(find_this, " ", "%20")
		set itemURL to get the reference URL of (item 1 of (selection as list)) & "?reveal=1" & "&search=" & find_this
		set the clipboard to itemURL
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "Script Error" message error_message as warning
		
	end try
end tell

on replaceText(theString, old, new)
	set {TID, text item delimiters} to {text item delimiters, old}
	set theStringItems to text items of theString
	set text item delimiters to new
	set theString to theStringItems as text
	set text item delimiters to TID
	return theString
end replaceText

Just insert “my” before replaceText:

set find_this to my replaceText(find_this, " ", "%20")

Thank you! (No sample I found online had “my”… a new thing?)

No, very old. Maybe it’s not required if the script does explicitly use newer AppleScript versions?

Thanks. Here’s a cleaned up version of the text. Gives you the option to create a link to the document itself, the current line position in the document, or the selected text in the document. I added it to my toolbar as a more robust version of the “copy item link” command.

tell application id "DNtp"
	try
		if not (exists think window 1) then error "No window is open."
		if not (exists content record) then error "Please open a document."
		
		set answer to the button returned of (display dialog "What part of the document do you want to link to?" buttons {"whole document", "position", "selected text"} default button 1)
		
		if answer = "whole document" then
			set itemURL to get the reference URL of (item 1 of (selection as list)) & "?reveal=1&line=0"
			
		else if answer = "position" then
			set this_line to the current line of the tab of think window 1
			set itemURL to get the reference URL of (item 1 of (selection as list)) & "?reveal=1"
			if this_line is not equal to "" then set itemURL to itemURL & "&line=" & this_line
			
		else if answer = "selected text" then
			set find_this to the selected text of think window 1 as string
			set find_this to my replaceText(find_this, " ", "%20")
			set itemURL to get the reference URL of (item 1 of (selection as list)) & "?reveal=1" & "&search=" & find_this

		end if		
		set the clipboard to itemURL
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
	end try
end tell

on replaceText(theString, old, new)
	set {TID, text item delimiters} to {text item delimiters, old}
	set theStringItems to text items of theString
	set text item delimiters to new
	set theString to theStringItems as text
	set text item delimiters to TID
	return theString
end replaceText
3 Likes

How can you add a specific script to the toolbar? Is that possible as a customization?

Go to the Scripts menu, select Open Scripts Folder. In the Scripts folder, put any script you want to use in the toolbar into the folder “Toolbar”. Restart DevonThink. Right-click on the toolbar to customize. Drag the script into the toolbar.

Note, if you want the script to have a custom icon in the toolbar, you can replace the script file’s icon in the Finder/

1 Like

Thank you - that is very very helpful

Did I miss that in the documentation somewhere?

Help > Documentation > Windows > Main Window > Toolbar (jumps to) Appendix > Toolbar Items > Custom Items

2 Likes

Thanks - I was looking in a different section

Very nice feature

Hi, @cgrunenberg,

I found that the current fix in DT3.0.1 is just a special case which only works for encoded space bar, but DT2 supports more encodings, at the same time & doesn’t work in DT3.0.1 either.

E.g., If I want to add a link to a specific paragraph of a pdf file on page 8, in DT2 I can do this by openning the page 8 of the pdf file, then encoding the beginning sentence of the paragraph and append it with &. DT2 enables me to encode more chars, like punctuations, so finally I can achieve this by inserting the following string into a RFT or a Markdown file, like:

[11, §2.2.2](x-devonthink-item://66DA-...-2B7E?page=8&?search=%5B11%2C%20%C2%A72.2.2%5D.)

Note that it has two parameters:

(1) ?page=8 let DT2 only search one page, instead of the whole file from the beginning, for a 600-page book this makes a big difference!

(2) ?search=%5B11%2C%20%C2%A72.2.2%5D., this is the encoded counterpart of the string 11, §2.2.2 which indicates the specific target paragraph, it contains numbers, space, ,, . and §, DT2 has no problem with this string.

in DT3 it’s impossible to search strings like 11, §2.2.2 or limit the search page with &.

BTW, sorry for my persistence of the support for this syntax. I know that in DT2/3 there are already a hierarchy of link functions implementd, like
(1) link to database,
(2) link to item
(3) link to page for pdf
(4) link to paragraph for txt
(5) back link in annotation panel.

I think my request is a good complement to this hierarchy, because right now there is no way to link to a paragraph for pdf, meanwhile (4) and (5) works the same way but only for txt, they actually append ?line=n at the end of the search, which suffers from a drawback: The target line may be shifted to another line after modifying the txt file. By searching it doesn’t matter at which line the paragraph locates.

I just checked the source of version 2 again, like version 3 it doesn’t support “search” and “page” parameters concurrently. “search” has the highest priority, therefore it probably only seems to use the “page” parameter as it jumps to the right page. In addition, all occurrences in the document are highlighted in both versions.

2 Likes

@BLUEFROG - is there any way to add a custom template script to the toolbar? I tried the procedure for regular scripts with the Pubmed template/script that comes with DT3; that did not work.

Put it in ~/Library/Application Support/DEVONthink 3/Templates.noindex/Toolbar, then quit and relaunc DEVONthink.

1 Like

That works great - thanks

No problem

I thought I’d try the script posted by dansroka (cleaned up version), I put the script in the toolbar folder, closed down & reopened DT3, & tried to drag it to the toolbar, but that didn’t seem to work so I put it in one of the menu folders. I ran the script with a pdf doc selected. Nothing seemed to happen, but now I have an icon on my toolbar that shows whatever file I’ve got selected in DT3.

I can’t get rid of this, I deleted the script, tried to delete the icon, drag to bin, reboot computer, but it remains. Any idea how I can get rid of the icon??
I know I shouldn’t play around with things I know nothing about

I made the script with RTF and plain text docs in mind. Maybe the script doesn’t work with PDFs…?

It puts the item link into the clipboard.

Open your destination document and press Command V