Home / Apple / BitBar, PHP, AppleScript and Dark Mode – Six colors

BitBar, PHP, AppleScript and Dark Mode – Six colors



I woke up this morning to find a challenge from Reader Max Velasco Knott. Max apparently likes Dark Mode on macOS – and my BitBar plugins do not consider the existence of Dark Mode, mainly because I never use it.

So I started trying to figure out how to create a BitBar plugin that supports Dark Mode. The answer, of course, is AppleScript:

    tell application "System Events"
        tell appearance preferences
            set theMode to dark mode
        end tell
    end tell
    return theMode

This script is coming back true if we are in dark mode and false if we are not. (I found several other suggestions online that used Apple defaults read terminal command to detect theme status, but they did not work for me in the Big Sur beta, and this did, so I go with this.)

But … my air quality script is written in PHP, so how can I bring AppleScript to a PHP script? This is how:

$darkmode = shell_exec('osascript <

Basically you use shell_exec to drive osascript command which is how to run AppleScript scripts from the command line. Then I make a Boolean, $darkmodestate, because I'm bad at PHP and it's hard to match strings.

Reader Max actually does not like my use of color in the menu bar, so he said he had made a version of my plugin that was monochrome. Which, okay! Here is my "AQI colors" subroutine, rebuilt to support Max's desire for dark mode and monochrome:

if ( $darkmodestate ) {
// we are in dark mode
        return '#ffffff';
} else {
// we are in light mode
        return '#000000';
    }
}

My personal version of this plugin has been updated to support Dark Mode, with different colors selected for both modes for (slightly) improved readability. And I have also made the monochrome version available. BitBar is still a free tool.

If you appreciate articles like this, you can support us by becoming a Six Colors subscriber. Subscribers get access to an exclusive podcast, members-only stories and a special community.




Source link