How to Load Another Webpage in the Custom HTML Prompt?

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:

	<script>
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});
	</script>
1 Like

@mrpasini,

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).

3 Likes

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";
button.style = "top:10;right:10;position:absolute;z-index: 9999"
document.body.appendChild(button);

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;
-rob.

Rob,
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?

-rob.

Rob,
how do you define web page ? Are you defining it as the actual Website (eg http://www.infoworld.com) 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.

Thanks!
-rob.

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).

-rob.

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?

ALYB,
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.

image

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

Can anyone please suggest how can I:

  • Toggle this HTML view's visibility based on the Trigger key? e.g. If I press ⌥ + T, it shows up, if I press it again and the view is visible, it closes/hides. Right now, it opens a new web view every time I trigger the macro.

  • Can I keep the view open in the background, basically in hidden mode, like the ⌘ + H feature of macOS where the app is hidden but still running the background. This would help to load the webview faster the next time I trigger it and take me to the exact place in the view. Reloading it makes some webpages lose their original position