Hi,
I’m trying to write a script, which stretches the frontmost window to maximal size, even with two displays.
The crucial points are:
¢ some applications are not scriptable
¢ some applications don’t even return the bounds of front window (like Preview.app with disabled AppleScript capabilities)
¢ some applications’ window 1 is not the visible front window
The present found main execptions are:
¢ Finder.app, which uses special bounds (doesn’t consider the menu bar)
¢ FireFox.app (AppleScript nightmare), which uses also special bounds
¢ Preview.app, which throws an error executing the get bounds of front window line
That’s what I have so far, suggestions are welcome
property Finder : false
property FireFox : false
property off_set : missing value
property scriptable : true
property AppName : ""
set menuSecondDisplay to false
tell application "Image Events" to set SecondDisplay to (count displays) > 1
tell paragraphs of (do shell script "defaults read /Library/Preferences/com.apple.windowserver DisplaySets | awk '/ Height =/||/ Width =/ ||/UnmirroredOriginX =/ ||/UnmirroredOriginY =/ {print $3}' | cut -f 1 -d ';'")
set {d1height, d1x1, d1y1, d1width} to {item 1 as integer, item 2 as integer, item 3 as integer, item 4 as integer}
try
set {d2height, d2x1, d2y1, d2width} to {item 5 as integer, item 6 as integer, item 7 as integer, item 8 as integer}
end try
end tell
if SecondDisplay then set menubarSecondDisplay to d2x1 < 0 -- true if the menu bar is on the second display
if d2x1 is not 0 then -- check if non mirrored
tell application "System Events" to set AppName to name of (get 1st process whose frontmost is true)
try
tell application AppName to get version
if result contains "unknown" then error
tell application AppName to set {winx1, winy1, winx2, winy2} to (get bounds of front window)
set scriptable to true
on error
tell application "System Events" to tell process AppName to set {{wPosx, wPosy}, {wWidth, wHeight}} to {position, size} of front window
set {winx1, winy1, winx2, winy2} to {wPosx, wPosy, wPosx + wWidth, wPosy + wHeight}
set scriptable to false
end try
set Finder to AppName is "Finder"
set FireFox to AppName is "firefox-bin"
if SecondDisplay then
if menubarSecondDisplay then
set display1 to (d2x1 < winx1 or d2y1 < winy1)
else
set display1 to (winx1 < d2x1 or winy1 < d2y1)
end if
set off_set to (((display1 is not menubarSecondDisplay) as integer) * 22) + ((Finder as integer) * 22)
if display1 then
set_Bounds(d1width, d1height, d1x1, d1y1)
else
set_Bounds(d2width, d2height, d2x1, d2y1)
end if
else
set_Bounds(d1width, d1height, 0, 0)
end if
end if
on set_Bounds(width, height, orix, oriy)
if scriptable then
tell application AppName to set bounds of window 1 to {orix, off_set, orix + width, oriy + height}
else
tell application "System Events"
tell process AppName
try
set w to (get 1st window whose visible is true)
on error
set w to front window
end try
tell w
set position to {orix, oriy + off_set}
if FireFox then
set size to {width, height - off_set}
else
set size to {width, height}
end if
end tell
end tell
end tell
end if
end set_Bounds