Current window to full size on current display
Alec Jacobson
November 03, 2010
There are a lot of "maximize window to window" to fit screen scripts floating around, but most don't bother with supporting a dual monitor set up. Here's a script that resizes the frontmost window of the frontmost application to fit the whole screen of the display that the window is currently in (decided by the position of the top left corner):
set main_display_size to get_main_display_size()
set main_display_width to item 1 of main_display_size
set main_display_height to item 2 of main_display_size
set secondary_display_size to get_secondary_display_size()
set secondary_display_width to item 1 of secondary_display_size
set secondary_display_height to item 2 of secondary_display_size
tell application "System Events"
set frontmostApplication to name of the first process whose frontmost is true
end tell
tell application frontmostApplication
set old_bounds to bounds of first window
if ((item 1 of old_bounds) < 0 or (item 1 of old_bounds) > main_display_width or (item 2 of old_bounds) < 0 or (item 2 of old_bounds) > main_display_height) and (secondary_display_width is not equal to -1 and secondary_display_height is not equal to -1) then
tell application "Finder"
set desktop_bounds to get bounds of window of desktop
end tell
set bounds of first window to {item 1 of desktop_bounds, item 2 of desktop_bounds, (item 1 of desktop_bounds) + secondary_display_width, (item 2 of desktop_bounds) + secondary_display_height}
else
set bounds of first window to {0, 0, main_display_width, main_display_height}
end if
end tell
on get_main_display_size()
set command to "/usr/sbin/system_profiler SPDisplaysDataType"
set output to (do shell script command)
set AppleScript's text item delimiters to "Resolution: "
set displays to text items of output
set i to 2
repeat ((length of displays) - 1) times
if (offset of "Main Display: Yes" in (item i of displays)) is not equal to 0 then
set word_list to words of item i of displays
set main_display_width to item 1 of word_list
set main_display_height to item 3 of word_list
end if
set i to i + 1
end repeat
return {main_display_width as integer, main_display_height as integer}
end get_main_display_size
on get_secondary_display_size()
set command to "/usr/sbin/system_profiler SPDisplaysDataType"
set output to (do shell script command)
set AppleScript's text item delimiters to "Resolution: "
set displays to text items of output
set i to 2
set found to false
repeat ((length of displays) - 1) times
if (offset of "Main Display: Yes" in (item i of displays)) is equal to 0 then
set word_list to words of item i of displays
set secondary_display_width to item 1 of word_list
set secondary_display_height to item 3 of word_list
set found to true
end if
set i to i + 1
end repeat
if found then
return {secondary_display_width as integer, secondary_display_height as integer}
else
return {-1, -1}
end if
end get_secondary_display_size