I can now add to this a JXA script file, which I really like better.
####DOWNLOADS
Original 3 files with AppleScript
[KM] DEMO KM HTML Form Set by AppleScript.zip
JXA Script File, which uses the same HTML file and Macro as above:
[KM] DEMO Custom HTML Form using JXA.scpt.zip (3.7 KB)
Both script files have had limited testing, so please report any bugs, issues, or concerns that you may find with them. And if you have any suggestions for improvements, or a better or alternate script, then please post that as well.
None of these files are intended for production use. They are all just examples that will hopefully help you get started using the great KM Custom HTML Prompt action with your scripts.
Remember that the primary/lead tool in this process is the script. You start by running the script, not the macro. The script then calls the KM macro at the appropriate point.
I look forward to seeing your feedback, and hope that we call learn together.
###Here's the JXA script:
Note: the code alignment here looks weird. I think it's due to the number of spaces for a TAB char that the forum uses, vs what I use, which is 2 spaces/TAB.
/*
===============================================================================
DEMO Use of KM HTML Form Set by JXA
===============================================================================
VER: 2.0 LAST UPDATE: 2015-12-28
PURPOSE:
• Show how to use the KM Action "Custom HTML Prompt" with JXA
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)
• getKMVar(pstrName)
• strToBool (pstrBool)
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.
===============================================================================
*/
var app = Application.currentApplication()
app.includeStandardAdditions = true
var strAlertTitle
var strAlertMsg
var oKMV = {} // Object that holds KM var name and value
//--- KM VARS USED ON HTML FORM ---
oKMV["ASName"] = "Johnny"
oKMV["ASEmail"] = "john@gmail.com"
oKMV["ASPW"] = "SetByJXA"
oKMV["ASCheckbox"] = "0" // will be set to Boolean after form processing.
oKMV["ASPopup"] = "TX"
oKMV["ASMessage"] = "Now is the time for all good men to come to the aid of their country"
oKMV["ASSelection"] = "TX"
oKMV["ASRadio"] = "B"
//--- SET KM VAR FOR HTML FILE ---
setKMVar("AS_HTML_Form_File",
"/Users/Shared/Dropbox/SW/DEV/KM/KM7/Actions/Custom HTML Prompt/KM-HTML-Form-Set-by-Applescript.html")
//--- MUST SET RESULT BUTTON, IF USER PRESSES ESC BUTTON IS NOT SET ---
setKMVar("HTML Result Button","TBD")
console.log(oKMV.ASName) // use this form for other processing in JXA
//--- SET KM VARS (Create if necessary) ---
for(var name in oKMV) {
console.log(name + ": " + oKMV[name])
setKMVar(name, oKMV[name]) // SET KM var
}
//--- CALL KM MACRO TO SHOW THE HTML FORM ---
var appKM = Application('Keyboard Maestro Engine')
appKM.doScript("[SCRIPT] HTML Form for Scripts");
//### SCRIPT WILL WAIT HERE UNTIL USER SUBMITS FORM ###
var strButton = getKMVar("HTML Result Button")
if (strButton === "Save") { //--- GET HTML FORM FIELDS ---
console.log ("--- RESULTS FROM FORM ---")
for(var name in oKMV) {
oKMV[name] = getKMVar(name) // Get KM var
console.log(name + ": " + oKMV[name])
} // END FOR
// ### INSERT YOUR PROCESSING OF FORM DATA ###
strAlertTitle = "SUCCESS! User Saved Form"
strAlertMsg = "Now Process Form Data"
} else {
console.log("*** USER CANCELLED FORM ***")
strAlertTitle = "User Cancelled Form"
strAlertMsg = "What next?"
} // END IF/else "Save" button
//--- CONVERT STRING "0" or "1" to Boolean ---
oKMV.ASCheckbox = strToBool(oKMV.ASCheckbox)
console.log(oKMV.ASCheckbox)
app.beep()
var oAns = app.displayAlert(
strAlertTitle, // main alert text
{
message: strAlertMsg,
as: "critical",
buttons: ['Cancel', 'Ignore', 'OK'],
defaultButton: 3,
cancelButton: 1
})
if (oAns.buttonReturned = "Cancel") {
// Quit the script?
}
//=============== END OF MAIN SCRIPT =================================
//=====================================================================
function boolToStr (pBoolVar) {
//=====================================================================
if (pBoolVar) {
strVar = "1"
} else {
strVar = "0"
}
return strVar
} // END function boolToStr
//–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
//=====================================================================
function strToBool (pstrBool) {
//=====================================================================
if (pstrBool === "0") {
bolVar = false
} else {
bolVar = true
}
return bolVar
} // END function strToBool
//–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
//=====================================================================
function setKMVar(pstrName, pstrValue) {
//=====================================================================
var app = Application.currentApplication()
app.includeStandardAdditions = true
var appKM = Application("Keyboard Maestro Engine")
var oVars = appKM.variables
try {
oVars[pstrName].name();
} catch (e) {
appKM.variables.push(appKM.Variable({'name': pstrName }));
app.displayNotification(
pstrName,
{
withTitle: "Set KM Variable",
subtitle: "Variable was Created"
//soundName: "Basso"
});
} // END try/catch
oVars[pstrName].value = pstrValue
return
} // END function setKMVar
//–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
//=====================================================================
function getKMVar(pstrName) {
//=====================================================================
var app = Application.currentApplication()
app.includeStandardAdditions = true
var appKM = Application("Keyboard Maestro Engine")
var oVars = appKM.variables
try {
var strValue = oVars[pstrName].value();
} catch (e) {
strValue = undefined
app.beep()
var oAns = app.displayAlert('KM Variable does NOT exist', {
message: 'Var Name: ' + pstrName,
as: 'critical'
})
} // END try/catch
return strValue
} // END function getKMVar
//–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
//======================= END OF FILE =====================================