Automatically Open, Position And Size Your Mac Application Windows

How to programmatically set windows to a certain size and position on your desktop with applescript

applescript,max,mac osx

03/04/2013

Recently I was helping a friend with the technology for his performance piece. He wanted to be able to initiate, from within Max, a Mac application (written in openFrameworks), and ensure that it opened on a secondary monitor being displayed on a projector screen, so that the switch between applications would appear seamless. I've searched around and found varying suggestions on how to do this, none of which quite worked for me, so I figured I'd post my approach.

Set up a push button in Max (or whatever sort of interaction you want to initiate the launching of the app), and connect to a message object. In the message object, insert the following code:


;
max launchbrowser file:////Users/[username]/[path]/[to]/[Application].app

where obviously the information in brackets is replaced with the path to the application you want to launch. If you need to know that path, simply open terminal, and drag your application in to the terminal window. This will create the absolute path to the file, which you can copy and paste into the above script after 'file:////'. Once you have this in place, you can test your button - it should launch your application. Now how to position it where you want it to be?

You'll need to write a little Apple Script. I had no experience in this whatsoever, but the language seems incredibly intuitive. Here's the script to position the window:


tell application "System Events"
  set position of first window of application process "[ApplicationName]" to {0, 0}
  set size of first window of application process "[ApplicationName]" to {1920, 1080}
end tell

Open up the Apple Script Editor, paste in that code, and change [0,0] to the screen coordinates for your second monitor (may take a little trial and error). Replace [ApplicationName] with the actual name of your application (the name that appears in the dock, for example, when the application is open. Then change the size to the width and height you want your window to be. Then, in the Apple Script Editor, click Save As... and choose 'Application' as the type.

Now, back in max, you can just create another message box with the same code as before, only now it will be launching your apple script and not your application:

;
max launchbrowser file:////Users/[username]/[path]/[to]/[ScriptName].app