In AppleScript or JavaScript how can you click menu item? I need to change a theme using a script for Mojave, Alfred and Sourcetree. I've already successfully done this with Mojave and Alfred using this script:

var alfredLightTheme = "Alfred"
var alfredDarkTheme = "Alfred Dark"

function run(args) {
    args = args ? args : []
    var systemEvents = Application("System Events")
    var alfred = Application("Alfred 3")

    if (args &amp;&amp; args == 'dark') {
        systemEvents.appearancePreferences.darkMode = true
        alfred.setTheme(alfredDarkTheme)
    } else if (args &amp;&amp; args == 'light') {
        systemEvents.appearancePreferences.darkMode = false
        alfred.setTheme(alfredLightTheme)
    } else {
        systemEvents.appearancePreferences.darkMode = !systemEvents.appearancePreferences.darkMode()
        alfred.setTheme(systemEvents.appearancePreferences.darkMode() ? alfredDarkTheme : alfredLightTheme)
    }

}


For Sourcetree it seems I need to click the menu items but how can I do that?
Since the OP is tagged with both AppleScript and JavaScript, and are two entirely separate languages, and it was not explicitly and specifically stated that the solution had to be only in JavaScript, here it some code that should work. I say "should work", because it does work on the applications tested on, but I do not have Sourcetree installed to test explicitly and specifically with it.

AppleScript code:

tell application "Sourcetree" to activate
delay 1
tell application "System Events"
    click menu item ¬
        "Dark" of menu 1 of menu item ¬
        "Theme" of menu 1 of menu bar item ¬
        "View" of menu bar 1 of application process "Sourcetree"
end tell


Note: The value of the delay command may need to be adjusted for your system.



JavaScript code:

menuItemClick("Sourcetree", ['View', 'Theme', 'Dark'])

function menuItemClick(strAppName, lstMenuPath) {
    var oApp = Application(strAppName),
        lngChain = lstMenuPath.length,
        blnResult = false;

    if (lngChain &gt; 1) {

        var appSE = Application("System Events"),
            lstApps = appSE.processes.where({
                name: strAppName
            }),
            procApp = lstApps.length ? lstApps[0] : null;

        if (procApp) {
            oApp.activate();
            var strMenu = lstMenuPath[0],
                fnMenu = procApp.menuBars[0].menus.byName(strMenu),
                lngLast = lngChain - 1;

            for (var i = 1; i &lt; lngLast; i++) {
                strMenu = lstMenuPath[i];
                fnMenu = fnMenu.menuItems[strMenu].menus[strMenu];
            }


            fnMenu.menuItems[
                lstMenuPath[lngLast]
            ].click();
            blnResult = true;
        }
    }
    return blnResult;
}


Note: The JavaScript code comes from jxaClickAppSubMenuItem.applescript by bumaociyuan and was forked from RobTrew/jxaClickAppSubMenuItem.applescript.