Custom HTML Action - Doesn't Accept Variables?

I'm finally starting to get the hang of the "Custom HTML Prompt" action. But I've hit a big hurdle. It doesn't accept KM variables in the data field: (you can see the absence of a T in the upper right corner of the image below)

So I thought, that's no problem, because there's an option in the action that lets me change from a text field to a data file:

But when I use a text file the resulting HTML box always comes out looking like the next image, failing to interpret the file as an HTML file, but just as a text file: (it doesn't matter what the HTML code is, the following is a simple example)

image

So why am I unable to pass text from a variable or a file to the Custom HTML Prompt action? I can easily pass plain/constant text, but when I want to customize the text by creating it in a file or in a variable, the action won't accept my data.

That example works perfectly well when you pass it as straight text, like this. So why doesn't this HTML code work when it's in a file? What's the purpose of the file option if it doesn't read the file as HTML code, but as plain text?

image

image

Basically I'm using KM code to generate the HTML code, but with no way to pass the computed code to the Custom HTML action, I can't get the HTML window that I want.

Probably I'm making a silly mistake. Can anyone tell me what I'm doing wrong? My suggestion is that the Custom HTML Prompt action should permit variables, and also should permit loading the HTML data from a file. But I'll be happy with either solution.

Hi @Sleepy,

The HTML prompt does accept variables. See the wiki page.

What you are doing, however, seems to be related to the HTML prompt window's initial size. This field does not seem to accept variables/tokens. There is a way to set the initial size of the HTML prompt window. You can even save the HTML window size and use it to set the initial size when it opens next time. See the example macro here:

1 Like

Hey @Sleepy,

As @martin mentions – yes you can get and set Keyboard Maestro variables from the Keyboard Maestro Custom HTML Prompt action.

The action pretty clearly specifies HTML Text:

Text fields are antithetical to calculation fields, so it's no surprise that neither bare variable names nor functions are available for expansion.

The fact that the actions tool menu and contextual menu doesn't have a section on token expansion is a pretty good clue to the fact that text tokens don't work either.

** Text tokens are not available in any of the scripting actions.

As @martin suggests – read the Wiki Page on the Custom HTML Prompt action carefully.

Your second example above works for me from both HTML text and HTML text file.

Here are three simple examples including your own:

Custom HTML Prompt Test v1.00 ⇢ @ccs.zip (10.1 KB)
image

-Chris

1 Like

I spent about 8 hours reading the [Wiki Page on the Custom HTML Prompt action] and that's why I came here, because I got as far as I could from reading that page.

But after reading your post, I found a big clue. The ability to read from a file apparently only works if the file extension was ".html". In my example above you can see that the file extension I used was ".txt". I had no idea that the "Custom HTML Prompt" action required specific file extensions. There seems to be no mention in the wiki that certain file extensions are required. Maybe .htm and .html? I'm not going to try to find a complete list of all legal file extensions.

I've got great plans for this Custom HTML action, and now that I can read files, I can get going. I plan to post my results on this website. I'm doing something really novel with KM and certain hardware.

You made a great point that none of the scripting actions accept tokens. Do we know the reason for that? I can't see why they are restricted, but now that I can read files, that's adequate.

1 Like

I did read that page before posting, but it wasn't the issue I was struggling with, so I didn't really need to understand it. CC gave me the clue that I needed to fix my problem.

To be fair with KM, the web browsers will not parse the HTML code either if the file extension is .txt (see examples below by Firefox and Safari). So I assume it is reasonable to assume that users should use the html file extension for the HTML prompt action.
image

Yes, that's true. But I'm learning from the KM documentation, and the KM documentation didn't mention that requirement. Neither you nor CC mentioned this requirement to me even after I showed you that I was using the .txt extension. So clearly it's not obvious. I spent over 8 hours trying to debug this before I came here for help. I freely admit that I'm dumb. I guess this is just one of those things you gotta know before you can use.

Sorry about that. I skimmed over your post and totally missed the extension. I was caught up by your title "Custom HTML Action - Doesn’t Accept Variables" and was trying to identify why it did not "accept variables". :joy:

I am truly grateful for all the help I get on this forum, and I thank you for everything even if some of the help occasionally misses. I am especially grateful and also in awe when Peter chimes in. None of us deserve his assistance. He should be prioritizing the next set of KM features.

I'm still hoping for some sort of HomeKit action built into KM. Maybe Monterey has some API for it now that Peter can latch into. However since HomeKit control is built into Monterey's Shortcuts, I think I can use KM to control my home that way.

1 Like

Ah, I see. My eyes glossed over the .txt, and only saw the html part of the file name:

~/html.txt

The action and Wiki are pretty clear about using HTML text or an HTML file, and a .txt file is never registered as a .html file no matter the content.

Even so – I've amended the wiki page:


  • The HTML code can be in a .html file, or as text (html code) in the Action

-Chris

It never occurred to me that I could register a file type with an app. I used to know how to do this in Windows but I had to google how to do that in macOS. Here's how: you "Get Info" on a file with the file type you are interested in, and look down the window for the "Open With" option. I presume I could have txt files open with Safari as another solution to this problem, but I won't bother testing that out.

image

Nyet.

Text files (.txt) will open as text and will NOT render.

@martin mentions and demonstrates this in post #6 above.

-Chris

Yes, he mentioned it and demonstrated it prior to our discussion of changing the file associations. That is correct.

In order to be confident, I just tested out what happens when you associate .txt with Safari, (which is something Martin didn't say he tested) and Safari still didn't render it as HTML.

The challenges aren't over yet. Apparently there are some more issues. Right now the Custom HTML action will open some HTML files in a browser, but some HTML files won't open, and won't even trigger an error, even though it is an HTML file that renders perfectly in Safari. So now I have to figure out what is the content in the HTML file that the Custom HTML action is rejecting. Shouldn't it at least produce an error if it can't open the HTML file?

I'm always facing these kinds of problems. It's life.

Not only does the Custom HTML macro abort execution for most of my HTML files, but even if you disable the flag "Failure Aborts Macro" for the action, it still aborts the macro. The KM log file simply reports "2021-08-11 19:25:45 Custom Prompt failed to get HTML file" but doesn't say why the action failed.

FIXED: I found out why Custom HTML Action was failing. My HTML code used the KM section called "<body data-kmwindow=..." and this section causes the action to crash without warning if you don't have a second monitor but your KM prefix code refers to the second monitor. So that's the solution, but it's still a shame that the malformed HTML code terminates the macro without explaining why in the log and without letting you continue when the flag for termination isn't set to terminate. Please tell me why this isn't a bug in the KM engine.