I notice that although the Wiki entry might suggest otherwise,
we do seem to be able to read Instance Variables from scripts, as long as we provide an instance argument to the .getvariable method.
Perhaps worth a pointer to this route in the Wiki ? Instance variables do seem the best default where they're feasible.
Sample JS β reading an instance variable from inside an Execute JavaScript action.
(() => {
'use strict';
// KEYBOARD MAESTRO INSTANCE AND VARIABLE VALUE ---------------
// kmInstanceMay :: -> Maybe Keyboard Maestro Instance
const kmInstanceMay = () => {
const oInstance = ObjC.unwrap(
$.NSProcessInfo.processInfo.environment
.objectForKey('KMINSTANCE')
);
return Boolean(oInstance) ? (
just(oInstance)
) : nothing('This code is not running in a KM Macro');
};
// kmVariableValue :: Maybe KM Instance -> String -> String
const kmVariableValue = (mbInstance, k) => {
const kme = Application("Keyboard Maestro Engine");
return mbInstance.nothing ? (
kme.getvariable(k)
) : kme.getvariable(k, {
instance: mbInstance.just
});
};
// GENERIC FUNCTIONS --------------------------------------------
// just :: a -> Just a
const just = x => ({
nothing: false,
just: x
});
// nothing :: () -> Nothing
const nothing = (optionalMsg) => ({
nothing: true,
msg: optionalMsg
});
// TEST ---------------------------------------------------------
// Returns a value only if the Execute Script action is running
// in a macro which defines the named Instance Variable.
// Testing here for an instance variable called InstanceAbbrevn
return kmVariableValue(
kmInstanceMay(),
'InstanceAbbrevn'
);
})();
Or, AppleScript example:
Read Instance Variable from Script action.kmmacros (19.6 KB)
use framework "Foundation"
use scripting additions
-- kmInstanceMay :: -> Maybe Keyboard Maestro Instance
on kmInstanceMay()
set ca to current application
set oInstance to ca's NSProcessInfo's processInfo's Β¬
environment's objectForKey:"KMINSTANCE"
if oInstance is not missing value then
just(oInstance as string)
else
nothing("This AppleScript is not running in a KM Macro")
end if
end kmInstanceMay
-- kmVariableValue :: Maybe KM Instance -> String -> String
on kmVariableValue(mbInstance, k)
tell application "Keyboard Maestro Engine"
if nothing of mbInstance then
getvariable k
else
getvariable k instance (just of mbInstance)
end if
end tell
end kmVariableValue
-- just :: a -> Just a
on just(x)
{nothing:false, just:x}
end just
-- nothing :: () -> Nothing
on nothing(msg)
{nothing:true, msg:msg}
end nothing
-- TEST ---------------------------------------------------------
-- Returns a value only if the Execute Script action is running
-- in a macro which defines the named Instance Variable.
-- Testing here for an instance variable called "InstanceTest"
on run
kmVariableValue(kmInstanceMay(), "InstanceTest")
end run
Yes, KM 8.0.3 provided us with script access on Oct 3:
As of 8.0.3, local and instance variables should work in Prompt For User Input and Custom HTML Input windows.
Also, you can now access them from scripts by using the KMINSTANCE environment variable as a key to which set of variables.
set inst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
set v to getvariable "LocalVar" instance inst
setvariable "LocalOut" instance inst to "FromAS"
end tell
v
And I posted the JXA access here:
I finally worked it out:
JXA Script to Get/Set Local & Instance Variables
var app = Application.currentApplication()
app.includeStandardAdditions = true
var kmInst = app.systemAttribute("KMINSTANCE");
var kmeApp = Application("Keyboard Maestro Engine")
var myLocalVar = kmeApp.getvariable("Local__MyVar", {instance: kmInst});
kmeApp.setvariable("Local__FromJXA", {instance: kmInst, to: "Set in JXA Script"})
Here's my simple test/demo macro:
###MACRO: Get Local Variables in JXA Script
~~~ β¦
But you're right, the KM Wiki does seem to need updating. I'll add that to my ToDo list.
However, the Wiki does correctly show how to access from JXA here:
Execute a JavaScript For Automation action (KM Wiki)
1 Like
Perhaps also worth adding a note on the distinction between:
code running from inside a KM action and accessing an Instance identifier, and
the same code being run/tested in Script Editor etc. where it wonβt obtain an instance, and will only see global variables ?
?
(For testing, it can, of course, simplify life to write code which covers the possibility of either evaluation context)
@ComplexPoint , thanks for all of your feedback about the wiki. Please take a look at the wiki update to see if it is clear and correct to you now.
manual:Variables [Keyboard Maestro Wiki]
4 Likes