Deleting (some) Keyboard Maestro variables – protecting others

Scripted deletion of Keyboard Maestro variables is not to be undertaken lightly, but is not impossible, and can be done with a ‘Keep list’ of variables which are to be protected from deletion.

For example, the following script (set the Script Editor language selector to JavaScript), contains a list of the names of variables which should not be deleted.

If you run it, it returns a list of variables which are protected, and another list of the remaining variables which would all be deleted.

(if, that is, the value of delete option at the bottom of the script were ever edited to true)

If you do experiment with it, edit the keep list to whatever you need, checking carefully that your edit contains no typos or omissions whatsoever …

Possible, but not necessarily recommended.

(function (dctOptions) {
    'use strict';

    // KEEP LIST
    // variables whose names are NOT listed here will be deleted
    var lstKeep = [
            'NoteApp',
            'NoteDateAdjust',
            'NoteExtn',
            'NoteFolder',
            'NotePrefix',
            'soundDeviceONE',
            'soundDeviceTWO',
            'man page PDF folder',
            'Shell command'
        ];

    var kme = Application("com.stairways.keyboardmaestro.engine"),
        kmVars = kme.variables,
        lstVars = kmVars();
            
    var  lstToDelete = lstVars
        .filter(function (v) {
            return lstKeep.indexOf(v.name()) === -1;
        }),
        lstAllNames = lstVars.map(function (x) {
            return x.name();
        }),
        lstZapNames = lstToDelete.map(function (x) {
            return x.name();
        });

    if (dctOptions.delete === true) {
        lstToDelete.forEach(function (x) {
            x.value = '%Delete%';
        })
    }

    if (dctOptions.list === true) {
        return {
            remaining: lstAllNames,
            protected: lstKeep,
            deletable: lstZapNames
        };
    }

})({
    delete: false,
    list: true
});
2 Likes

Hi

Would it be possible to create a script that does the opposite?
Deletes the variables listed in the list.

Then I could add this at the end of a macro instead of then Set variable to %Delete%.

Would it be possible to create a script that does the opposite ?

You could experiment (carefully : - ) with something like this, I think


(function (dctOptions) {
    'use strict';

    // ZAP LIST
    // variables whose names ARE listed here will be deleted

    var lstZapNames = [
        'allTheUsualSuspects',
        'superNumerary',
        'noLongerNeeded'
    ];

    var kme = Application("com.stairways.keyboardmaestro.engine"),
        kmVars = kme.variables,
        lstVars = kmVars();

    var lstToDelete = lstVars.filter(function (v) {
            return lstZapNames.indexOf(v.name()) !== -1;
        }),
        lstToGoNames = lstToDelete
        .map(function (x) {
            return x.name();
        }),
        lstKeep = lstVars
        .filter(function (v) {
            return lstZapNames.indexOf(v.name()) === -1;
        })
        .map(function (x) {
            return x.name();
        }),
        lstAllNames = lstVars.map(function (x) {
            return x.name();
        });


    if (dctOptions.delete === true) {
        lstToDelete.forEach(function (x) {
            x.value = '%Delete%';
        })
    }

    if (dctOptions.list === true) {
        return {
            remaining: lstAllNames,
            protected: lstKeep,
            deletable: lstToGoNames
        };
    }

})({
    delete: false,
    list: true
});
2 Likes

Thanks. I will try it and report back

Thanks, Rob. That could be very useful.

Maybe when you have time you could turn that into another of your famous custom Action Plugins, where we could just list each variable to be deleted on a separate line. That would be awesome. :sunglasses: