Find coordinates of front most window, including alerts, dialog window, etc

Sometimes I want to set a very specific position for an alert, dialog window, etc.
Using the window tokens and trying to guess their values is always a big confusing to me and something that seems very slow.

I was trying to find a way to have an AppleScript that gets the X and Y values of the frontmost window (which I will drag to the desired position), relative to the top left corner of the screen and then save those values to the clipboard, but no matter what I do, I can't make it work.

For example this script gets the coordinates of Keyboard Maestro's Editor window, but not a dialog box that I "triggered" from a different macro, even when I click it to make it the focus (and frontmost) window:

tell application "System Events"
	set frontApp to first application process whose frontmost is true
	tell front window of frontApp
		set windowPosition to position
	end tell
end tell

It seems that the script is ignoring the dialog as the frontmost window.

I then went to Activity Monitor and saw that I have 3 "osascript" processes running and when I close the dialog window I triggered, I get 2, so that tells me that the process I need to target is osascript so I used:

tell application "System Events"
	tell front window of "osascript"
		set windowPosition to position
	end tell
end tell

but since I have 3, how would it know which to target?
So I get this error, not sure if it's related?
2024-06-08 09:46:32 Action 15910677 failed: Execute an AppleScript failed with script error: text-script:91:99: execution error: The variable position is not defined. (-2753)

So how could I target the frontmost window of the osascript window?

I even tried an IF THEN action with a condition to check the title of the dialog window, but that doesn't work either. It always shows me a "false" result.
image

When I hover over the dialog window using UI Browser I get that the application is osascript and it's window 1. Those are the only 2 things in UI Browser

After some research it seems that a dialog window isn't a "normal" window (I can't explain it in a more basic way than this). It seems that a dialog box can't be easily manipulated via AppleScript (I was trying to add the coordinates on the AppleScript itself, but it wasn't working either).

Even the Set Next Engine Window Position, Center Next Engine Window and Move Last Engine Window to Position actions weren't working, so I guess I will have to deal with the fact that I won't be able to move a dialog window.

This just sucks, because if I have 2 or more dialog window being open (I have these because I need multiple messages to show in the morning) they just overlap.

If you open a Keyboard Maestro Alert, you can run this AppleScript in another Macro to get its position:

tell application "System Events"
	tell application process "Keyboard Maestro Engine"
		get position of window "Keyboard Maestro Alert"
	end tell
end tell

This assumes the Alert has the default name "Keyboard Maestro Alert"

The below works for me:

Yes, with Alerts it does work, but not with a Dialog window created via AppleScript (via display dialog)

An AppleScript dialog isn't a window, and can't be manipulated like one. And it isn't a KM Engine Window, so none of those actions will work.

If you want that sort of control over dialog/alerts you'll have to either use KM's (including Custom HTML Prompts) or try something else -- take a look at swiftDialog.