Questions Regarding AppleScript and JavaScript for Automation

oh I thought it would return a number and not a string.
ok.
I will try that.

Keyboard Maestro variables are always and only strings.

(() => {
    "use strict";

    const 
        thresholdString = "5",
        thresholdNumber = parseInt(thresholdString, 10);

    return ["3", "4", "5", "6", "7", "8"].map(
        s => thresholdNumber < parseInt(s, 10) ? (
            `${s} hi`
        ) : `${s} lo`
    ).join("\n");
})();

Ok this time, no syntax errors in vscode. Fixed that up.
Not sure why its not outputting the values into the input form this time round.

Group.kmactions (3.6 KB)

What does "the" values mean to you ?

What are their names ?

How and where are you returning those named values to Keyboard Maestro ?

Prompt for User input will display anything that I feed in there. I
its not being fed cos there is a problem with this line:

km.setVariable('Variant type', {to: JA_Variant_Type});

I take this line out and the input box displays and when I put it back in, it doesnt.
Have I lost the scope of the km variable?
Do I have to use kmInst ever though im not using local or global?

The only one i used is instance at the front of a variable cos that it cleared before use. Previously I was clearing myself via code.

I have since changed that line to: jm.setVariable('Variant type', {to: JA_Variant_Type.toString()}) cos maybe became an integer by mistake and that didnt fix the problem either

Group.kmactions (3.5 KB)

so in

"the values" refers to the single variable name JA_Variant_Type ?

I don't see anything resembling

%Variable%JA_Variant_Type%

in the display text of your prompt ...

Does JA_Variant_Type not appear in the Keyboard Maestro Editor
under Preferences > Variables ?

JA_Variant_Type is the Javascript Automation variable hence the prefix
The other one Variant type is the KM variable.

The reason why I dont have a prefix like instance or KM in front of the KM variable is so that my variables appear in the forms in their proper names and I dont use underscores inside forms. Looks like code speak and not english.

eg.
Name
Age
etc

and what value does the KM variable "Variant type" contain after this process ?

(if you look in Preferences > Variables, for example)

Incidentally if JA_Variant_Type is a string (as required by those concatenations you are applying) then .toString() is redundant.

Its not in preferences. Does not show up in the list of variables. Either way with or without .toString(), it doesnt display.

so what happens if you execute this in VSCode or Script Editor ?


while and ++ etc is always fiddly and a mess to trace incidentally.

Much simpler to let JS do the work and switch to .map or .reduce

well I had to change it to this:

if (JA_eight_bit_integer_incrementer < JA_Number) {
  JA_Variant_Type = JA_Variant_Type + 'u8,';
  myVariant_type = JA_Variant_Type;
} else {
  JA_Variant_Type = JA_Variant_Type + 'u8';
  myVariant_type = JA_Variant_Type;
}

console.log(myVariant_type)

cos ive never seen that other kind of syntax before getVariable and setVariable in javascript so I stripped it out. lol, i never did test the while loop inside javascript.

I think you may be trying to run before you can walk.

I would suggest that you put all these actions aside for the moment, and simply describe what value you are actually trying to define.

Show us an example of:

  • the output you hope for, and
  • the input(s) that went into it.

(Not as as screenshot, just as text)

Dont worry. I figured it out.

its setvariable not setVariable. Just simply ridiculous. Hours on some ridiculous problem. Is there some static analysis tool coming for the AS or JA?

Yes, all names are context sensitive in JS, and the sdef listings for AS and JS in Script Editor are always useful:

Screenshot 2022-03-19 at 18.39.19

Incidentally, Array.join(",") may turn out to be simpler (fewer moving parts – more maintainable) than trudging through a loop with conditionals and mutating variables:

Group.kmactions (4.8 KB)

(() => {
    "use strict";

    const
        app = Object.assign(
            Application.currentApplication(), {
                includeStandardAdditions: true
            }),
        kmInst = app.systemAttribute("KMINSTANCE"),
        km = Application("Keyboard Maestro Engine");

    const
        JA_eight_bit_integer_incrementer = parseInt(
            km.getvariable("local_eight_bit_integer_incrementer", {
                instance: kmInst
            }), 10
        ),
        JA_Number = parseInt(km.getvariable("Number"), 10);

    km.setvariable("Variant type", {
        to: Array.from({
                length: 1 + (
                    JA_Number - JA_eight_bit_integer_incrementer
                )
            }, () => "u8")
            .join(",")
    });
})();

what is that image?
The one that says Terminology, Kind, Suite. Ive never seen that before.
How do I get that?

Ben

Hide and show the macro again.

Cmd-Opt-G then Cmd-Opt-M

Drop the Keyboard Maestro Editor on the Apple Script Editor.app to show its available scripting dictionaries.

Ok in your suite column, it says Keyboard Maestro Engine.
In my suite column, it says Keyboard Maestro Suite.

Why am I not seeing the engine?
Why is this not in the manual?

Ben

Whups. I missed “Engine Suite” when I looked at the image.

Finding the Keyboard Maestro Engine in the Finder

and another approach is to:

  • open Script Editor (with the language drop-down at top left set to either JavaScript or AppleScript)
  • choose File > Open Dictionary...
  • browse through the apps there to Keyboard Maestro Engine