Script: Lookup Safari URL in open databases

This script checks if the current Safari URL is in one of my open databases. For me it does the following, but you can easily adjust it to your needs:

no results found : display notification
one result found : open record
> 1 results found : open results in search window

Hope it’s useful! 8)

tell application id "com.apple.Safari" to tell window 1 to set theURL to current tab's URL

tell application id "com.devon-technologies.thinkpro2"
	try
		set theDatabases to databases
		set theResults to {}
		repeat with thisDatabase in theDatabases
			set thisDatabasesResults to lookup records with URL theURL in thisDatabase
			set theResults to theResults & thisDatabasesResults
		end repeat
		
		set ResultCount to (count of theResults)
		
		if ResultCount = 0 then
			display notification "Noch nicht hinzugefügt" with title "DEVONthink"
			return
		end if
		
		if ResultCount = 1 then
			open window for record (item 1 of theResults)
			activate
			return
		end if
		
		if ResultCount > 1 then
			set theSearchURL to "x-devonthink://search?query=" & theURL
			open location theSearchURL
		end if
		
	end try
end tell

Thanks for posting!
One note… Your code isn’t ”wrong" but

tell application id "com.devon-technologies.thinkpro2"

is generally

tell application id “DNtp"

nowadays. Still works though (and it’s easier to type :mrgreen: )

I normally use a Typinator snippet with “DNtp"- but I wanted to do it extra “right” for the forum (as much as I can…) and used Script Debuggers “Paste Tell” menu… which gave me “com.devon-technologies.thinkpro2” :laughing:

Actually, DNtp is a better option for the future. :smiley:

What would I need to add to the script so it searches in an HTML file where there is a url list to a certain url?

This is just a simple proof-of-concept example, relating specifically to your question…

property htmlDocID : "651F0FEB-D35D-4851-8D4B-930672549FE6" -- This is the UUID of a specific document, take from the Item Link
set currentURL to "https://en.wikipedia.org/wiki/URL_shortening" -- This is just hardcoded for the example

tell application id "DNtp"
	set sourceDocument to (get record with uuid htmlDocID)
 -- Get a reference to the document
	set src to (source of sourceDocument)
 -- Get the underlying markup
	set docLinks to (get links of src type html)
 -- Parse it for links
	docLinks contains currentURL
 -- A simple Boolean validation whether the link is found or not.
end tell

I have a script that gathers all active urls in safari and saves it in an HTML file. So there is a list of Urls in the HTML file.

This version searches both, “https://” and “http://” URLs. This is important as DEVONthink can’t find an old record with a http scheme if the website you are searching for uses https in the meantime.

If used on a discourse forum URL (the ones I know…) like the DEVONthink forum, it will find posts from the same thread. But this is only possible by using the search window (instead of first using the “lookup records with URL” command) so if theres no record found you’ll only see an empty search window. Don’t know another way to do it, as the “lookup” command doesn’t allow wildcards. If you know more discourse forums let me know.

tell application "Safari" to tell window 1 to set theURL to current tab's URL

set theURL to characters ((offset of "//" in theURL) + 2) thru -1 in theURL as string

set theForums to {"discourse.", "discuss.", "forum.", "forums.", "meta."}

if (characters 1 thru (offset of "." in theURL) in theURL as string) is in theForums then
	
	set d to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "/"
	set TextItems to text items of theURL
	set AppleScript's text item delimiters to d
	
	try
		item -2 of TextItems as integer
		set reverseURL to reverse of (characters of theURL) as string
		set reverseOffset to -((offset of "/" in reverseURL) + 1)
		set theURL to (characters 1 thru reverseOffset) in theURL as string
		set theSearchURL to "x-devonthink://search?query=" & theURL
		open location theSearchURL
		return
	end try
	
end if

tell application id "DNtp"
	try
		set theDatabases to databases
		set theResults to {}
		
		repeat with thisScheme in {"https://", "http://"}
			repeat with thisDatabase in theDatabases
				set thisDatabasesResults to lookup records with URL thisScheme & theURL in thisDatabase
				set theResults to theResults & thisDatabasesResults
			end repeat
		end repeat
		
		set ResultCount to (count of theResults)
		
		if ResultCount = 0 then
			set noResults to display notification "Noch nicht hinzugefügt" with title "DEVONthink URL Check"
			return
		end if
		
		if ResultCount = 1 then
			open window for record (item 1 of theResults)
			activate
			return
		end if
		
		if ResultCount > 1 then
			set theSearchURL to "x-devonthink://search?query=" & theURL
		end if
		
	end try
end tell

open location theSearchURL

What if you have a list of URLs in a file? Can it find it?

No this is for Safari. I don’t know HTML…

Yes for safari.
I have a script that gathers all URLs then it makes a list of all the current opened tabs.

I guess this script doesn’t search for ULRs within a file.

Here’s a new version

  • If there’s one record found it will be opened
  • If there are several records they are shown in a new window
-- Lookup Safari URL in open databases

tell application "Safari"
	try
		if not (exists current tab of window 1) then error "Please open a tab"
		set theURL to URL of current tab of window 1
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "Safari" message error_message as warning
		return
	end try
end tell

tell application id "DNtp"
	try
		set theResults to my lookup_URL(theURL)
		
		if theResults ≠ {} then
			if (count theResults) = 1 then
				open window for record (item 1 of theResults)
				activate
			else
				open window for record (root of inbox) with force
				activate
				set search results of viewer window 1 to theResults
			end if
		else
			display notification "URL is not in open databases" with title "DEVONthink URL Check"
		end if
		
	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 lookup_URL(theURL) -- http, https and redirected URL
	try
		tell application id "DNtp"
			set theDatabases to databases
			set theResults to {}
			
			set theURLs to {theURL}
			
			if theURL starts with "https" then
				set end of theURLs to ("http" & characters 6 thru -1 in theURL) as string
			else if theURL starts with "http" then
				set end of theURLs to ("https" & characters 5 thru -1 in theURL) as string
			end if
			
			try
				set theURL_redirected to do shell script "curl -Ls -o /dev/null -w %{url_effective} " & quoted form of theURL
				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
			
			return theResults
		end tell
		
	on error error_message number error_number
		activate
		if the error_number is not -128 then display alert "Error: Handler \"lookup_URL\"" message error_message as warning
		error number -128
	end try
end lookup_URL

This is great, thanks for sharing @pete31.

And here’s a slightly modified version of the script that works with Brave:

-- Lookup Brave URL in open databases

tell application "Brave Browser"
	try
		if not (exists active tab of window 1) then error "Please open a tab"
		set theURL to URL of active tab of window 1
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "Brave" message error_message as warning
		return
	end try
end tell

tell application id "DNtp"
	try
		set theResults to my lookup_URL(theURL)
		
		if theResults ≠ {} then
			if (count theResults) = 1 then
				open window for record (item 1 of theResults)
				activate
			else
				open window for record (root of inbox) with force
				activate
				set search results of viewer window 1 to theResults
			end if
		else
			display notification "URL is not in open databases" with title "DEVONthink URL Check"
		end if
		
	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 lookup_URL(theURL) -- http, https and redirected URL
	try
		tell application id "DNtp"
			set theDatabases to databases
			set theResults to {}
			
			set theURLs to {theURL}
			
			if theURL starts with "https" then
				set end of theURLs to ("http" & characters 6 thru -1 in theURL) as string
			else if theURL starts with "http" then
				set end of theURLs to ("https" & characters 5 thru -1 in theURL) as string
			end if
			
			try
				set theURL_redirected to do shell script "curl -Ls -o /dev/null -w %{url_effective} " & quoted form of theURL
				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
			
			return theResults
		end tell
		
	on error error_message number error_number
		activate
		if the error_number is not -128 then display alert "Error: Handler \"lookup_URL\"" message error_message as warning
		error number -128
	end try
end lookup_URL

1 Like