MACRO: Custom HTML Prompt with Dynamic Size and Position

MACRO: Custom HTML Prompt with Dynamic Size and Position

(I was working on this for myself, and thought I might as well post it).

Custom HTML Prompt with Dynamic Size and Position.kmmacros (5.9 KB)

Combining several things I've learned over the past few months, this is a stripped-down example of how to create a Custom HTML Prompt that does the following:

  1. Saves/Restores its position, so it will show in the same position on the screen as the last time you used it.
  • NOTE: Exiting the prompt by pressing the Escape key does NOT save the position.
  1. Specify the Width and Height of the prompt using KM variables.
  • Dynamically sets the HTML "body" style to values that work with the Width and Height.

  • There's only one place to change the values, making it easy to keep everything in sync.

  • Easier for Users to change the width and height, if needed, rather than digging through HTML and css code.

7 Likes

Dan! You must stop this! My hard drive is nearly full from downloading all of your Macros. I've had to hire an assistant just to keep up with this. LOL

Another great macro from Dan the Macro Man! :thumbsup:
I'm adding this to the "Best Macros List".

LOL. Get used to it. This is what I do. I used to do it for a living. I’ve always been the “tools guy”.

Whenever I find myself saying “there’s got to be an easier way - hasn’t anyone done this before me?”, I figure that once I figure out how to do it, I may as well document it for others.

It’s actually kind of an obsession for me - almost OCD-like. You could probably tell. :slight_smile:

Please don’t make me stop! Please!! :wink:

2 Likes

I've been talking to myself over on this post, which takes advantage of KM 8's resizable windows.

Awesome!!

@iNik and @DanThomas, this is so great and saved me tons of time trying to configure this myself :slight_smile:

a question arises though. @iNik, im using your implementation adding the script part to the html. Yet how does one address several html prompts with different sizes?

I tried changing the TestDefaultWindowSize to a unique variable per prompt ie to ws_mailmate, and adjusted that in the code with no success.

here is the current macro:

and the changed html script


<script>
	function KMInit() {
		window.addEventListener("resize", savePosition, true);
		window.addEventListener("unload", savePosition, true); //this doesn't seem to work
	} 
	
	function KMWindow() {
		winBounds = window.KeyboardMaestro.GetVariable("savedTestWindowPosition");
		if(!winBounds) {
			winBounds = window.KeyboardMaestro.GetVariable("ws_mailmate");
			}
		return winBounds;
		} 

	function savePosition() {
		window.KeyboardMaestro.SetVariable('savedTestWindowPosition', [window.screenX, window.screenY + 16, window.innerWidth,window.innerHeight].join(',') );
		}

	function submitWindow(event) {
		savePosition();
		window.KeyboardMaestro.Submit( event );
		}
	function cancelWindow(event) {
		savePosition();
		window.KeyboardMaestro.Cancel( event );
		}
</script>

any help would be greatly appreciated!

thx again for your cool macros!

Z

Ok..answering myself, i did get it to work, but it seems to me in a very hacky way..i changed both variables on the html script to unique variables, and this seem to work now:

<script>
	function KMInit() {
		window.addEventListener("resize", savePosition, true);
		window.addEventListener("unload", savePosition, true); //this doesn't seem to work
	} 
	
	function KMWindow() {
		winBounds = window.KeyboardMaestro.GetVariable("ws_hyper2");
		if(!winBounds) {
			winBounds = window.KeyboardMaestro.GetVariable("ws_hyper");
			}
		return winBounds;
		} 

	function savePosition() {
		window.KeyboardMaestro.SetVariable('ws_hyper2', [window.screenX, window.screenY + 16, window.innerWidth,window.innerHeight].join(',') );
		}

	function submitWindow(event) {
		savePosition();
		window.KeyboardMaestro.Submit( event );
		}
	function cancelWindow(event) {
		savePosition();
		window.KeyboardMaestro.Cancel( event );
		}
</script>

also disabled the delete part of the script

i assume this is an idiotic way to go isn't it? :wink:

thx again

Z

Here is an untested idea: Try using either Local or Instance Variable . I would try Local first.

If this works as I expect (untested), then each Macro that uses the HTML Prompt will have a separate copy of the Local variable, and there should be no conflicts.

Great explanation thank u so much

Thx @JMichaelTX for the interesting idea!

for some reason (and the reason probably being me :slight_smile: ) i cant get it to work.

the old method using a normal variable seemed to work

yet when using local or instance variables it dosent seem to be working and i don't get the variable in the variable list

(see the empty in the macro window)

any clue what im botching here?

thx

Z

Can you be more specific by what you mean by "doesn't seem to be working"?
Did you change the variable name in the HTML JavaScript for the Prompt?

Hi

Sorry yeah i did here is the full section in the html


<script>
	function KMInit() {
		window.addEventListener("resize", savePosition, true);
		window.addEventListener("unload", savePosition, true); //this doesn't seem to work
	} 
	
	function KMWindow() {
		winBounds = window.KeyboardMaestro.GetVariable("Local_htmlsizedsave");
		if(!winBounds) {
			winBounds = window.KeyboardMaestro.GetVariable("Local_htmlsize");
			}
		return winBounds;
		} 

	function savePosition() {
		window.KeyboardMaestro.SetVariable('Local_htmlsizedsave', [window.screenX, window.screenY + 16, window.innerWidth,window.innerHeight].join(',') );
		}

	function submitWindow(event) {
		savePosition();
		window.KeyboardMaestro.Submit( event );
		}
	function cancelWindow(event) {
		savePosition();
		window.KeyboardMaestro.Cancel( event );
		}
</script>

also this is the full macro

and it dosent seem to save the position at all

thx again @JMichaelTX

Z

I think you may be using the wrong variable:

function savePosition() {
    window.KeyboardMaestro.SetVariable('Local_htmlsizedsave', [window.screenX, window.screenY + 16, window.innerWidth,window.innerHeight].join(',') );
    }

The script saves to "Local_htmlsizedsave", but you are setting "Local_htmlsize".

Hi @DanThomas. With Keyboard Maestro 10.0.1, the Custom HTML Prompt (CDP) position is not saved*. Any ideas? Thanks in advance!

*By "saved" I'm assuming:

  • Trigger the macro to display the CDP.
  • Drag the CDP to a new location.
  • Click OK to close the CDP and end the macro.
  • Trigger the macro to display the CDP. It should appear at the previously-dragged position.

There's another thread somewhere that talks about this. There's even a solution. I don't remember what thread, but it wasn't too long ago.

2 Likes

Hi _jims,
I can't get it to work either … nor find the "thread somewhere that talks about this" as Dan mentioned.
Did you solve it?

1 Like

Hi @LarryLayouter. No I didn't solve it, but @DanThomas did.

I think this is the thread that he was referring to above: Custom HTML Prompt WindowPositionController - Macro Library - Keyboard Maestro Discourse

1 Like

Thanks to _jims for help
and
thanks to Dan for this great macro!