Script: Look up record URL, website URL, http(s) links and x-devonthink-item links

This script can be used in different situations.

  • Run it on a selected or opened record to look up the record’s URL in all databases.

  • Run it on a visited website to look up its URL.

  • Run it on on selected text to look up http(s) links. Matching records open in document windows, URLs with no matching record open in tabs.

  • Run it on selected x-devonthink-item links to open records in document windows.

If a URL starts with http it also looks up a https version and vice versa. Additionally it checks if a URL is redirected, if so it looks up this too.

It uses macOS System Events to copy selected text so it may be that you have to increase the delay if you use it via shortcut (e.g. via Alfred or Keyboard Maestro) as there’s a chance that once in a while you don’t let your triggering shortcut go fast enough which will let macOS register your shortcut plus the shortcut System Events presses. If you use it via DEVONthink’s menu or toolbar that’s of course no issue.

-- Look up record URL, website URL, http(s) links and x-devonthink-item links

tell application id "DNtp"
	try
		set theWindow to think window 1
		set theContentRecord to missing value
		set theRecord to missing value
		set theWindow_URL to missing value
		try
			set theContentRecord to content record of theWindow
			try
				set selectedText to selected text of theWindow & "" as string
				delay 0.5
				activate
				tell application "System Events" to tell process "DEVONthink 3" to keystroke "c" using {command down}
				try
					set theGrepResults to do shell script "osascript -e 'the clipboard as «class RTF »' | perl -ne 'print chr foreach unpack(\"C*\",pack(\"H*\",substr($_,11,-3)))' | egrep -o 'http[^\"]*|x-devonthink-item[^\"]*'" -- https://superuser.com/a/1409995/
				on error
					display alert "Keine Links gefunden" buttons {"Ok"} default button 1 message "Regex kann erweitert werden …" as critical
					return
				end try
				set theQuery_URLs to paragraphs of theGrepResults
			on error
				set theWindow_URL to URL of current tab of theWindow
				if theWindow_URL ≠ "" then
					set theQuery_URLs to {theWindow_URL}
				else
					set theContentRecord_URL to URL of theContentRecord
					set theQuery_URLs to {theContentRecord_URL}
				end if
			end try
		on error
			set theSelection to selection of theWindow
			if theSelection = {} then error "Nichts ausgewählt"
			if (count theSelection) > 1 then error "Skript akzeptiert nur 1 Record"
			set theRecord to item 1 of theSelection
			set theRecord_URL to URL of theRecord
			set theQuery_URLs to {theRecord_URL}
		end try
		
		set firstURL to true
		set theDatabases to databases
		repeat with thisQuery_URL in theQuery_URLs
			set thisQuery_URL to thisQuery_URL as string
			if thisQuery_URL ≠ "" and thisQuery_URL does not start with "file:///" then
				if thisQuery_URL does not start with "x-devonthink-item://" then
					set theResults to {}
					set theURLs to {thisQuery_URL}
					
					if thisQuery_URL starts with "https" then
						set end of theURLs to ("http" & characters 6 thru -1 in thisQuery_URL) as string
					else if thisQuery_URL starts with "http" then
						set end of theURLs to ("https" & characters 5 thru -1 in thisQuery_URL) as string
					end if
					
					try
						set theURL_redirected to do shell script "curl -Ls -o /dev/null -w %{url_effective} " & quoted form of thisQuery_URL
						if theURL_redirected is not in theURLs then set end of theURLs to theURL_redirected
					end try
					
					repeat with thisURL in theURLs
						repeat with thisDatabase in theDatabases
							set thisDatabasesResults to lookup records with URL thisURL in thisDatabase
							set theResults to theResults & thisDatabasesResults
						end repeat
					end repeat
					
					set theResults_Count to (count theResults)
					
					if theResults_Count = 0 then
						if thisQuery_URL ≠ theWindow_URL then
							set newTab to open tab for URL thisQuery_URL in theWindow
							if firstURL = true then
								set current tab of theWindow to newTab
								set firstURL to false
							end if
						else
							display alert "URL nicht gefunden" buttons {"Ok"} default button 1 message "In geöffneten Datenbanken …" as informational
						end if
					else if theResults_Count = 1 then
						set thisRecord to item 1 of theResults
						if thisRecord = theContentRecord or thisRecord = theRecord then
							display alert "Keine weiteren Records gefunden" buttons {"Ok"} default button 1 message "In geöffneten Datenbanken …" as informational
						else
							open window for record thisRecord
							activate
						end if
					else if theResults_Count > 1 then
						set newWindow to open window for record (root of inbox)
						set search results of newWindow to theResults
					end if
				else
					open window for record (get record with uuid (characters 21 thru -1 in thisQuery_URL) as string)
					activate
				end if
			else
				display alert "Record hat keine URL" buttons {"Ok"} default button 1 message "" as informational
			end if
		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
2 Likes