Open and tile two apps

The Shortcuts app has an action that opens and tiles two specified apps. This generally works as expected, but I wondered if an AppleScript would do a better job. In the end, neither solution is significantly better in my testing, although the shortcut might be preferred for simplicity.

Note should be made that the AppleScript assumes that an app’s application and process names are the same. They almost always are, and, if not, the process name is easily set in the first line of tileWindows handler. Also, some apps will not tile exactly as desired, but the shortcut generally does a better job of this.

The script:

use framework "Foundation"
use scripting additions

set theApps to {"Mail", "Safari"}
set theDisplay to getDisplay() -- returns {x, y, w, h} and does not include menu bar or dock
set theWindows to getWindows(theDisplay) -- returns {x, y, w, h} for each tiled app

repeat with i from 2 to 1 by -1
	tell application (item i of theApps) to activate
	delay 0.2 -- may not be necessary
	tileApp(item i of theApps, item i of theWindows)
end repeat

on getDisplay()
	set theScreen to current application's NSScreen's mainScreen()
	set {{x1, y1}, {w1, h1}} to theScreen's frame()
	set {{x2, y2}, {w2, h2}} to theScreen's visibleFrame()
	return {x2 as integer, (h1 - h2 - y2) as integer, w2 as integer, h2 as integer}
end getDisplay

on getWindows(displayBounds)
	set {x, y, w, h} to displayBounds
	set halfWidth to w div 2
	return {{x, y, halfWidth, h}, {halfWidth + x + 1, y, halfWidth, h}}
end getWindows

on tileApp(theApp, theWindow)
	tell application "System Events" to tell process theApp to tell every window
		set position to {item 1, item 2} of theWindow
		set size to {item 3, item 4} of theWindow
	end tell
end tileApp

The shortcut:

Open and Tile.shortcut (21.5 KB)