En AppleScript o JavaScript, ¿cómo puede hacer clic en el elemento del menú?

Necesito cambiar un tema usando un script para Mojave, Alfred y Sourcetree. Ya lo hice con éxito con Mojave y Alfred usando este 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 && args == 'dark') {
        systemEvents.appearancePreferences.darkMode = true
        alfred.setTheme(alfredDarkTheme)
    } else if (args && args == 'light') {
        systemEvents.appearancePreferences.darkMode = false
        alfred.setTheme(alfredLightTheme)
    } else {
        systemEvents.appearancePreferences.darkMode = !systemEvents.appearancePreferences.darkMode()
        alfred.setTheme(systemEvents.appearancePreferences.darkMode() ? alfredDarkTheme : alfredLightTheme)
    }

}

Para Sourcetree, parece que necesito hacer clic en los elementos del menú, pero ¿cómo puedo hacerlo?

tema de cambio de sourcetree

Respuestas (1)

Dado que el OP está etiquetado con AppleScript y JavaScript , y son dos idiomas completamente separados, y no se indicó explícita y específicamente que la solución tenía que estar solo en JavaScript , aquí hay un código que debería funcionar. Digo "debería funcionar", porque funciona en las aplicaciones probadas, pero no tengo Sourcetree instalado para probar explícita y específicamente con él.

Código AppleScript :

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

Nota: Es posible que sea necesario ajustar el valor del delay comando para su sistema.


Código JavaScript :

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

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

    if (lngChain > 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 < lngLast; i++) {
                strMenu = lstMenuPath[i];
                fnMenu = fnMenu.menuItems[strMenu].menus[strMenu];
            }


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

Nota: el código JavaScript proviene de jxaClickAppSubMenuItem.applescript de bumaociyuan y se bifurcó de RobTrew/jxaClickAppSubMenuItem.applescript .