Create a Quick Action To Center Any Window On a Mac

Learn a little bit of JavaScript by creating an Automator Quick Action to center the current window on your Mac. You can assign this to a keybard shortcut and also alter the script to position the window in other ways.



Here’s the code:

function run(input, parameters) {
	
	var frontAppName = Application("System Events").processes.whose({frontmost: {'=': true }})[0].name();
	var frontApp = Application(frontAppName);
	var topWindow = frontApp.windows[0];

	var windowBounds = topWindow.bounds();
	
	var desktopBounds = Application("Finder").desktop.window().bounds();
	
	windowBounds.x = (desktopBounds.width - windowBounds.width) / 2 + desktopBounds.x;
	windowBounds.y = (desktopBounds.height - windowBounds.height) / 2 + desktopBounds.y;
	
	topWindow.bounds = windowBounds;
}

Comments: 10 Responses to “Create a Quick Action To Center Any Window On a Mac”

    Robert Salter
    3 years ago

    Followed your code. Works fine with all Apple Applications, however with all of my 3rd party applications, I get the following error message: The action “Run JavaScript” encountered an error: “Error: TypeError: undefined is not an object (evaluating 'topWindow.bounds')” Would you be able to briefly explain the error code? Thank You

    3 years ago

    Robert: Sounds like the app you using doesn't have a regular main window. Most apps do.

    Jim Rietz
    3 years ago

    I found your video interesting and started to use in with my MBP and external monitor, which is positioned above and half to the right. I did some digging and it seems that javascript does not have the ability to determine the number of screens (monitors) and only has the over all height and width of both monitors moving the window to an odd location. It appears that AppleScript may be able to do that.

    3 years ago

    Jim: Just use my suggestion at the end of the video to handle that.

    Christoph
    3 years ago

    Very cool tutorial, thank you!

    Robert Primmer
    3 years ago

    Gary, one nit: the code included on this page differs from the video, adding "+ desktopBounds.x|y". This addition doesn't change operation as (at least on my system) as x & y = 0. This does matter however when you add a second monitor. Jim Rietz: this might be your issue. When I used my iPad as Sidecar (on left of main display) I had to change the code to adjust not just for main display resolution, but adjust for new center point (windowBounds.x = ((3008 - windowBounds.width) / 2) - 213.5;

    Nils
    3 years ago

    Hi, this is really cool and thank you very much :)
    I would like to follow up on Robert Salter's comment and confirm that the code doesn't seem to be working with (at least some of) non-Apple apps (Acrobat Reader for instance, or WhatApp desktop).
    Also, the code as published here does centre horizontally, but not vertically (at least on my screen). The windows go top centre.
    Apart from these small issues, this is great. Thanks again!

    Nils
    3 years ago

    Ah, it turns out that one a window exceeds a certain size, then the code automatically centres it top centre. If it's small enough though, then it's 'centre centre' :)

    Will
    2 years ago

    Regarding the 'topWindow.bounds' issue mentioned above, I was not able to move some windows but was able to avoid the error by including a check, for example:

    var windowBounds;
    try {
    windowBounds = topWindow.bounds();
    } catch { return; }

    Daniel Payne
    2 years ago

    As noted by others doesn't work on many 3rd part apps, or some Apple apps (Maps/Calculator). Nevertheless great little utility.

Comments Closed.