I have three screens and always have many opened document windows scattered all around. This is a small utility to consolidate all document-only windows in all displays and cascade those windows at the main monitor. The initial position and the (x,y) point offset among all cascaded windows are defined at the first few lines of the script. You may modify the script to ur likings.
Cheers
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
-- by ngan 2020.02.13
global screenWidth, screenHeight, theIniPosition, theXoffset, theYoffset
global theDocWins
set {screenWidth, screenHeight} to {3840, 2160}
set {theXoffset, theYoffset} to {50, 50}
set theIniPosition to "LF" -- LF/CF/RF: 1/3 vertical full screen
set sortByName to true
tell application id "DNtp"
set theDocWins to document windows
if sortByName is true then
set theDocWins to my sortObjlist(theDocWins)
end if
my cascadeWindows(theDocWins, theIniPosition, theXoffset, theYoffset, screenWidth, screenHeight)
end tell
on cascadeWindows(theItems, theIniPosition, theXoffset, theYoffset, screenWidth, screenHeight)
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.85 * (screenHeight - 22)} -- pos in 1/3 x 7/8 of screen
set yAxis to 22
set xAxis to 0
-- 1/9 screen e.g. UL = upper left 1/3 x 1/3 of screen
if theIniPosition is "UL" then set {x1, y1, x2, y2} to {xAxis, yAxis, xAxis + WinWidth, yAxis + WinHeight}
if theIniPosition is "ML" then set {x1, y1, x2, y2} to {xAxis, yAxis + WinHeight, xAxis + WinWidth, yAxis + 2 * WinHeight}
if theIniPosition is "LL" then set {x1, y1, x2, y2} to {xAxis, yAxis + 2 * WinHeight, xAxis + WinWidth, yAxis + 3 * WinHeight}
if theIniPosition is "UM" then set {x1, y1, x2, y2} to {xAxis + WinWidth, yAxis, xAxis + 2 * WinWidth, yAxis + WinHeight}
if theIniPosition is "MM" then set {x1, y1, x2, y2} to {xAxis + WinWidth, yAxis + WinHeight, xAxis + 2 * WinWidth, yAxis + 2 * WinHeight}
if theIniPosition is "LM" then set {x1, y1, x2, y2} to {xAxis + WinWidth, yAxis + 2 * WinHeight, xAxis + 2 * WinWidth, yAxis + 3 * WinHeight}
if theIniPosition is "UR" then set {x1, y1, x2, y2} to {xAxis + 2 * WinWidth, yAxis, xAxis + 3 * WinWidth, yAxis + WinHeight}
if theIniPosition is "MR" then set {x1, y1, x2, y2} to {xAxis + 2 * WinWidth, yAxis + WinHeight, xAxis + 3 * WinWidth, yAxis + 2 * WinHeight}
if theIniPosition is "LR" then set {x1, y1, x2, y2} to {xAxis + 2 * WinWidth, yAxis + 2 * WinHeight, xAxis + 3 * WinWidth, yAxis + 3 * WinHeight}
-- 1/4 screen e.g. LU = left-upper quarter screen
if theIniPosition is "LU" then set {x1, y1, x2, y2} to {xAxis, yAxis, xAxis + WinWidthQ, yAxis + WinHeightQ}
if theIniPosition is "LD" then set {x1, y1, x2, y2} to {xAxis, yAxis + WinHeightQ, xAxis + WinWidthQ, yAxis + 2 * WinHeightQ}
if theIniPosition is "RU" then set {x1, y1, x2, y2} to {xAxis + WinWidthQ, yAxis, xAxis + 2 * WinWidthQ, yAxis + WinHeightQ}
if theIniPosition is "RL" then set {x1, y1, x2, y2} to {xAxis + WinWidthQ, yAxis + WinHeightQ, xAxis + 2 * WinWidthQ, yAxis + 2 * WinHeightQ}
-- 1/3 of 85% of vertical screen e.g. LF = 85% of full height with 1/3 width at left
if theIniPosition is "LF" then set {x1, y1, x2, y2} to {xAxis, yAxis, xAxis + WinWidthH, yAxis + WinHeightH}
if theIniPosition is "CF" then set {x1, y1, x2, y2} to {xAxis + WinWidthH, yAxis, xAxis + 2 * WinWidthH, yAxis + WinHeightH}
if theIniPosition is "RF" then set {x1, y1, x2, y2} to {xAxis + 2 * WinWidthH, yAxis, xAxis + 3 * WinWidthH, yAxis + WinHeightH}
repeat with each in theItems
set index of each to 1
set bounds of each to {x1, y1, x2, y2}
set {x1, y1, x2, y2} to {x1 + theXoffset, y1 + theYoffset, x2 + theXoffset, y2 + theYoffset}
end repeat
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 cascadeWindows
on sortObjlist(theList)
tell application id "DNtp"
set theIndexList to {}
set theSortedList to {}
repeat (length of theList) times
set theLowItem to ""
repeat with a from 1 to (length of theList)
if a is not in theIndexList then
set theCurrentItem to item a of theList
if theLowItem is "" then
set theLowItem to theCurrentItem
set theLowItemIndex to a
else if (name of theCurrentItem) comes before (name of theLowItem) then
set theLowItem to theCurrentItem
set theLowItemIndex to a
end if
end if
end repeat
set end of theSortedList to theLowItem
set end of theIndexList to theLowItemIndex
end repeat
return theSortedList
end tell
end sortObjlist