JavaScript chooseFromList window focus

I am trying to run JavaScript chooseFromList user interface where from the keyboard only user can select the item. It works except that the list window does not have focus requiring extra mouse click on the window before being able to select item by typing first couple letters from keyboard. If I run from within Mac Script Editor the list does popup with focus as desired but can’t figure out how to make this happen within Keybaord Maestro. Does anyone know if there is a way to have the list have focus when executed. I do something similar in AppleScript within Keybaord Maestro choose from list which does have focus when list displayed.

var app = Application.currentApplication()
app.includeStandardAdditions = true
r = app.chooseFromList([‘red’, ‘green’, ‘blue’], { withPrompt: ‘What is your favorite color?’ })

I appreciate if you are able to provide guidance on this.

Calling dialogues and giving them focus is always complicated when the script is being run, through osascript, by an app like Keyboard Maestro.

Essentially, you need to activate the app for which standard additions have been included, and which is raising the dialog. One traditional solution is to make that app something like Application("System Events") or Application("SystemUIServer")

Focus.kmmacros (18.6 KB)

(function () {
    'use strict';

    var su = Application("SystemUIServer");

    su.includeStandardAdditions = true;

    su.activate(); // <---
    
    var lst = ['red', 'green', 'blue'],
        choice = su.chooseFromList(lst, {
        withPrompt: 'What is your favorite color?',
        withTitle: 'Activate the app providing the dialog',
        defaultItems: lst[0]
    });
    
    return choice;

})();
1 Like

Thank you … works perfectly

Nice JXA/KM tip! :+1:

Thanks Rob.