use framework "AppKit"
use scripting additions
-- =========================================================
-- SETTINGS
-- =========================================================
set windowWidth to 1120
set windowHeight to 500
set offsetX to 45
set offsetY to 45
-- =========================================================
-- GET THE MAIN DISPLAY
-- =========================================================
set targetScreen to current application's NSScreen's mainScreen()
-- visibleFrame format:
-- {{originX, originY}, {width, height}}
set screenFrame to targetScreen's visibleFrame() as list
set screenOrigin to item 1 of screenFrame
set screenSize to item 2 of screenFrame
set screenLeft to item 1 of screenOrigin as integer
set screenBottom to item 2 of screenOrigin as integer
set screenWidth to item 1 of screenSize as integer
set screenHeight to item 2 of screenSize as integer
-- =========================================================
-- CONVERT macOS SCREEN COORDINATES
-- =========================================================
-- AppKit uses bottom-left coordinates.
-- Accessibility/System Events uses top-left coordinates.
-- Find the highest Y coordinate across all displays.
set screenList to current application's NSScreen's screens()
set maxY to 0
repeat with aScreen in screenList
set aFrame to aScreen's frame() as list
set aOrigin to item 1 of aFrame
set aSize to item 2 of aFrame
set screenTopY to (item 2 of aOrigin) + (item 2 of aSize)
if screenTopY > maxY then set maxY to screenTopY
end repeat
-- Convert the target screen's visible frame to System Events coordinates
set screenTop to maxY - (screenBottom + screenHeight)
-- =========================================================
-- CASCADE TEXTSOAP WINDOWS
-- =========================================================
tell application "System Events"
if not (exists process "TextSoap") then
display dialog "TextSoap is not running."
else
tell process "TextSoap"
set textSoapWindows to every window
set windowCount to count of textSoapWindows
if windowCount > 0 then
repeat with i from 1 to windowCount
-- Reverse the cascade order.
set cascadePosition to windowCount - i
set xPos to screenLeft + (cascadePosition * offsetX)
set yPos to screenTop + (cascadePosition * offsetY)
-- Keep windows within the main display.
if xPos + windowWidth > screenLeft + screenWidth then
set xPos to screenLeft
end if
if yPos + windowHeight > screenTop + screenHeight then
set yPos to screenTop
end if
-- Move and resize through Accessibility
set position of item i of textSoapWindows to {xPos, yPos}
set size of item i of textSoapWindows to {windowWidth, windowHeight}
end repeat
end if
end tell
end if
end tell