Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write an Applescript to resize an application window on external monitor

I want to write an Applescript to resize the Safari window on my external screen. I have a Macbook Air Screen of 1440x900 and an Asus external monitor of 2560x1440. I want to keep Safari on my external monitor, with a 170 pixel gap on the left-hand side. I wrote the following basic script:

tell application "System Events"
    tell application "Safari"
        activate
        set bounds of window 1 to {170, 0, 2560, 1440}
    end tell
end tell

The result is that the only Safari window open gets moved from my external monitor to my Macbook Air (Mojave) screen with the correct width (2560-170 = 2390) and incorrect height (900 and not the 1440 specified).

How do I ensure the correct width and height AND keep the window on my external monitor. Many thanks.

like image 974
Colin Avatar asked Sep 14 '25 01:09

Colin


2 Answers

In this case, you don't need to use System Events; Safari knows how to resize its own windows. All you need to do is get the bounds of the window, change the last two items of the returned list, and then set the bounds to the revised array. This will change the size without moving the position of the window.

tell application "Safari"
    activate
    set old_bounds to bounds of window 1
    set item 3 of old_bounds to 2560
    set item 4 of old_bounds to 1440
    set bounds of window 1 to old_bounds
end tell
like image 185
Ted Wrigley Avatar answered Sep 17 '25 04:09

Ted Wrigley


suggestion to is to use this:

tell application "Safari"
    set the bounds of window 1 to {260, 21, 1660, 1400}
    activate
end tell
like image 30
Miklos Philips Avatar answered Sep 17 '25 04:09

Miklos Philips