Preferences : Variables

In KM 10.0.1

Is there a way to change the height and width of the 'Preferences : Variables' window?

Or is there a different tool used for exposing variable values?

I don't know of any way to change the size of that pane, but you can also use the Value Inspector to see variables contents in real time. Granted, in some ways it's not as convenient as the Preferences pane since you have to manually type in the variables/tokens etc. that you want to see, but it's very handy either way.

-Chris

1 Like

Someone might be able to create a macro that auto-populates the Value Inspector using the values in the %AccessedVariables% token. That might earn someone three claps. It doesn't look too hard.

Hmm, it would probably have to be code that is run from inside the macro in question. I guess that's inconvenient, but it would still work. A person would just have to copy the code into their macro and run in from there.

1 Like

Well now I know what I’ll be tooling with tomorrow in my free time :sweat_smile:

Sure. There's no need to give me any credit. I know I helped.

1 Like

Hey Roy,

Nyet. It's been discussed, and Peter will not make it resizable.

The value inspector window has been mentioned.

If you want more then use BBEdit. The commercial version reverts to a freeware “lite” version after a trial period – it is still very powerful and scriptable.

It's easy to script the display of a variable's contents into a BBEdit document and vice versa. You also get very powerful find/replace.

-Chris

1 Like

Ooh - interesting idea, @Sleepy. I've put my first draft here (I thought a separate thread might be neater). Sorry if I'm treading on your toes here @cdthomer - I just fancied the challenge :grinning: )

Haha no toes have been treaded on. I didn’t even have time to look into this yesterday so thank you for doing so :grin:

-Chris

1 Like

You can, of course, copy the full set of variable names and values as a key:value JSON dictionary, if you want to paste that into a text editor for inspection:

Copy all KM variable names and values as JSON.kmmacros (2.9 KB)

Expand disclosure triangle to view JS Source
(() => {
    "use strict";

    // All KM variable name and values visible
    // in JSON format.

    // Rob Trew @2021
    // Ver 0.1

    const main = () => {
        const
            variables = Application(
                "Keyboard Maestro Engine"
            ).variables;

        return JSON.stringify(
            Object.fromEntries(
                zip(
                    variables.name()
                )(
                    variables.value()
                )
            ),
            null, 2
        );
    };

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

    // zip :: [a] -> [b] -> [(a, b)]
    const zip = xs =>
        // The paired members of xs and ys, up to
        // the length of the shorter of the two lists.
        ys => Array.from({
            length: Math.min(xs.length, ys.length)
        }, (_, i) => [xs[i], ys[i]]);

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

Also possible with AppleScript, if you prefer:

Expand disclosure triangle to view AppleScript
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

-- JSON from full set of Keyboard Maestro variables
on run
    tell application "Keyboard Maestro Engine"
        tell variables
            set ks to name
            set vs to value
        end tell
    end tell
    
    set json to showJSON(dictFromZip(ks, vs) as record)
    set the clipboard to json
    json
end run


-- dictFromZip :: NSArray -> NSArray -> NSDictionary
on dictFromZip(ks, vs)
    tell current application
        its (NSDictionary's ¬
            dictionaryWithObjects:vs forKeys:ks)
    end tell
end dictFromZip


-- showJSON :: a -> String
on showJSON(x)
    set c to class of x
    if (c is list) or (c is record) then
        set ca to current application
        set {json, e} to ca's NSJSONSerialization's dataWithJSONObject:x options:1 |error|:(reference)
        if json is missing value then
            e's localizedDescription() as text
        else
            (ca's NSString's alloc()'s initWithData:json encoding:(ca's NSUTF8StringEncoding)) as text
        end if
    else if c is date then
        "\"" & ((x - (time to GMT)) as «class isot» as string) & ".000Z" & "\""
    else if c is text then
        "\"" & x & "\""
    else if (c is integer or c is real) then
        x as text
    else if c is class then
        "null"
    else
        try
            x as text
        on error
            ("«" & c as text) & "»"
        end try
    end if
end showJSON
2 Likes

Oh these are all neat ideas. Thank you :slight_smile: