Questions Regarding AppleScript and JavaScript for Automation

Questions regarding applescript

Keyboard Maestro Actions.kmactions (2.9 KB)

Hello,

I am trying to convert the bottom if statements into Applescript cos they take up too much screen real-estate.
I want to know if I always have to create a Applescript variable every time I get a KM variable?
Also wanted to know how I append with applescript and KM.
I can see that you can append with & but how would that look like?
Is there also anyway to shrink Applescript to fit only the text. As you see there is a huge waste of whitespace at the bottom using up my screen realestate
Ben

Another problem. This time im trying Javascript for automation.
Ive tried two different approaches. Neither works. I cant even get and return a variable.

Group.kmactions (3.0 KB)

Group.kmactions (3.1 KB)

Ive made a lil progress on retrieving variables from KM to JXA as well as making changes to the variables value and returning them back to KM. However im still having some problems.

For one this do while loop doesnt work.
Am I approaching this right? Anyone know why this doesnt work?

Group Action (v10.0.2)

Group.kmactions (3.5 KB)

Ben

You may get a faster response by sparing your readers the inductive effort building an understanding what you are actually trying to do :slight_smile:

  • What is the change that you seek in these variable values ?
  • And under what conditions do you want to apply or avoid that change ?

Incidentally the font sizes in the screen grab will be larger, and impose less effort on potential readers, if you narrow the window and actions to just fit the right text margin of the code before you capture.

(Thousands of pixels are devoted to white space at the right, in those shots)


Indcidentally

someName.concat("u8,")

Just defines a value – the existing value of someName followed by an affix – it's up to you to make a copy of that value or give it a name.

Nothing is "done to" someName there.

"hello".concat("goodbye")

defines a longer string, without binding a name to it. Nothing is 'done to' "hello", and no name is yet given to that longer string.

ah yes, I will narrow the window before snapshoting. Never thought how that could make it better for readers. I always have it at full screen.

well in the first image, I was trying to replicate the actions below but inside applescript. I only got so far.
Had no responses so I moved onto JXA.

I have managed to return a variable so that moves me onto the last image and the first.
The last image is where I was trying to concatenate to Variant Type. It should be something like u8, u8, u8 where there is no , on the last iteration.
Well thats how I do it.

I just saw your additional comments @ComplexPoint.

I still would like to know how I would have achieved the same in Applescript.
I do not know why Syntax highlighting isnt available in JXA. It is slow in Applescript but yeah.

Ben

What we need is a description of the problem :slight_smile:

You are are comparing two values and appending an affix to one of them depending on the result of that comparison ?

The comparison is numeric ? (8 > 3)
or lexical ? ("abc" < "xyz")

its numeric. The original posts were missing the initial while loop. My aim was to reduce the number of actions in this macro from 177 to a much lower number so that I maintain and add to it.

While.kmactions (7.5 KB)

the first action Set variable will be gone. Will write that also in JXA or AssemblyScript.

The comparison is numeric

but Keyboard Maestro variables are always strings.

What would you expect the value of this expression to be – true or false ?

(() => {
    "use strict";

    return "2" < "1000";
})();

If you want a numeric comparison, you must first convert the numeric strings to number values, e.g. with parseInt(numberString, 10) if the numeric values are decimal.

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