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)