Do "Instance" Variables Work Across AppleScript to JXA?

I have a macro that uses a AppleScript and a JavaScript For Automation.

Can I use an instance (temporary) variable between those two?

I am having trouble getting variable to work.

Vanilla variables clearly work across different kinds of script actions, and I personally seem to have reverted to plain variables generally, combined with some intermittent spring cleaning.

See: Listing KM Variables by Descending Size (And Optionally Clearing Some)

but I think I may well be missing something, even with Local variables, which seem to be legible here from an Execute Shell action, but not (at least not in the way that I am trying) from Execute (JS | AS) actions:

local vars between different execute actions.kmmacros (19.5 KB)

1 Like

For local and instance variables you need to reference the KM instance as follows:

set kmInst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
	set kmLocalVar1 to getvariable "Local__SomeLocalVariable" instance kmInst
	setvariable "Local__FromAS" instance kmInst to "Variable set in AppleScript."
end tell

There’s something similar for Javascript.

RTM on variables https://wiki.keyboardmaestro.com/manual/Variables specifically the bit on variables in scripts...

2 Likes

In JS, assuming that the standardAdditions property on some application object has been set to true,

In a pattern like this, for example:

// standardAdditions :: () -> Application
const standardAdditions = () =>
    Object.assign(Application.currentApplication(), {
        includeStandardAdditions: true
    });

Then I think we should have access to the app.systemAttribute method,

but to be honest, I like to code things out quite fast, and the cost / value of non-global KM Vars didn't feel quite right to me in the end – I see that the most recent code in which I used them appears to be from 2015 ...

1 Like

We could wrap it like this in JavaScript for Automation, I think:

(() => {
    'use strict';

    const main = () => {
        return kmInstanceID();
    }

    // --------------------- GENERIC ---------------------

    // kmInstanceID :: () -> String
    const kmInstanceID = () =>
        Object.assign(Application.currentApplication(), {
            includeStandardAdditions: true
        }).systemAttribute('KMINSTANCE')


    return main();
})();
1 Like

Well, the KM wiki gives this example for JXA:

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"})

Not being at all conversant with JS I can’t say how that compares/contrasts with your suggestion or why I would want to use one over the other. Just me being ignorant again!

2 Likes

Yes. It should work fine.
If you want help debugging, please upload your macro with both scripts, and highlight where you think it is not working.

However, you may want to use Local Variables instead of Instance Variables, unless you need to share the variables with a sub-macro.

But scripts can see both Local and Instance Variables.

You may want to read:
Tip: How Do I Get The Best Answer in the Shortest Time?

1 Like

Thank you all! Very helpful info!

2 Likes