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.
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
suggestion to is to use this:
tell application "Safari"
set the bounds of window 1 to {260, 21, 1660, 1400}
activate
end tell
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With