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 && args == 'dark') {
        systemEvents.appearancePreferences.darkMode = true
    } else if (args && args == 'light') {
        systemEvents.appearancePreferences.darkMode = false
    } 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?

sourcetree change theme

1 Answer 1


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 > 1) {

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

        if (procApp) {
            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];

            blnResult = true;
    return blnResult;

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .