How to Load Another Webpage in the Custom HTML Prompt?

Sure, the Translate option for Text Tools I has the code. Let me conjure up a quick version...


Translating the above text:

Ho appena ampliato questa versione per mostrarne un elenco popup con Google come opzione, se non c'è selezione e passare quell'URL al prompt HTML. Ma potresti facilmente creare una macro per ogni app Web che hai con il suo tasto di scelta rapida, ovviamente.

I get a warning to choose an application that can open about:blank that I didn't resolve but here's a preliminary version that flies well enough:

Browser-free Google Translate Macro (v10.0.2)

Browser-free Google Translate.kmmacros (7.5 KB)

1 Like

Thanks @mrpasini

Yes, I get that too. I wonder why.

This is the Macro I made for myself before, which works well but relies on Safari to open Google Translate. That's why I wondered if the code could be modified to work in your Browser Free version:

Google Translate.kmmacros (34.1 KB)
Google Translate

OK, this takes care of that. It's a little app that handles the request to about:blank. The author explains:

Browsers are supposed to display the URL about:blank as a blank page. Unfortunately, some web browsers or similar apps do not know how to do this and will ask macOS to handle this URL. Apps that do this are misbehaving, but until these apps are fixed, you can use AboutWeb to silence these errors.


My solution works but it lost all KM functions of custom Prompt as window.KeyboardMaestro no longer available.

I wonder if Peter can find a way to add window.KeyboardMaestro into custom prompt when using site url. That will be good idea. I don't think security is the worry because there is other library doing that exactly,

  • JxBrowser (Java)
  • Microsoft WebView2 (.net)
  • Microsoft PlayWright (node.js library)
  • Tampermonkey (Browser extension)
    and many more.

Imagine able to access the power of KM in a site.

By the way, you can also inject custom javascript this way by assigning a name to data-kmwindowid in the body tag. [action:Custom HTML Prompt [Keyboard Maestro Wiki]] and making custom prompt asynchronous


Some may suggest using iFrame inside custom prompt but it does not work if you are trying to inject code into iframe due to WKWebview's security check.


This that you provided is a must for those who using site in Custom HTML Prompt window. Even Google Translate site will popup the annoying following message


So the solution you provide did suppress that perfectly.

@mrpasini, thanks so much for sharing. I'll be using this one daily! (Another fine example of the amazing versatility of Keyboard Maestro.)

BTW, with KM 8.0.3+ it seems that a local variable should work, but for some reason with this macro it doesn't. (I confirmed on my system: macOS 12.1; KM 10.0.2.) @peternlewis, what are we missing?

You will just have to preserve the window.KeyboardMaestro object if you wish to replace the window object. I am no JavaScript expert, but I presume you can take a reference to the object and it will remain after the window is deleted.


I have verified that window.KeyboardMaestro object does exists. I find that in order to inject js code into custom prompt that use site (eg, the injected code should be done in the "Execute Javascript in Custom Prompt" action (orange) where window.KeyboardMaestro exists.

window.KeyboardMaestro only exists in "Custom Floating HTML Prompt" action (purple) before the line window.location.href = url is called, and custom functions defined there are no longer recognized once the location is loaded.

So in other words, it is possible to inject js code for Custom Prompt Window that use site.

For example, I have removed unwanted elements in Google Translate site in Custom Prompt Window and variable passing is done through window.KeyboardMaestro's GetVariable function



I'd love to see your version, @macdevign_mac. When I use a local variable for my global kmVar, which I'd prefer, it just doesn't fly:

function KMInit(){window.location.href=window.KeyboardMaestro.GetVariable("localURL");}

Even when I add in:

function KMHandleSchemes(){return "http https";}
function KMInit(){var app = Application.currentApplication()
app.includeStandardAdditions = true
var kmInst = app.systemAttribute("KMINSTANCE");
var kmeApp = Application("Keyboard Maestro Engine") 
window.location.href = kmeApp.getvariable("localURL",  {instance: kmInst});
1 Like


Accessing a KM variable is different in the Custom HTML Prompt action

The following which you used is for Execute Javascript for Automation action, not Custom HTML Prompt action.

action:Execute a JavaScript For Automation [Keyboard Maestro Wiki]

var app = Application.currentApplication()
app.includeStandardAdditions = true
var kmInst = app.systemAttribute("KMINSTANCE");

The custom prompt version is using:

window.KeyboardMaestro.GetVariable( 'KMVariableName' )

action:Custom HTML Prompt [Keyboard Maestro Wiki]

The reason why local_variable failed is because of async Custom HTML Prompt. There is ways to solve this

  • Put a Pause action after Custom HTML Prompt action (2sec)
  • Use document.kmvar.Local_variable
  • Use global variable.

When async is used for Custom HTML prompt, it returns immediately for KM to execute rest of actions, so the macro may finish before the custom prompt get to read the local variable (hence return empty).


Ah ha! Thanks for the async explanation. That was the issue, resolved with document.kmvar.localURL in this version:

Browser-Free Google Translate.kmmacros (8.0 KB)

1 Like

This macro is great—I've turned it into a generic subroutine I call from a number of spots. What I'd like to do is add a floating button stuck to the top right corner of the window that would open the URL in the browser.

I am a Javascript neophyte, so everything I use comes from elsewhere :). I found some code for a floating button:

var button = document.createElement("Button");
button.innerHTML = "My Button"; = "top:10;right:10;position:absolute;z-index: 9999"

Here's how that bit of the macro looks:

The custom HTML prompt is loading asynchronously, and I get no errors at any point…but nothing happens.

All pointers appreciated;

For this to work, the data—kmwindowid must be defined in body tag of custom HTML Prompt statically, not dynamically. So your custom prompt need to include the body tag with data—kmwindowid and cannot be created with document.write.

Anyway do not use document.write as it is destroying the KM reference to the prompt so you can no longer use Execute Javascript in Custom Prompt action

Use the following instead:

1 Like

I was using the code above from @macdevign_mac, which uses document.write to put a live webpage into the custom HTML prompt. I'm far from an expert (hence, the help request), but I don't see how I could do that with your template?


how do you define web page ? Are you defining it as the actual Website (eg or some thing generated by KM variable as shown by your example ?

Thanks for clarifying. As I want to have an actual web page in the window, I think I can solve this another way—I'll just put my own prompt onscreen in another HTML window, and position the two of them together.


It's as shown in my screenshot above: I'm using it as a subroutine, so I pass in the name of the page to open (along with target window dimensions).


Thank you for this, mrpasini, I can use technique this for my Ultimate Web Search macro. Instead of using Brave and create tabs, I can position six windows on my main screen and another six on my secondary screen:

No need to tab around in Brave...

I've determined the dimensions of the first window:

With this, I can position the other 11 windows. Then I'll have to come up with a counter to position these 11 windows correctly.

Question: How can I close all 12 windows with one command?

You can try putting all the custom prompt with the same data-kmwindowid , and when closing them together, just iterate and close them until the action indicates a status to break loop.


The easiest thing to do is click on the red Close button with the Option key down. They'll all close at once.