Because of a bug in the new WebKit, there's a little trick you need to do. Here's an example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Custom HTML Prompt Window Position Bug</title>
</head>
<script type="text/javascript">
function _debugLog(msg) {
var ele = document.getElementById("debugText");
ele.style.display = "inline";
ele.textContent += msg + "\n";
}
function KMWindow() {
_debugLog(`window.screenX: ${window.screenX}`);
_debugLog(`window.screenY: ${window.screenY}`);
_debugLog(`window.screen.availWidth: ${window.screen.availWidth}`);
_debugLog(`window.screen.availHeight: ${window.screen.availHeight}`);
_debugLog(`window.screen.availTop: ${window.screen.availTop}`);
_debugLog(`window.screen.availLeft: ${window.screen.availLeft}`);
// You can include the window width and height
var savedPosition = window.KeyboardMaestro.GetVariable("TestSaveWindowPosition");
if (!savedPosition)
savedPosition = "800, 200";
return `${savedPosition}, 400, 400`;
}
function saveWindowPosition() {
// "parseint" isn't really needed here, but since I use it for the Y
// position, I thought I'd be consistent.'
var x = parseInt(window.KeyboardMaestro.Calculate("WINDOW(Left)"));
// Note that we add 126 to this value, to take into account the height
// of the title bar.
var y = parseInt(window.KeyboardMaestro.Calculate("WINDOW(Top)")) + 16;
window.KeyboardMaestro.SetVariable("TestSaveWindowPosition", `${x}, ${y}`);
}
function submitWindow(result) {
saveWindowPosition();
window.KeyboardMaestro.Submit(result);
}
function cancelWindow(result) {
saveWindowPosition();
window.KeyboardMaestro.Cancel(result);
}
</script>
<body>
<div id="title">
Custom HTML Prompt Save Position Example
</div>
<form id="myform" action="#" method="post">
<div class="center submit">
<button name="Cancel" type="button" onclick="cancelWindow('Cancel')">Cancel</button>
<button name="OK" type="button" onclick="submitWindow('OK')">OK</button>
</div>
</form>
<textarea id="debugText" rows="10" style="width: 100%; display: none"></textarea>
</body>
</html>
I'm heading out for awhile, so if you have questions, I won't be able to respond right away.
Your solution was spot on as before. But I have some questions.
What is the WebKit bug? Is it something apple supplies or the implation into KM?
Also I am having one problem in getting the code to work in my html. Since this is the first time I have ever tried this I am sure it is some stupid mistake on my part.
Thanks for your help
Roger
function getPosition() {
var savedPosition = window.KeyboardMaestro.GetVariable("grwPosition");
return savedPosition + "420, 400";
}
</script>
<body data-KMWindow=20,40,420,400> <<<this works
<body data-KMWindow=getPosition> <<<this will not work
As I understand it, this is a Webkit bug. It's not related to KM.
Also I am having one problem in getting the code to work in my html.
Don't put the position in the "data-kmwindow" attribute. That only works for hard-coded values. Instead, return it from a function called "KMWindow()", as my example shows.
Since this is the first time I have ever tried this I am sure it is some stupid mistake on my part.
The fact you're even trying this makes you ahead of the curve. There's a lot to be learned, depending on what you're trying to do, so don't feel stupid. It took me a long time to figure all this stuff out.
Thanks for the help. I knew which 2 lines of code was the problem but could not figure out why. So I took another route.
I was trying to add your saving position into another macro by (JMichaelTX JMichaelTX) that I found on Keyboard Maestro. So I changed and added the look of the other macro to your sample and it works like I wanted. I still plan to look at both and try to figure out what is the difference.