Script: Open the pdf-only See Also documents of the current database in a viewer window with more stringent condition

The script will extract see-also documents in the current database only, and with the option (1) to show only pdf or pdf and text files, (2) to open a new viewer window or use the most current viewer window to show the list of documents, (3) to specify the min matching score and max number of See-Also documents to be shown. It’s a rush job, not a refined script.

This script is probably just a reinvention of the wheel but it has utility for me: I tend to review the more currently discovered papers and might have missed relevant literature that is collected previously. So, I use some combinations of search by tagging, by keywords, and by See-Also with a more stringent condition to triangulate the set of related papers.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

-- ngan 2020.02.17
-- v1b2 2020.02.17

global thisItem, theNewWinPos, theDoc, theSeeAlsoDocs, theResults
global screenWidth, screenHeight

property minSeeAlsoWeight : 0.4
property maxSeeAlsoDocs : 15

-- options
set {screenWidth, screenHeight} to {3840, 2160}
property newViewerWin : true
set theNewWinPos to "LD" -- new window for see-also docuemnts is 1/4 of screen and is located in lower left croner 
property pdfOnly : true


tell application id "DNtp"
	
	set thisItem to item 1 of {selection}
	set theWin to think window 1
	
	set theDoc to content record of theWin
	set theSeeAlsoDocs to my getSeeAlsoDocs(theDoc, minSeeAlsoWeight, maxSeeAlsoDocs, "")
	if newViewerWin is true then
		open window for record root of current database
		my newCardPosition(theNewWinPos)
	end if
	set search results of viewer window 1 to theSeeAlsoDocs
	
end tell


on getSeeAlsoDocs(theDoc, minSeeAlsoWeight, maxSeeAlsoDocs, theSeeAlsoChoice)
	local l, lt
	set {l, lt} to {{}, {}}
	tell application id "DNtp"
		
		set theResults to compare record theDoc to current database
		
		if theResults ≠ {} then
			repeat with i from 1 to my min(maxSeeAlsoDocs, my max(length of theResults, 1))
				if pdfOnly is true then
					if ((score of theResults's item i) ≥ minSeeAlsoWeight) and (type of theResults's item i is PDF document) then set end of l to theResults's item i
				else
					
					if ((score of theResults's item i) ≥ minSeeAlsoWeight) and (type of theResults's item i is in {PDF document, rtf, rtfd, txt}) then set end of l to theResults's item i
				end if
			end repeat
		end if
		return l
		
	end tell
end getSeeAlsoDocs


on newCardPosition(thePosition)
	tell application id "DNtp"
		try
			set {WinWidth, WinHeight} to {screenWidth / 3, (screenHeight - 22) / 3} -- position in 1/3 x 1/3 of screen
			set {WinWidthQ, WinHeightQ} to {screenWidth / 2, (screenHeight - 22) / 2} -- pos in 1/2 x 1/2 of screen
			set {WinWidthH, WinHeightH} to {screenWidth / 3, 0.875 * (screenHeight - 22)} -- pos in 1/3 x 7/8 of screen
			
			set yAxis to 22
			set xAxis to 0
			
			-- the screen is divided into 9 sectors
			if thePosition is "UL" then set bounds of window 1 to {xAxis, yAxis, xAxis + WinWidth, yAxis + WinHeight}
			if thePosition is "ML" then set bounds of window 1 to {xAxis, yAxis + WinHeight, xAxis + WinWidth, yAxis + 2 * WinHeight}
			if thePosition is "LL" then set bounds of window 1 to {xAxis, yAxis + 2 * WinHeight, xAxis + WinWidth, yAxis + 3 * WinHeight}
			if thePosition is "UM" then set bounds of window 1 to {xAxis + WinWidth, yAxis, xAxis + 2 * WinWidth, yAxis + WinHeight}
			if thePosition is "MM" then set bounds of window 1 to {xAxis + WinWidth, yAxis + WinHeight, xAxis + 2 * WinWidth, yAxis + 2 * WinHeight}
			if thePosition is "LM" then set bounds of window 1 to {xAxis + WinWidth, yAxis + 2 * WinHeight, xAxis + 2 * WinWidth, yAxis + 3 * WinHeight}
			if thePosition is "UR" then set bounds of window 1 to {xAxis + 2 * WinWidth, yAxis, xAxis + 3 * WinWidth, yAxis + WinHeight}
			if thePosition is "MR" then set bounds of window 1 to {xAxis + 2 * WinWidth, yAxis + WinHeight, xAxis + 3 * WinWidth, yAxis + 2 * WinHeight}
			if thePosition is "LR" then set bounds of window 1 to {xAxis + 2 * WinWidth, yAxis + 2 * WinHeight, xAxis + 3 * WinWidth, yAxis + 3 * WinHeight}
			
			-- the screen is divided in 4 sectors LU = left-upper quartered screen
			if thePosition is "LU" then set bounds of window 1 to {xAxis, yAxis, xAxis + WinWidthQ, yAxis + WinHeightQ}
			if thePosition is "LD" then set bounds of window 1 to {xAxis, yAxis + WinHeightQ, xAxis + WinWidthQ, yAxis + 2 * WinHeightQ}
			if thePosition is "RU" then set bounds of window 1 to {xAxis + WinWidthQ, yAxis, xAxis + 2 * WinWidthQ, yAxis + WinHeightQ}
			if thePosition is "RL" then set bounds of window 1 to {xAxis + WinWidthQ, yAxis + WinHeightQ, xAxis + 2 * WinWidthQ, yAxis + 2 * WinHeightQ}
			
			-- the screen is divided in 3 sectors LF = left 1/3 screen width and 7/8 of screen height
			if thePosition is "LF" then set bounds of window 1 to {xAxis, yAxis, xAxis + WinWidthH, yAxis + WinHeightH}
			if thePosition is "CF" then set bounds of window 1 to {xAxis + WinWidthH, yAxis, xAxis + 2 * WinWidthH, yAxis + WinHeightH}
			if thePosition is "RF" then set bounds of window 1 to {xAxis + 2 * WinWidthH, yAxis, xAxis + 3 * WinWidthH, yAxis + WinHeightH}
		on error error_message number error_number
			if the error_number is not -128 then display alert "NewCardPosition() has error" message error_message as warning
		end try
	end tell
end newCardPosition


on min(x, y)
	if x ≤ y then
		return x
	else
		return y
	end if
end min
on max(x, y)
	if x ≤ y then
		return y
	else
		return x
	end if
end max