Centering front window using only AppleScript when you have multiple screens

Howdy folks, I'm hoping some of the AppleScript gurus can chime in here.

I'm trying to learn window positioning using AppleScript, particularly when there are multiple screens involved. My reason for this is to be able to streamline certain macros by having a single AppleScript do everything, instead of breaking it up into multiple AppleScripts and Keyboard Maestro actions.

To give an example, the below macro allows me to quickly add an auto-correction to one my Typinator sets. It's designed to copy the misspelled word, open the Typinator "Create new item" window, select the appropriate set, and click the suggestions menu. It works flawlessly and I've been using it for many months. But recently I decided I wanted to center it in the front screen, and I wanted to do that from within the AppleScript so as not to have to break up the AppleScript to allow Keyboard Maestro to position it using the manipulate a window action.

So just below is the part of the script that does this for me.

--set Typinator window position
tell window winName
	set winSize to its size
	set winXsize to item 1 of winSize
	set winYsize to item 2 of winSize
	set its position to {(xRes - winXsize) / 2, (yRes - winYsize) / 2}
end tell

And below this is the entire AppleScript for context.

----------------------------------------------------------
# Author:			Chris Thomerson
# Version:			1.3
# Version History
#					1.3 (Added script to set Typinator window position)
#					1.2 (Added selecting English auto correction)
#					1.1 (Combined all previous scripts into this script)
#					1.0 (Initial script)
# Created:			Monday, September 6, 2021
# Modified:			Friday, December 10, 2021
# macOS:			11.5.2 (Big Sur)
# I claim no responsibility nor guarantee compatibility
# As with any kind of custom scripts, these must be tested thoroughly on each person's device
# May be used and distributed freely
----------------------------------------------------------

--set AppleScript variables
set setName to "English auto correction"
set winName to "Create new Typinator Item"

--get screen variables from Keyboard Maestro
tell application "Keyboard Maestro Engine"
	set xRes to getvariable "Typinator_xRes"
	set yRes to getvariable "Typinator_yRes"
end tell

--create a new entry from clipboard
tell application "Typinator"
	create from selection
end tell

tell application "System Events"
	tell application process "Typinator"
		
		--wait until the Typinator window appears
		repeat until window winName exists
			delay 0.1
		end repeat
		
		--set Typinator window position
		tell window winName
			set winSize to its size
			set winXsize to item 1 of winSize
			set winYsize to item 2 of winSize
			set its position to {(xRes - winXsize) / 2, (yRes - winYsize) / 2}
		end tell
		
		--wait until the set menu appears
		repeat until pop up button 1 of window winName exists
			delay 0.1
		end repeat
		
		if value of pop up button 1 of window winName is setName then
			
			--do nothing if current set is already English auto correction
			
		else
			
			--click the set menu
			click pop up button 1 of window winName
			
			--wait until English auto correction is in the menu
			repeat until menu item setName of menu 1 of pop up button 1 of window winName exists
				delay 0.1
			end repeat
			
			--select the English auto correction menu item
			click menu item setName of menu 1 of pop up button 1 of window winName
			
		end if
		
		--pauses until suggestions button exists
		repeat until menu button 1 of group 1 of window winName exists
			delay 0.1
		end repeat
		
		--clicks the suggestion button
		click menu button 1 of group 1 of window winName
		
	end tell
end tell

tell application "Keyboard Maestro Engine"
	set value of variables whose name starts with "Typinator_" to "%Delete%"
end tell

And below this is the macro itself so you can see how I'm setting the screen resolution variables referenced in the AppleScript.

This works just fine for me, reliably centering the window on whatever screen is frontmost. But what I would like to know is if there's a better way of doing this that way I can become more efficient.

Any help is appreciated, thanks in advance!

-Chris

Typinator- Create expansion from selection (English).kmmacros (35 KB)

Hey Chris,

AppleScript sees a multi-screen desktop as one contiguous space.

Take a window and move it around your set up whilst taking snapshots of position and size. That will show you where your screen boundaries are.

You can play with NSScreen in AppleScriptObjC to see more detail, but you'll still have to deal with the contiguous space for referencing with System Events.

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/02/19 00:46
# dMod: 2017/02/19 00:49
# Appl: AppleScriptObjC
# Task: Screen info using NSScreen
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @Screen, @Information, @NSScreen
--------------------------------------------------------
use framework "AppKit" -- for NSScreen
--------------------------------------------------------

set theScreens to (current application's NSScreen's screens())
set screenCount to theScreens's |count|()

set screenWithMenu to current application's NSScreen's screens()'s objectAtIndex:0
set screenWithMenuFrame to screenWithMenu's frame() as list

# This breaks with Mojave.
# set screenWithMenuFrameSize to (screenWithMenu's frame()'s |size|()) as list
# set screenWithMenuVisibleFrame to screenWithMenu's visibleFrame()
# set screenWithMenuVisibleFrameSize to (screenWithMenu's visibleFrame()'s |size|()) as list

set screenWithMenuFrameSize to (screenWithMenu's frame()'s item 2)

--------------------------------------------------------

I only have one screen presently, so I can't test for more detail. If you want to know more then ask on the Late Night Forum.

-Chris