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.