Resizing Window in Custom HTML Prompt

When using Custom HTML Prompt you can set the window size and position in two ways according to the docs:

  1. Set data-kmwindow attribute on the body element
  2. Use JavaScript by calling window.KeyboardMaestro.ResizeWindow()

If you want to programmatically set the window size/position, you have to use JavaScript.

The problem is that I have not been able to get, window.KeyboardMaestro.ResizeWindow() to set the size or position. If you use a button to do it, it works, but just calling the function from your script will do nothing.

I think this is a bug.
See related post: In KM10, is window.KeyboardMaestro.ResizeWindow() broken in Custom HTML Prompts? by Dan Thomas

My workaround is simple. Setting the size/position with the data-kmwindow attribute on the body element works. So, I just set that attribute with JS:

// Desired location and/or size as string
let windowTarget = "100,100,500,400"

let body = document.querySelector('body')
body.setAttribute('data-kmwindow', windowTarget)

The good things are that this will happen when the window is opened, and can be set programmatically using variables from KM.

Can you post the Javascript-based resize macro that isn't working?

I'm a newb with JS, so may be completely misunderstanding the problem, but doesn't the following show this does work when called as a function?

Resize Test.kmmacros (2.1 KB)

Summary

Your method is nice. Here's what I think should work, but doesn't:
Resize Problem Example.kmmacros (1.7 KB)

Image of Macro

So what I thought should work is a script at the end the body with just one line, window.KeyboardMaestro.ResizeWindow( "900, 400" ). That one line of JavaScript would seem to just do the thing, but on its own it does nothing. It has to be called by something, which is what you did; an onload event that you put on the body which calls it.

This could very well be the intended behavior, but I found it confusing, so I thought I'd add my solution in case others hit the same roadblock.

I don't know enough about JavaScript or how the KM HTML Prompt works to answer -- hopefully someone else will chip in. But when in doubt, do things explicitly!

You don't need to define a separate function, that was just to show things working. In your case you can simply include the call as part of the body tag:

<body onload='window.KeyboardMaestro.ResizeWindow("900,400")'>