Setting Window Sizes Precisely With a Script

You can use JXA in the Script Editor or Automator to set the location of a window to an exact location and size. It is easy to set up the one-line script and reuse it by simply changing the application name and size numbers. You can also create an Automator service to repeat a setting you use often.



Here is an example of the JXA script I am using in the video:
Application("Safari").windows[0].bounds = {"x":0, "y":0, "width":1280, "height":697}

Comments: 19 Responses to “Setting Window Sizes Precisely With a Script”

    Mark V.
    6 years ago

    Hello Gary. Informative video. Two questions. First I tried to build an automator service for Firefox and it would not show up under services when I used in "Firefox" or in "Any Application".

    Second, where do i find identifiers for the different windows? I was trying to put apps on different desktops and then also on my main laptop screen. Now I'm using the monitor attached to the laptop as ".windows[0]".

    6 years ago

    Mark: Sometimes those services can be finicky. Try quitting Firefox and Automator. Then launch just Automator. Create the service from scratch. Save it. Then launch Firefox and see if it is there.
    The windows for an app are just numbered. I'm not sure you can get an "identifier" that is reliable.

    Mark V.
    6 years ago

    Thanks for the response on the first part. As for the second, I guess I was trying to put apps on other desktops. I tried 1,2&3 but none worked, I tried -1 as well. Does this

    Application("Safari").windows[0].bounds

    Always have to be 0?

    6 years ago

    Mark: windows[0] means the first window of the application. For instance, if Safari had two windows open, one would be windows[0] and the other windows[1]. It doesn't have to do with screens. Not sure if you can set the bounds to another screen. There is probably another property, separate from bounds, that represents the screen. I don't know what that would be offhand.

    Mark V.
    6 years ago

    Ok, I'm with you now! Thanks!

    Mark Scola
    6 years ago

    Hey Gary, I like this idea. How's about posting the text code?

    6 years ago

    Mark: I just put an example under the video above.

    Mark Scola
    6 years ago

    Thanks Gary, it works perfectly. Nice trick to know!

    nick
    6 years ago

    Always amazed of all the little utilities in the Mac OS. Thanks Gary, I'm forever resizing windows on the desktop and this is a clever solution. Question: to make the service available in other apps would I just add more script lines below the Safari one, following the same syntax?

    6 years ago

    nick: No, it would get a lot more complex. If you had two lines, one for Safari and one for Mail, then every time you ran the script it would resize the first window in both apps. You'll need to add more code to figure out the current app and use that instead. It will take some AppleScript/JXA know-how.

    Actually, if you just replaced Application("Safari") with Application.currentApplication() it may do what you want.

    Lali Raj
    6 years ago

    Thanks Gary, it will really help instead of resizing an apps screen. Once again thanks for the tip

    Lali Raj
    6 years ago

    Gary is there anyway one can create a similar script for finder window?

    6 years ago

    Lali: Just change the application name to "Finder" instead.

    Brett
    6 years ago

    Thanks Gary, appreciate this post. This works great with two application windows side by side, triggered by a services keyboard short-cut. Thanks again!

    Lali Raj
    6 years ago

    Gary I setup the JXA script for TextEditor app, it worked when I tested it but when I restarted the computer it did not work. Why?

    6 years ago

    Lali: I don't know. Try building it again. Test, experiment, explore, etc.

    Cory
    6 years ago

    Thanks Gary! I really like this script.
    Someone asked how to do this with multiple monitors. add "-" before the position of the window. Basically the second display is negative position.
    Here is what I used to equally size 4 windows on 2 monitors:
    {"x":0, "y":0, "width":960, "height":1200}
    {"x":960, "y":0, "width":960, "height":1200}
    {"x":-960, "y":0, "width":960, "height":1200}
    {"x":-1920, "y":0, "width":960, "height":1200}

    Cory
    6 years ago

    Also Gary, How can I use this script to resize windows of any open application. Say I want safari, chrome and firefox all open and resize them equally.
    I have tried:
    Application(*)
    but I get errors.
    Thank you!!

    6 years ago

    Cory: You can't use * as a wildcard or anything like that. You'd need to do a line for each app and window, or just change the app name and values and run it again and again.

Comments Closed.