How to Modify "Custom HTML Prompt" Action from Script?

###How to Modify "Custom HTML Prompt" Action from Script?

Rob, thanks for showing us this can be done.
I'd like to do something similar, but far simpler, I hope.
Sorry, I tried to follow your code, but it was just too complex for me to understand as an example.

All I want to do is change, via AppleScript or JXA, the HTML file that the Custom HTML Prompt uses, and then execute the Action. Can you please help?

I don't know where to find the .kmActions file, to begin with.
Once I find it, can I copy it elsewhere for use with my script?

Here is what I'd like to do:

Create an AppleScript file that can be distributed to anyone who has KM installed, that does the following:

  1. Get the KM Action XML, and set the HTML file it will use
  2. Execute the Custom HTML Prompt action with this changed file/XML

Any reason this is not doable?

BTW, I did notice that in the AppleScript provided by the KM Macro under the trigger "by AppleScript", it showed one form that allows passing of a parameter:

do script "4AD03836-C696-4DE0-A61D-D915E371949A" with parameter "Whatever"

Could this be used to pass the HTML file the Macro Action should use?

Thanks.

Well, I seem to have a bad habit lately of posting a question, and then finding the answer shortly afterward.

###Although I'd still like to know how to modify an Action to be executed by a script, I actually have found, IMO, a simpler solution:

Create a KM Macro that uses a KM variable as the "HTML file" in the Custom HTML Prompt Action. Then I just set that KM variable via AppleScript before calling the Macro.

Here's my test Macro. The production ver will remove the "Set Variable" Action.
I just tested this Macro and it works fine.

BTW, for others following this thread, it is easy to execute a KM Macro from AppleScript, or any script. At the top of the Macro where you set the triggers, just expand/select the "Or by script" trigger, and pick your scripting language:

The other really cool behavior that I have just confirmed, is that when you run the AppleScript it will auto pause waiting for the KM Macro to complete. In this case, it means waiting until the user has submitted the form.

My test script:

tell application "Keyboard Maestro Engine"
	-- do script "4AD03836-C696-4DE0-A61D-D915E371949A"
	do script "[KM]  Custom HTML Prompt Example"
	-- or: do script "4AD03836-C696-4DE0-A61D-D915E371949A" with parameter "Whatever"
end tell

set recAns to (display alert "AFTER KM HTML Form" message "Hopefully don't see this until forum is submitted" as critical ¬
	buttons {"Cancel", "Ignore"}) -- last button is default
set strAns to button returned of recAns

I suppose the best approach is to use the Macro UUID rather than it's name, so that it is not a problem if the name ever changes.

1 Like

Perhaps it is useful to know, in case of dowloaded macro, the Macro UUID is (I think :wink: to be checked by Peter/expert(s)) pseudo-randomly dynamically define locally by each user KM version, meaning unique for each KM license at a given time.
– Alain

Good point, and you're probably right.
If I intend to distribute the KM Macro and AppleScript script, I may have to stick with using the Macro name in the code.

Or even use of the token %ExecutingMacroUUID% in an unique initial setting of a persistent variable to be bullet proof against macro name change and cumulate the best the two solutions :wink:

-- Alain

When Keyboard Maestro imports a macro, it will honour the imported UUID unless it clashes with an existing one (which should only happen if you had already imported the macro previously).

The UUID is just a random number with enough bits in it that a clash is highly improbable (the number is large enough to count the number of grains of sand in the solar system if you filled the whole solar system with sand so its highly likely to be unique).

2 Likes

Thanks for the clarification.
– Alain