Demo Using KM Custom HTML Prompt with AppleScript
First, let me give @peternlewis a huge thank you for providing the Custom HTML Prompt action as a new feature in KM7. This is really huge, and opens up all kinds of opportunities/solutions we didn't have before.
One opportunity I am very interested in is using this KM HTML form with scripts like AppleScript and JXA. Neither of these have support for HTML dialogs natively, and the dialogs available are very simple.
So, I have learned that I can create an AppleScript that executes a KM Macro with a Custom HTML Prompt action, and waits for the action to complete before continuing the script. So we start the process with AppleScript, and stay in AppleScript entirely, except for when we want to show the HTML form via KM. Again, this is huge, IMO.
Turns out it is not that hard to build such a script, once you know how. LOL I know, that sounds stupid, but what I mean is that the resulting script and the HTML file are pretty simple once you understand how to put the parts together.
So, I'm sharing today a complete example of:
- AppleScript
- HTML File
- KM Macro
If anyone finds any bugs or issues, or have suggestions for improvements, please post here.
The KM Macro is extremely simple since it is just a placeholder that all scripts can execute. Each script just needs to set a KM variable with the POSIX path to the HTML file before executing the macro.
Download one Zip File with all 3 files:
[KM] DEMO KM HTML Form Set by AppleScript.zip (17.6 KB)
I have now added this JXA script file, which I really like better.
To whet your appetite, let me start with a screenshot of the HTML form itself:
(BTW, I reworked the sample KM file, using tables for layout instead of CSS, mainly because I don't understand how to use CSS for layout/positioning)
The KM Macro is extremely simple. The key is using a KM variable for the path to the HTML file:
KNOWN ISSUES:
-
There is an apparent BUG in KM, causing it to return a value of "Password", instead of the actual value, for any KM variable that has been used in a HTML password form field.
-
You can't use KM variables with an underscore ("
_
") in the name, since KM replaces "_
" with a space when the HTML Form Field Name is returned to KM.
AppleScript Code
I saved the meat for the last, because it is the longest:
(*
===============================================================================
DEMO Use of KM HTML Form Set by AppleScript
===============================================================================
VER: 2.0 LAST UPDATE: 2015-12-27
PURPOSE:
β’ Show how to use the KM Action "Custom HTML Prompt" with AppleScript
AUTHOR: JMichaelTX
Find any bugs/issues or have suggestions for improvement?
Contact me via PM or at blog.jmichaeltx.com/contact/
REQUIRED:
1. Mac OS X Yosemite 10.10.5+
2. Mac Applications
β’ Keyboard Maestro Engine
3. EXTERNAL OSAX Additions/LIBRARIES/FUNCTIONS
β’ None
4. INTERNAL FUNCTIONS:
β’ setKMVar(pKMVarName, pKMVarValue)
β’ bolToNum(pbolVar)
β’ numToBol(pnumVar)
REF: The following were used in some way in the writing of this script.
1. [Custom HTML Prompt action]
(https://wiki.keyboardmaestro.com/action/Custom_HTML_Prompt)
2. [Trigger Macro via AppleScript]
(https://wiki.keyboardmaestro.com/trigger/Script)
KNOWN ISSUES:
1. There is an apparent BUG in KM, causing it to return a value of "Password", instead of the actual value, for any
KM variable that has been used in a HTML password form field.
2. You can't use KM variables with an underscore ("_") in the name, since KM replaces "_" with " " when the HTML Form Field Name is returned to KM.
===============================================================================
*)
--- SET VALUES TO AS VARS THAT WILL BE USED ON FORM ---
set strASName to "Mr. No Name"
set strASEMail to "johnny@AppleScript.com"
set strASPW to "BadPassWord"
set bolASCheckbox to false
set strASMessage to "Now is the time for all good men to come to the aid of their country"
set strASPopup to "TX"
set strASSelection to "TX"
set strASRadio to "B"
--- SET POSIX FILE PATH FOR HTML FILE ---
set strHTMLFile to "/Users/Shared/Dropbox/SW/DEV/KM/KM7/Actions/Custom HTML Prompt/KM-HTML-Form-Set-by-Applescript.html"
-----------------------------------------------
--- SET KM VARS (Create if necessary) ---
-----------------------------------------------
--setKMVar("HTML Prompt File", strHTMLFile)
setKMVar("AS_HTML_Form_File", strHTMLFile)
--- MUST SET RESULT BUTTON, IF USER PRESSES ESC BUTTON IS NOT SET ---
setKMVar("HTML Result Button", "TBD")
--- SET INITIAL VALUE FOR FORM FIELD NAMES ---
setKMVar("ASName", strASName)
setKMVar("ASEmail", strASEMail)
setKMVar("ASPW", strASPW)
setKMVar("ASCheckbox", bolToNum(bolASCheckbox)) -- KM uses 1/0 for checkbox checked/unchecked
setKMVar("ASMessage", strASMessage)
setKMVar("ASPopup", strASPopup)
setKMVar("ASSelection", strASSelection)
setKMVar("ASRadio", strASRadio)
log strASPW
--------------------------------------------------------------
tell application "Keyboard Maestro Engine"
----------------------------------------------------------
--- EXECUTE MACRO TO DISPLAY HTML FORM ---
do script "[SCRIPT] HTML Form for Scripts"
## AppleScript will wait here until User submits form ##
--- OK, FORM HAS BEEN SUBMITTED, GET DATA ---
--- GET BUTTON CLICKED ---
-- If form was closed by user pressing ESC, no button was clicked
-- and the KM var "HTML Result Button" will NOT be set.
-- But we will treat it as if the user cancelled.
-- That's why before form was submitted, the "HTML Result Button" variable
-- was set to "TBD"
set strKMButton to value of variable "HTML Result Button"
log "Result Button: " & strKMButton
if (strKMButton = "Save") then -- GET KM vars, & process
set strASName to value of variable "ASName"
set strASEMail to value of variable "ASEmail"
set strASPW to value of variable "ASPW" ## BUG - KM is returning "Password" instead of actual value ##
set bolASCheckbox to my numToBol(value of variable "ASCheckbox")
set strASMessage to value of variable "ASMessage"
set strASPopup to value of variable "ASPopup"
set strASSelection to value of variable "ASSelection"
set strASRadio to value of variable "ASRadio"
### INSERT YOUR PROCESSING OF FORM DATA ###
set strAlertTitle to "SUCCESS"
set strAlertMsg to "Variables Updated via KM Form"
else -- User Canceled, so terminate script, or handle cancellation
log "User Canceled"
set strAlertTitle to "*** USER CANCELLED ***"
set strAlertMsg to "What Now?"
end if -- (strKMButton = "Save")
----------------------------------------------------------
end tell -- application "Keyboard Maestro Engine"
--------------------------------------------------------------
display alert strAlertTitle message strAlertMsg as critical Β¬
buttons {"Cancel", "Ignore"} -- last button is default
--- LOG VARS AFTER FORM WAS SUBMITTED OR CANCELLED ---
log strASName
log strASEMail
log strASPW
log bolASCheckbox
log strASRadio
log strASPopup
log strASSelection
log strASMessage
--============================= END OF MAIN SCRIPT ====================
### ββββββββββββββββββββββββββββββββββββββββββββββ
# setKMVar() Sets KM Variable, Makes if needed
#
# Ver 2.0 2015-12-27
### ββββββββββββββββββββββββββββββββββββββββββββββ
on setKMVar(pKMVarName, pKMVarValue)
--log ("setKMVar: " & pKMVarName & ": " & pKMVarValue)
tell application "Keyboard Maestro Engine"
try -- to set variable, will error if it doesn't exist
set value of variable pKMVarName to pKMVarValue
on error -- Make & Set Variable
make new variable with properties {name:pKMVarName, value:pKMVarValue}
end try
end tell -- KM
end setKMVar
--ββββββββββββββββββββββββββββββββββββββββββββββ
### ββββββββββββββββββββββββββββββββββββββββββββββ
# bolToNum() Convert Boolean to Number (1/0)
#
# Ver 1.0 2015-12-27
### ββββββββββββββββββββββββββββββββββββββββββββββ
on bolToNum(pbolVar)
if pbolVar then
set nVar to 1
else
set nVar to 0
end if
return nVar
end bolToNum
--ββββββββββββββββββββββββββββββββββββββββββββββ
### ββββββββββββββββββββββββββββββββββββββββββββββ
# numToBol() Convert Number to Boolean
#
# Ver 1.0 2015-12-27
### ββββββββββββββββββββββββββββββββββββββββββββββ
on numToBol(pnumVar)
if (pnumVar as integer) = 0 then
set bolVar to false
else
set bolVar to true
end if
return bolVar
end numToBol
--ββββββββββββββββββββββββββββββββββββββββββββββ
Results of Script
(*BadPassWord*)
(*Result Button: Save*)
(*Mr. Big Boss*)
(*bossy@bigco.com*)
(*Password*)
(*true*)
(*A*)
(*CN*)
(*CO*)
(*Now is the time for all good men to come to the aid of their country*)