Repeatedly Position a Window (For Machines That Suffer From the Window Positioning Bug)

Howdy folks, I'm sharing this macro to (hopefully) help others who have had issues with the window positioning bug.

See these posts/comments for more information:

This macro uses three variables set from within Keyboard Maestro, and then an AppleScript to repeatedly move the target window until it's position matches the coordinates supplied by the variables.

Setup and use is fairly straight-forward and well documented in the macro itself. Special shoutout to @Nige_S for help polishing the AppleScript.

In my testing over the last 6 months it works quite well. But feel free to supply feedback if for some reason it doesn’t work properly or if you feel it can be improved.

-Chris

13)Position window (subroutine).kmmacros (22 KB)

Macro screenshot (click to expand/collapse)

AppleScript (click to expand/collapse)
### Requires Keyboard Maestro 8.0.3+ ###
set kmInst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
	set appName to getvariable "instance__App Name" instance kmInst
	set windowFrame to getvariable "instance__Window Frame" instance kmInst
	set windowName to getvariable "instance__Window Name" instance kmInst
	set windowIndex to getvariable "instance__Window Index" instance kmInst
	set commType to getvariable "instance__Command Type" instance kmInst
end tell

set windowIndex to windowIndex as integer --sets window index to integer for use as a variable

set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ","}
set targetPos to {0 + (text item 1 of windowFrame), 0 + (text item 2 of windowFrame)}
set targetSize to {0 + (text item 3 of windowFrame), 0 + (text item 4 of windowFrame)}
set AppleScript's text item delimiters to oldTIDS

tell application "System Events"
	tell application process appName
		
		--send command to window with specified name
		if commType is "Window Name" then
			tell (first window whose name contains windowName)
				repeat until (its position = targetPos) and (its size = targetSize)
					set its position to targetPos
					set its size to targetSize
					delay 0.1
				end repeat
			end tell
			
		else
			
			--send command to window with specified index
			tell window windowIndex
				repeat until (its position = targetPos) and (its size = targetSize)
					set its position to targetPos
					set its size to targetSize
					delay 0.1
				end repeat
			end tell
		end if
		
	end tell
end tell

return {windowIndex, commType}

UPDATE Monday, January 02, 2023
I converted this to a subroutine and added the ability to specify a window index instead of a name. See post 1 to download the updated macro.

2 Likes