Managing windows

Hi, I’m trying to figure out a way to manage windows. I have Moom, Mosaic, keyboard Maestro, but I haven’t found a way to do what I want.

I want to be able to hit a shortcut and have my mac look at the current app, and if there is one window, set it to half the width of my screen but center it. If I have two windows, then set the width to half the width and put them side by side. If I have 3 windows, split the screen in 4, and set them in the four corner, same with 4 windows. If I have 5 or 6, split the screen in 3 on the width and half on the height. maybe have an option with 8 windows, split in 4 on the width etc,…

With Applescript, you have to call the app by name, so it would work only on one app. I want this to work whether I’m in Safari, Word, Chrome or whatever. Anyone tried this?

1 Like

You can get names of all running apps in AS:

Should be possible to iterate over that list. Or some variant with System Events. Sorry not at my Mac to check.

thanks dfay,

I found that I could use the command
set myApplication to name of current application

The only problem is that it only works if I’m in Script editor or Script Debugger:-(

You want to look for a tiling window manager. A quick google search revealed three for macOS: kwm, amethyst, chunkwm. There may be more.

Thanks Jahala, but have you looked at those??? Man talk about geeking out! You have to get Brew to install them, and they are dependent on a bunch of other stuff. I’m looking to help my father out with this, so NO way is he going to mess with this. I don’t know why there isn’t an app that does this with a simple GUI. Moom or Mosaic could probably add that too.

dfay,

I found that the right way to get it is with

tell application “System Events”
set myApplication to name of first application process whose frontmost is true
end tell

I have now a working script that works as I want (mostly). It works for the main display only and up to 8 windows. If anyone is interested I’ll post it.

I went on the same quest a few weeks or months ago and failed to find anything. As you said, the existing solutions are too geeky. And as I’m usually running more than 8 open windows simultaneously, I fear your script would not work for me either.

yes please, thanks! would love to see it.

I have used most of them in the past. It is a bit involved to get it setup, but once it is running, I found them easy to use. Of course, I used to be a computer programmer who used to write C and C++ code to create user interfaces in GUI applications, so what is easy for me is sometimes very daunting to those without that background.

Personally, I use Magnet. The problem I always had with the tiling windows managers is the order in which the non-main applications were arranged. I was always fiddling to get them where I wanted.

Magnet has mouse drag and keyboard shortcuts to resize the windows in more ways than I can remember so it works well for me. I can put my windows where I want them. I would like to see your script, though. I am intrigued.

Here you go

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "System Events"
	set myApplication to name of first application process whose frontmost is true
end tell
set screenWidth to 1680 --2560 --1920
set screenHeight to 1440
tell application myApplication
	set myWindowsCount to count of windows
	if myWindowsCount = 0 then
		display dialog "You don't have any windows open!" buttons "OK" default button 1
	else if myWindowsCount ≤ 2 then
		set myWidth to screenWidth / 2
		set bounds of window 1 to {0, 0, myWidth, screenHeight}
		try
			set bounds of window 2 to {myWidth, 0, myWidth * 2, screenHeight}
		end try
	else if myWindowsCount ≥ 3 and myWindowsCount ≤ 4 then
		set myWidth to screenWidth / 2
		set myHeight to (screenHeight - 22) / 2
		set bounds of window 1 to {0, 0, myWidth, myHeight}
		set bounds of window 2 to {myWidth, 0, myWidth * 2, myHeight}
		set bounds of window 3 to {0, myHeight + 22, myWidth, (myHeight * 2) + 22}
		try
			set bounds of window 4 to {myWidth, myHeight + 22, myWidth * 2, (myHeight * 2) + 22}
		end try
	else if myWindowsCount ≥ 5 and myWindowsCount ≤ 6 then
		set myWidth to screenWidth / 3
		set myHeight to (screenHeight - 22) / 2
		set bounds of window 1 to {0, 0, myWidth, myHeight}
		set bounds of window 2 to {myWidth, 0, myWidth * 2, myHeight}
		set bounds of window 3 to {myWidth * 2, 0, myWidth * 3, myHeight}
		set bounds of window 4 to {0, myHeight + 22, myWidth, (myHeight * 2) + 22}
		set bounds of window 5 to {myWidth, myHeight + 22, myWidth * 2, (myHeight * 2) + 22}
		try
			set bounds of window 6 to {myWidth * 2, myHeight + 22, myWidth * 3, (myHeight * 2) + 22}
		end try
	else if myWindowsCount ≥ 7 and myWindowsCount ≤ 8 then
		set myWidth to screenWidth / 4
		set myHeight to (screenHeight - 22) / 2
		set bounds of window 1 to {0, 0, myWidth, myHeight}
		set bounds of window 2 to {myWidth, 0, myWidth * 2, myHeight}
		set bounds of window 3 to {myWidth * 2, 0, myWidth * 3, myHeight}
		set bounds of window 4 to {myWidth * 3, 0, myWidth * 4, myHeight}
		set bounds of window 5 to {0, myHeight + 22, myWidth, (myHeight * 2) + 22}
		set bounds of window 6 to {myWidth, myHeight + 22, myWidth * 2, (myHeight * 2) + 22}
		set bounds of window 7 to {myWidth * 2, myHeight + 22, myWidth * 3, (myHeight * 2) + 22}
		try
			set bounds of window 8 to {myWidth * 3, myHeight + 22, myWidth * 4, (myHeight * 2) + 22}
		end try
	end if
end tell

Like I said, it is only working on the main monitor, meaning if you have put windows on a second monitor, they will be counted and placed on the main monitor. If anyone has a solution for that, it’d be great. I’ve tried to get the bounds of the windows, and if the first item is less than 0 to not count it but I can’t figure out the syntax for it.

I should also mention that I am NOT a programmer so there surely a better way to do this.

@MitchWagner I have posted my version of the script, if you want to modify it to fit your need, you’re welcome to use it.

1 Like

Probably above my skill level, alas. Plus I’m not sure I can articulate my needs specifically enough to write a script for it. For one thing, it occurs to me that in any given situation I won’t want to tile ALL my open windows – just some of them.

@MitchWagner that’s why I want to be able to target only ONE monitor, in my case, I would have windows open on the second monitor, that I DON’T want to move, and the ones on the main monitor would need to tile. Still looking for a way to do this.

2 Likes

Since I did my search weeks or months ago, I’ve found that tiling windows is something that I THINK I need but in fact I don’t seem to. 50-50 vertical split serves my needs 99% of the time.

Brew is absolute awesomeness. I wish there was a way to make all my apps installed via Brew.

Brew is absolute awesomeness. I wish there was a way to mke all my apps installed bia Brew

I currently use Moom by Many Tricks Software and am quite pleased with it. Moom has been mentioned multiple times on MPU. I like that you can save window layouts and call them by name or keyboard shortcut. I use Moom with Bunch to set workspaces for different contexts.

I had been using Magnet, which is a good choice if you don’t need to save layouts. Magnet has also been mentioned on MPU.

As to how to choose the appropriate tool, I tend to prefer special purpose tools that do one thing well, especially when that one thing can be done simply. Note that I have and use both Keyboard Maestro and Better Touch Tool, and recommend them both. But I find Moom to have the right balance of features and ease of use for my window management needs.

Like you I am a light user of Mosaic. I use it because it’s included in SetApp and I have minimal needs. I haven’t used AutoLayout but I looked into it since you mentioned it. Here’s what I found:

I don’t think it automatically memorizes the position of an open window for specific apps. It does allow you to use a keyboard shortcut to put the window into the desired position.

The shortcut is “option-command-shift-A”. Here is an example: You want Safari on the left side of the screen and Pages on the right. To setup Safari for the first time, open Safari, then “option-command-shift-A”. It will show available screen positions on the bottom of the screen. Select the one for left half of the screen. Now every time you open Safari, if you type “option-command-shift-A”, it will put Safari on the left side. Do the same for Pages (but for right side).

When you open Safari, type “option-command-shift-A” and Safari moves to the left.
When you open Pages, type “option-command-shift-A” and Pages moves to the right.
“option-command-shift-A” works on the active application.

One thing I’ve noted. If an application is in full screen mode, “option-command-shift-A” will not move the app to the appropriate screen position.