Open in two windows with a small additional function

DT3 has a very handy script “Open in two windows” for side by side reading/editing (credit to @bluefrog). I wrote this script last year to add one more function: (1) If the document is a pdf or text file with no attached annotation, the script will do almost exactly the same as the “Open in two windows” script. (2) If the document has an attached annotation file, the script will ask whether the user wants to have document-document side-by-side or document-annotation side-by-side.

I wrote the script last year, and the script is highly customised. The script I posted here is simplified. The screen resolution, the position of the two side-by-side windows, and the size of the windows must be defined in the first few lines in the script. Hope it may be useful to some DT3 users.

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

-- ngan 2019.10.17

global screenWidth, screenHeight, winHeight, winWidth, WinWidthH, WinHeightH
global theWin, theDoc, theDocAnno, doc1, doc2, openOneWin, askWhichWin, theChoice

set {screenWidth, screenHeight} to {3840, 2160}
-- LF=Left /CF=centre /RF=right , the document window is 1/3 of screen width and 7/8 of screen height*
set {posLeftWin, posRightWin} to {"CF", "RF"} 
set {WinWidthH, WinHeightH} to {screenWidth / 3, 0.875 * (screenHeight - 22)} 

tell application id "DNtp"
	
	if (count of selection) > 1 then -- only support one item
		display alert "Select one item only"
		return
		
	else if (count of selection) = 1 then
		set {theDoc} to item 1 of {selection}
		if class of theDoc is record then
			set {openOneWin, askWhichWin, doc1, doc2} to my getWinInfo(theDoc)
		else
			display alert "Select document only"
			return
		end if
		set theWin to think window 1
	end if
	
	if askWhichWin then set theChoice to button returned of (display dialog ("This document has annotation, open which pair") buttons {"Doc-Anno", "Doc-Doc"} default button 1)
	
	if openOneWin and (askWhichWin is false) then
		-- display alert "open one window - doc"
		my winPosition(theWin, posLeftWin)
		open tab for record doc1
		my winPosition(window 1, posRightWin)
		
	end if
	
	if openOneWin and askWhichWin then
		if theChoice is "Doc-Doc" then
			-- display alert "open one window - doc"
			my winPosition(theWin, posLeftWin)
			open tab for record doc1
			my winPosition(window 1, posRightWin)
			
		else
			--display alert "open one window - anno"
			my winPosition(theWin, posLeftWin)
			open tab for record doc2
			my winPosition(window 1, posRightWin)
			
		end if
	end if
	
	if (openOneWin is false) and (askWhichWin is false) then
		--display alert "open two window - doc-doc"
		open tab for record doc1
		my winPosition(window 1, posLeftWin)
		open tab for record doc1
		my winPosition(window 1, posRightWin)
		
	end if
	
	if (openOneWin is false) and askWhichWin then
		if theChoice is "Doc-Doc" then
			--display alert "open 2 wins - doc-doc"
			open tab for record doc1
			my winPosition(window 1, posLeftWin)
			open tab for record doc1
			my winPosition(window 1, posRightWin)
			
		else
			--display alert "open 2 wins - doc-anno"
			open tab for record doc1
			my winPosition(window 1, posLeftWin)
			open tab for record doc2
			my winPosition(window 1, posRightWin)
			
		end if
	end if
	
	
end tell

on getWinInfo(theDoc)
	tell application id "DNtp"
		local isdw, askw, d1, d2, o1w -- isDocWin, askWhichWinToOpen, doc1,doc2,numWinToOpen
		set isdw to (class of think window 1 is document window)
		
		if isdw then
			set o1w to true
		else
			set o1w to false
		end if
		
		if (annotation of theDoc exists) then
			set d1 to theDoc
			set d2 to annotation of theDoc
			set askw to true
		else
			set d1 to theDoc
			set d2 to theDoc
			set askw to false
		end if
		
		return {o1w, askw, d1, d2}
		
	end tell
end getWinInfo

on winPosition(theWin, thePosition)
	
	local winWidth, winHeight, WinWidthQ, WinHeightQ
	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 yAxis to 22
		set xAxis to 0
		
		-- 1/3 of sreen width x 1/3 of the screen height 
		if thePosition is "UL" then set bounds of theWin to {xAxis, yAxis, xAxis + winWidth, yAxis + winHeight}
		if thePosition is "ML" then set bounds of theWin to {xAxis, yAxis + winHeight, xAxis + winWidth, yAxis + 2 * winHeight}
		if thePosition is "LL" then set bounds of theWin to {xAxis, yAxis + 2 * winHeight, xAxis + winWidth, yAxis + 3 * winHeight}
		if thePosition is "UM" then set bounds of theWin to {xAxis + winWidth, yAxis, xAxis + 2 * winWidth, yAxis + winHeight}
		if thePosition is "MM" then set bounds of theWin to {xAxis + winWidth, yAxis + winHeight, xAxis + 2 * winWidth, yAxis + 2 * winHeight}
		if thePosition is "LM" then set bounds of theWin to {xAxis + winWidth, yAxis + 2 * winHeight, xAxis + 2 * winWidth, yAxis + 3 * winHeight}
		if thePosition is "UR" then set bounds of theWin to {xAxis + 2 * winWidth, yAxis, xAxis + 3 * winWidth, yAxis + winHeight}
		if thePosition is "MR" then set bounds of theWin to {xAxis + 2 * winWidth, yAxis + winHeight, xAxis + 3 * winWidth, yAxis + 2 * winHeight}
		if thePosition is "LR" then set bounds of theWin to {xAxis + 2 * winWidth, yAxis + 2 * winHeight, xAxis + 3 * winWidth, yAxis + 3 * winHeight}
		
		-- 1/2 of sreen width x 1/2 of the screen height
		if thePosition is "LU" then set bounds of theWin to {xAxis, yAxis, xAxis + WinWidthQ, yAxis + WinHeightQ}
		if thePosition is "LD" then set bounds of theWin to {xAxis, yAxis + WinHeightQ, xAxis + WinWidthQ, yAxis + 2 * WinHeightQ}
		if thePosition is "RU" then set bounds of theWin to {xAxis + WinWidthQ, yAxis, xAxis + 2 * WinWidthQ, yAxis + WinHeightQ}
		if thePosition is "RL" then set bounds of theWin to {xAxis + WinWidthQ, yAxis + WinHeightQ, xAxis + 2 * WinWidthQ, yAxis + 2 * WinHeightQ}
		
		-- 1/3 of screen width at 7/8 of screen height
		if thePosition is "LF" then set bounds of theWin to {xAxis, (screenHeight - yAxis - WinHeightH) / 2, xAxis + WinWidthH, (screenHeight - yAxis - WinHeightH) / 2 + WinHeightH}
		if thePosition is "CF" then set bounds of theWin to {xAxis + WinWidthH, (screenHeight - yAxis - WinHeightH) / 2, xAxis + 2 * WinWidthH, (screenHeight - yAxis - WinHeightH) / 2 + WinHeightH}
		if thePosition is "RF" then set bounds of theWin to {xAxis + 2 * WinWidthH, (screenHeight - yAxis - WinHeightH) / 2, xAxis + 3 * WinWidthH, (screenHeight - yAxis - WinHeightH) / 2 + WinHeightH}
		
	end tell
end winPosition


Actually, the one in DEVONthink 2 opened an Annotation document if one was found.
Since the Annotation mechanism changed in DEVONthink 3, it no longer does but if you looked at the code, you’d see it still in there. :stuck_out_tongue:

:+1: :+1: :+1: There are always many hidden treasures in the scripts provided by you guys! The stock scripts are excellent because they aim for no-brainers ease of use. I always like to add options…

1 Like