Command-W to close custom html prompt?

I feel like I have the answer to this somewhere and it might involve javascript, but is there a way close a custom html prompt with Command-W? I also thought about running a second macro asynchronously that waits for a keystroke and then closes a window by its ID, but now my head hurts. Thoughts?

PS: I usually just use the Escape key, but sometimes my fingers want to Command-W like it's a Safari window.

You have to add an event listener which responds to the W key and calls a window close function. I'm away without a mac at the moment so I can't demonstrate, but...

Paste your existing code into Chat GPT and ask it to add the function for you. Something as simple as that shouldn't be too much trouble.

1 Like
	document.addEventListener("keydown", function(event) {
		if (event.key === "w" && event.metaKey) {

Thanks, @noisneil and ChatGPT

I spent some time Googling and reviewing my other macros and notes before posting and got in the vicinity of this solution, but ChatGPT give me this exacty, minus the opening script tag.

1 Like

I've recently been developing some macros that have similarities with what you are asking for, so here is my take based on the approach I've been exploring:

Close HTML prompt with cmd+w CALLING.kmmacros (1.9 KB)
Close HTML prompt with cmd+w SUBMACRO.kmmacros (4.8 KB)

Macro Images


Cool, thanks for sharing these @Alexander. This was the other approach I was trying to figure out. I got as far as the calling macro with the parameter %ExecutingInstance% but I wasn't sure if I needed to Pause Until, While, If, etc. Can you explain the While block? I understand the Break that fires the Escape key once the keystroke is made, but is it repeatedly checking with the If statement as long as the calling macro is running?

I hope my question makes sense.

EDIT: Okay I think I got it. The While action is "true" so it essentially continues until the nested If statement is true, which breaks out of the While loop. So this technique could be applied even by using "While 1=1" to make the While loop continue until the If statement is true.

Very helpful. Thank you!

This is exactly it. The While block is in this configuration pretty much a Pause Until that also accepts an OR condition in addition to the AND condition in the embedded If Then.

The %ExecutingInstance% is passed through to the Submacro as the TriggerValue and in the While block this instance is searched for in the commaseparated list of all current instances given by the %ExecutingInstances%-token. The While block breaks from its loop IF the calling macro is no longer executing OR the Cmd AND W keys are pressed. Most of this is to prevent the Submacro from being kept running if the calling macro was ended in another way than having the prompt window closed by the Submacro.

Edit: Fixed an error where I wrote Escape-key instead of W-key

1 Like

I tested this when you posted the macro so I know it's true :slight_smile: but why is that? Is that because the semaphore lock is released once the calling macro finishes?

The Semaphore lock is there only as a prevention if the Submacro for some reason is Called several times simultaneously. It is the Break From Loop-action alone that makes the loop break. The last If Then rechecks if the Calling macro is still running: IF it is, it means the While-loop was broken by pressing the cmd- and W-key, so the Escape-key will be simulated to close the Prompt window; IF it is not still running, (it means the While-loop was broken by the Calling macro not executing any more) and the Submacro will simply run to its end and finish.

I am not good at explaining these things, as I am pretty new to programming logic, but hoping it makes sense. IF not, I’ll try to explain better, haha!

1 Like

That's great! Thanks for taking the time to help me understand. You and @noisneil gave me two new tools in my toolbox :+1: