Reading Instance variables from scripts. (Does seem to be possible)

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:

And I posted the JXA access here:

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)

Good point.

@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