Retina Resolution quirk

I have a quick & dirty script which I’ve been using for years to set the window size for Safari.

The original script put the upper left corner of each window at 0,0. I decided that I wanted the upper left to be at the left edge of the screen and start about one-third to one-half of the screen from the top with the intent of the bottom left of the window to be at the bottom left corner of the screen.

I found some code here to allow me to determine the resolution of the screen, but it returns what I presume is the non-retina resolution (which is half of what macOS reports). There were references in several posts to using Jon’s Commands, but I suspect that it’s no longer available (at least when I tried to find it). I also found some code in Getting Screen Coordinates but that’s really old and the data returned isn’t parseable by the code.

My problem is that the top of the window doesn’t start where I want it and it isn’t as tall as I thought it would be.

I’ve tried changing the height value (1050 here), but that doesn’t seem to change the actual height as I would expect.

I know I’m missing something here, but can’t quite figure out what.

Thanks for looking at this and pointing out my mistake(s).

Cheers,
Jon

set screenWidth to (word 3 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver | grep -w Width") as number)

set screenHeight to (word 3 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver | grep -w Height") as number)

tell application "Safari"
	activate
	try
		-- Horizontal, Vertical, Width, Height
		set bounds of every window to {0, (screenHeight-1050), (screenWidth / 2), 1050}
	end try
end tell

Try the following, which works on my 4k monitor:

tell application "Finder" to set {x1, y1, x2, y2} to bounds of desktop's container window
tell application "Safari"
	activate
	set bounds of every window to {0, ((y2 - 25) div 3) + 25, x2, y2} -- assumes menu height is 25
end tell

If the Safari window extends beneath the dock, and if this is not desired, try the following:

use framework "AppKit"
use framework "Foundation"
use scripting additions

set {x1, y1, x2, y2} to getVisibleFrame()
tell application "Safari"
	activate
	set bounds of every window to {x1, ((y2 - y1) div 3) + y1, x2, y2}
end tell

on getVisibleFrame()
	set theScreen to current application's NSScreen's mainScreen()
	set {{x1, y1}, {w1, h1}} to theScreen's frame()
	set {{x2, y2}, {w2, h2}} to theScreen's visibleFrame()
	return {x2 as integer, (h1 - h2 - y2) as integer, (x2 + w2) as integer, (h1 - y2) as integer}
end getVisibleFrame

You might try sizing a window manually and then have your script get bounds of window 1. Then change the ‘get’ to ‘set’ and modify the rest of the line accordingly.

j.a.duke. Two quick comments regarding your script.

The first line of your script reports an error on my Sonoma computer. I assume it works with older versions of macOS, but it’s only of use if the native display resolution and the current display resolution (as set in System Settings) are the same. With a Retina or 4k display, this is typically not the case.

The Finder is most commonly used in AppleScripts to get current display resolution. NSScreen is used in my second script to get this data, and it also returns current display resolution.

To set the position and size of a Safari window, the bounds property is normally used. This property uses screen coordinates in the format {x1, y1, x2, y2}–window width and height are not used.

If the script is only going to be used on one display, Mockman’s suggestion is an excellent one. On my 4k monitor–which has a current resolution of 1920 x 1080 with the dock hidden–the following script sets the Safari window to the bottom half of the visible screen.

-- set Safari window to bottom half of visible screen
-- assumes current resolution of 1920 x 1080 with dock hidden

set x1 to 0
set y1 to ((1080 - 25) div 2) + 25 -- assumes menu height of 25
set x2 to 1920
set y2 to 1080

tell application "Safari"
	activate
	set bounds of every window to {x1, y1, x2, y2}
end tell

For anyone running macOS Monterey or newer, the following shortcut is functionally equivalent to the AppleScript in the preceding post. A possible advantage of this shortcut is the numerous methods by which it can be run (shortcut menu, dock icon, keyboard shortcut, and so on), as well as the options in the first two actions.

It should be noted that the Move and Resize actions have many presets, although they don’t always work as expected. The following shortcut uses the Coordinates and Dimensions options, which allow greater precision.

Safari Set.shortcut (22.1 KB)