Centering macOS open/save windows

I'm well aware of Center Next Engine Window, but it doesn't work on macOS open/save dialog windows (as they're clearly not new windows from the KM engine).

Is there any effective way to center them, or do you just have to put up with them wherever they appear? (I think any solution would have to use AppleScript, as I don't believe KM can do anything once that window is open?)

-rob.

I wanted to help you, but when I clicked on "Help" for that action, it brought me to a page that "doesn't exist." When using Google to do the search I found the page:

https://wiki.keyboardmaestro.com/action/Set_Next_Engine_Window_Position

I wish this action also centered the KM Engine's Debugger window. That window seems to have a mind of its own.

The Alert action window also does not respond to the next engine action. I rolled my own applet to toggle the debugger window and set it’s position; here’s the underlying AppleScript if you want to take a look at it and do something similar.

set xPos to 0 as integer
set yPos to 510 as integer
set xSize to 756 as integer
set ySize to 448 as integer
set delayInteger to 0.2
set theWindow to "Keyboard Maestro Debugger"

set apscDone1 to false
set loopCount1 to 0
repeat until apscDone1 is true or loopCount1 is 15
	try
		
		tell application "Keyboard Maestro Engine"
			# Determine if work apps are open
			set workAppsOpen to getvariable "LB__Work Apps Open"
			# Determine if opening or closing debugger window
			if window "Keyboard Maestro Debugger" exists then
				set openingDebugger to "False"
			else
				set openingDebugger to "True"
			end if
		end tell
		
		# Open debugger window and proceed with macro
		tell application "Keyboard Maestro"
			set macroAction to my kmToggleDebuggerScript(debuggerActionsXML)
		end tell
		
		# position window if opening and working
		if openingDebugger is "True" and workAppsOpen is "True" then
			tell application "System Events"
				tell application process "Keyboard Maestro Engine"
					# wait for window to appear
					repeat until window theWindow exists
						delay delayInteger
					end repeat
					tell window theWindow
						# set its size
						repeat until its size is {xSize, ySize}
							set its size to {xSize, ySize}
							delay delayInteger
						end repeat
						# set its position
						repeat until its position is {xPos, yPos}
							set its position to {xPos, yPos}
							delay delayInteger
						end repeat
					end tell
				end tell
			end tell
		end if
		
		set apscDone1 to true
	on error
		set apscDone1 to false
		set loopCount1 to loopCount1 + 1
	end try
end repeat

--------------------------------------------------------
--» HANDLERS
--------------------------------------------------------
on kmToggleDebuggerScript(theScript)
	tell application "Keyboard Maestro Engine"
		do script theScript
	end tell
end kmToggleDebuggerScript

--------------------------------------------------------
--» PROPERTIES
--------------------------------------------------------
property debuggerActionsXML : "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<array>
	<dict>
		<key>ActionUID</key>
		<integer>15155310</integer>
		<key>DebuggerAction</key>
		<string>Toggle</string>
		<key>IsDisclosed</key>
		<false/>
		<key>MacroActionType</key>
		<string>DebuggerAction</string>
	</dict>
	<dict>
		<key>ActionUID</key>
		<integer>15155326</integer>
		<key>DebuggerAction</key>
		<string>Continue</string>
		<key>IsDisclosed</key>
		<false/>
		<key>MacroActionType</key>
		<string>DebuggerAction</string>
	</dict>
</array>
</plist>
"

griffman,
Just use the resize/move window action will work

image

The issue with that might be that when the Debugger window activates, it generally pauses all currently executing macros, which means if this action is placed after the toggle debugger action, it won’t execute. At least that’s how it works on my system and why I had to roll my own solution.

EDIT: Just realized you might not be talking about positioning the debugger window, if so, disregard me comment.

cdhomer,
I mean the
" Center Next Engine Window, but it doesn't work on macOS open/save dialog windows (as they're clearly not new windows from the KM engine)."
The manipulate window action can actually do this.

I correct my response with the poster.
thank

1 Like

Great point.

Forgot to mention that I had tried that first, as it's the obvious solution—sorry.

It doesn't seem to work, regardless as to if it's called before or after the export window appears.The first trick is that the window doesn't have an obvious title, but I was able to get that easily enough via our own app Witch.

But even knowing that, it fails as it can't find the window:

2023-11-07 05:05:57 Action 15336195 failed: Manipulate Window could not find any matching windows
2023-11-07 05:05:57 Manipulate Window could not find any matching windows in macro “•main | MacroBackerUpper” (while executing Center Window Named “Export Macros”).

I have a recollection of dealing with this many years ago in another macro, and the AppleScript method as shown by Chris was the real solution ... I had just lost any code snippets I had related to that method. I'll play around with Chris' code this morning, thanks!

-rob.

griffman,
make sure you enter the window title correctly because mine is using that title, yours could be different.
How do you find the correct window title?
You can find the correct window name using the token %WindowName%1%
Create a shortcut key macro and activate it over the current Open/Save dialog focus, and display the name using %WindowName%1% token

you also need to ensure that those window can be moved manually using mouse first, as not every open/save similar window can be moved, some are fixed in place.

I believe this is the problem, and you can try it on your KM: Just select File > Export > Export All Macros as Folder.

Once that dialog is onscreen, macOS is in charge, and KM can't interact with the window in any way. (Our app, Witch, gets the window title via the macOS Accessibility system, so I'm positive I have its actual title correct.)

But because macOS takes over when that window is onscreen, no normal KM methods can interact with that window. That's why I think Chris' AppleScript is the right solution. I did find a copy of it in an older macro, but it doesn't seem to work with the Export Macros window. Here's a simplified version of it that I'm just trying to run from Script Editor while the export window is onscreen (and not centered), but it doesn't do anything.

----------------------------------------------------------
# Author:				Chris Thomerson
# Updated by Rob Griffiths for his particular needs
----------------------------------------------------------

set theCenter to 400
set fromTop to 200
-- (Not actual center; I just chose a couple random values for testing)
set theWindowName to "Export Macros"

display dialog theWindowName

tell application "System Events"
	tell application process "Keyboard Maestro Engine"
		repeat until window theWindowName exists
			delay 0.1
		end repeat
		
		tell window theWindowName
			
			--gets user prompt position and size
			set uipSize to its size
			
			--separates user prompt individual coordinates
			set {uipXSize, uipYSize} to uipSize
			set adjFromTop to fromTop - uipYSize - 17
			set adjFromLeft to theCenter - uipXSize / 2
			set its position to {adjFromLeft, adjFromTop}
			perform action "AXRaise"
		end tell
	end tell
end tell

I guess I'll just live with it appearing wherever it wants to appear :).

-rob.

That “ window” is not actually a window. It’s a sheet, part of window 1 (which is the Editor).

The following AppleScript can interact with it, though in my limited testing, setting it’s size and position is finicky.

EDIT: You may need to even run this via an asynchronous execute a macro action to get it to work.

tell application "System Events" to tell application process "Keyboard Maestro" to tell window 1
	tell (first sheet whose description contains "export macros")
		set its size to {500, 500}
		set its position to {25, 25}
	end tell
end tell

EDIT 2: the following macro works to open that sheet and then interact with it. You can tinker with it as you like.

Download Macro(s): Position export macros sheet.kmmacros (3.2 KB)

Macro-Image

Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
  • The user must ensure the macro is enabled.
  • The user must also ensure the macro's parent macro-group is enabled.
System Information
  • macOS 13.6
  • Keyboard Maestro v11.0.1

Thanks Chris … I think I also have to wait for that sheet to appear, but I'm not sure how to do that. It's straightforward with a window, as seen in your earlier script above. But how do you say "wait for first sheet whose description contains 'export macros'" ?

-rob.

Hey Rob, see my EDIT 2 from my initial comment. :wink:

Ha, I replied too quickly :).

And now, with that macro, I can see exactly what you mean and the exact problem: It is a sheet, anchored to the KM editor window. So that makes things much simpler, actually—there's no way to center the export window, but if I just center the KM editor window, the export window will then be centered without any effort.

Just tested, and it works. I save the editor window size/location, center it, open the export window, then restore the window when done. All this for something that's onscreen for just a few seconds, but seeing it out of alignment was really bugging me!

thanks!
-rob.

1 Like

Haha no, I have a tendency of editing my replies afterwards, though I try to avoid doing it too much, and too long after to avoid things like this. So it’s on me. :laughing:

Glad you got it going!