Problem setting terminal window bounds with AppleScript

applescriptterminal

Problem trying to solve

I use multiple terminal windows to develop web applications. I'll have a terminal open for vim, a terminal open for git and a terminal open for basic file/directory commands. The terminal application doesn't remember window settings so whenever I quit the terminal or have to restart I must manually resize and position my terminal windows. I would like to automate this process using AppleScript.

  • 1 window, big, fills up left half of screen (vim)
  • 2 windows, smaller, stacked on top of one another, filling up other half of screen

My attempted solution

tell application "Terminal"

    activate
    set the bounds of the front window to {5, 0, 1000, 950}
    do script "clear" -- opens up the second window
    set the bounds of the front window to {1105, 0, 400, 500}
    -- haven't gotten to third window yet   

end tell

The problem

The second window, the one being created by do script "clear", is being positioned oddly. The left, top and height are all behaving as expected. However, the window is set to it's minimum width, regardless of the value passed in the width parameter. It could be 1000 and it's still set to minimum. I've search high and low on Google, SO, Super User and Ask Different with no luck.

Any advice on what I'm doing wrong or what could be causing this issue with the weird width on the window created by do script "clear"?

Disclaimer

I'm an AppleScript beginner. The syntax is unlike anything I've ever worked with (PHP, VBA, JavaScript) and I'm almost positive this is not the best, or even correct, way to be doing what I want to do. However, I don't know AppleScript enough to really tell, other than gut feeling.

Best Answer

The window bounds are a list of coordinates {left, top, right, bottom}. You probably intended "400" to be the width, but it's the position of the right edge of the window and 400 is to the left of 1105, so you get a minimum width window. Change 400 to 1105 plus the desired width, e.g., 1505.

But before you pursue this further, Terminal has a better solution for this: Window Groups. If you set up a group of windows and save them as a Window Group, each time you open that group it will create windows with the same layout and appearance.

Window > Save Windows as Group…

You can even tell Terminal to open a selected window group at startup:

Terminal > Preferences > Startup > On startup, open: > Window group:

(As a shortcut, when creating a window group there's a checkbox for making it the startup group.)

To automatically run particular commands in those windows, you can create custom settings profiles and specify the command with

Terminal > Preferences > Settings > [profile] > Shell > Startup > Run command:

then create each window with the appropriate profile.

Going further, in Mac OS X Lion 10.7 you can have window groups automatically restore commands without creating custom profiles, by creating the terminals using

Shell > New Command

instead of running the command inside the terminal shell. When creating the window group, you can check "Restore all commands". (By default, it will restore a small set of "safe" commands, but you must explicitly tell it if you want it to re-run all commands when opening the group.)

Moreover, Lion Terminal supports Resume and will automatically restore all your windows each time you open Terminal. It will even restore "safe" commands for terminals created with New Command.