MacOS – control window size and position when opening files via script

macosscriptwindow-manager

I have a script that opens three shortcuts to VNC locations that open in Screen Sharing.app. I would like to specify where the windows go, and their size, when these screenshares launch – one maximized on my main monitor, and two overlapping on my secondary monitor.

Is there a key I can use with the open command that will allow me to specify this? If not, how else could I accomplish this?

Update: I'm now using AppleScript instead of a shell script, but the purpose is the same.

Best Answer

Having a look at the man page for open, it doesn't seem to allow it, but you can use applescript to resize and position windows:

tell application "Finder" to set the bounds of window 1 to {325, 465, 1095, 926}

The numbers are the coordinates in pixels of the top left and bottom right of the the window:

  1. top left corner x
  2. top left corner y
  3. bottom right corner x
  4. bottom right corner y

If you set the windows as you like them, you can use applescript to tell you the current bounds:

tell application "Finder" to get the bounds of window 1

The window numbers are assigned going from front to back, starting at 1.

If you prefer, you can also refer to windows by name instead of id, though this won't work if the name of the window is different every time:

tell application "Finder" to get the name of window 1
tell application "Finder" to set the bounds of window "name" to {325, 465, 1095, 926}

Update: Coordinates for multiple monitors behave as though a single large display were present.