@ecbanks

Using AppleScript To Size A Window To 16×9 On MacOS

U

As part of an automation workflow I’m building around Elgato Stream Deck, I needed a way to size an application window to 16×9. This would be one component of a workflow that would allow me to launch an app, size the window, position it on the screen, and hide all the other windows with the push of a Stream Deck button.

The easy part was the Stream Deck configuration. The hard part was the AppleScript–I had never written one.

About The Script

This script is an amalgamation of various blog posts, Apple documentation, and Stack Exchange queries that I came across as I got smarter about what to ask the DuckDuckGo search engine. It’s my first go with AppleScript, but I’m reasonably happy with it.

If you landed here trying to solve an AppleScript problem, I recommend popping some of the commands from the code below into your favorite search engine. When I started searching on lines of code, I started getting especially helpful results.

What This Script Does

In plain language, the script first discovers the dimensions of the primary display. Distinguishing the primary display is helpful when working on multi-monitor Macs. This script only concerns itself with the primary display, and does not allow for placing the application window on other displays. (This could be done with more math, but wasn’t relevant to my use case.)

Using the dimensions of the primary display as the base, the script performs simple arithmetic to calculate where to place the application named at the command line and what size to make it. The result is an app window sized at 16×9 and placed in the upper center of the primary display.

The script is flexible in that it will work no matter what the dimensions of the primary display are. Therefore, if you decide to run a different screen resolution, the script will still work.

Run the script from the command line as follows, where AppName is the name of the application whose window you want to move and resize.

osascript 16x9.scpt AppName

The Code

Heavily commented inline…

# We need these frameworks and additions for the NSScreen function
use framework "Foundation"
use framework "AppKit"
use scripting additions

# Run handler. Argv contains parameters passed to osascript at
# the command line.
on run argv 
	
    # We only care about the first parameter, which is the
    # app name.
	set theApp to the first item of argv
	
    # Get a list of lists with the primary display bounds.
	set thePrimaryDisplay to current application's NSScreen's screens()'s firstObject()'s frame()
    # Save the crucial list values.
	set screenWidth to item 1 of item 2 of thePrimaryDisplay as integer
	set screenHeight to item 2 of item 2 of thePrimaryDisplay as integer
	
    # 0.78125 centers the app nicely on the display.
	set appWidth to screenWidth * 0.78125 as integer
    # We want a 16x9 app window, and set the height accordingly.
	set appHeight to appWidth / 16 * 9 as integer
	
    # Set the top left window pixel horizontal coordinate to
    # center the app.
	set xPos to (screenWidth - appWidth) / 2 as integer
    # Set the top left window pixel vertical coordinate to be
    # closer to the top.
    # Divide by 2 instead of 4 to center the app in the display.
	set yPos to (screenHeight - appHeight) / 4 as integer
	
    # This nested tell block puts the upper left corner of the
    # app where it belongs, then resizes the window to 16x9.
	tell application "System Events" to tell process theApp
		tell window 1
			set position to {xPos, yPos}
			set size to {appWidth, appHeight}
		end tell
	end tell
	
end run

More Worth Explaining

Mac applications can’t be acted upon by AppleScript unless they have a dictionary cataloging the commands that they will respond to. Some applications have this dictionary. Many do not. The System Events process referenced in the final tell block is a useful proxy for resizing windows when the app you want to resize doesn’t have the required dictionary.

Using Apple’s Script Editor, select File > Open Dictionary to browse what can be referenced in the script. Here’s a look at what AppleScript directives Firefox will respond to. For example, setting window bounds.

This code snippet will work for Firefox, no proxying through System Events required.

tell application theApp
	set bounds of front window to {xPos, yPos, appWidth + xPos, appHeight + yPos}
end tell

On OS X 10.15, you might run into security problems the first time you run the script (or application, if you decided to save the script as an app in Script Editor). Follow the prompts and grant the script the appropriate permissions in System Preferences > Security & Privacy. You might need to make adjustments in the Accessibility and/or Automation sections, but don’t overthink it at first. Your Mac will prompt you to do what needs to be done.

Updated 19-Feb-2020.

About the author

Ethan Banks

Most people know me because I write & podcast about IT on the Packet Pushers network. I also co-authored "Computer Networks Problems & Solutions" with Russ White.

Find out more on my about page.

@ecbanks

Most people know me because I write & podcast about IT on the Packet Pushers network. I also co-authored "Computer Networks Problems & Solutions" with Russ White.

Find out more on my about page.

Subscribe via Email

Receive complete, ad-free posts in your inbox as I publish them here.

Secured By miniOrange