Building an Automator Script To Snap Windows Into Position

If you would like to precisely position your application windows without using any third-party tool, you can build a versatile script in Automator to snap windows to portions of your screen. You can give yourself multiple options such as the Left Third, Right Two-Thirds, corners or anything you can come up with. Then you can put the workflow into the Services menu where you can quickly trigger it with a keyboard shortcut. The framework in this JavaScript/JXA script can be used to develop your own customizable window organizing tool.



Here is the final script:

function run(input, parameters) {
	
	app = Application.currentApplication();
	app.includeStandardAdditions = true;

	var frontAppName = Application("System Events").processes.whose({frontmost: {'=': true }})[0].name();
	var frontApp = Application(frontAppName);

	var areaChoices = ["Left Third", "Right Two-Thirds", "Top Left Corner"];
	var area = app.chooseFromList(areaChoices, {withPrompt: "Where?"});

	var displayText = app.doShellScript("/usr/sbin/system_profiler SPDisplaysDataType");
	var displayInfo = displayText.split("UI Looks like: ");
	
	for (var info of displayInfo) {
		if (info.indexOf("Main Display: Yes") > -1) {
			var infoPieces = info.split(" ");
			displayWidth = infoPieces[0];
			displayHeight = infoPieces[2];
			break;
		}
	}
	
	if (area == "Left Third") {
		frontApp.windows[0].bounds = {x: 0, y:0, width: displayWidth*.3333, height: displayHeight};
	} else if (area == "Right Two-Thirds") {
		frontApp.windows[0].bounds = {x: displayWidth*.3333, y:0, width: displayWidth*.6667, height: displayHeight};
	}		
}

Comments: One Response to “Building an Automator Script To Snap Windows Into Position”

    inghilt munoz
    4 months ago

    It has been extremely useful, and the explanation very clear. many thanks 💚

Comments Closed.