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!
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
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”
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
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
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
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
-- Lookup Google Chrome URL in open databases
tell application "Google Chrome"
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 "Chrome" 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
I think the if theResults ≠ {}check is not necessary. You could instead use a three-step if else:
set resultCount to (count theResult)
if resultCount = 0 then
-- error
else if resultCount = 1 then
-- open window for 1 record
else
-- open search results
end if
since count {} is always 0 (as is {} count. I just love languages without a grammar). That would make the flow a bit clearer, I think.
Question: Is the tell block in on lookup_URL needed in AppleScript, given that the handler is called from within an identical tell block? As I understand it, the tell (kind of) sets the environment for the block, i.e. which methods and properties are available. Those should be the same in the caller and in lookup_URL, I think.
Aside: Why are you wrapping nearly everything in a try block? I’d see the point if the code would handle errors, but it does not (except for showing an error message, that is).