BitBar Lets You Put Anything in Your Mac’s Menu Bar

posted in: July 2020 | 0
16 comments

BitBar Lets You Put Anything in Your Mac’s Menu Bar

There are all sorts of apps that add various capabilities to your Mac’s menu bar, but I’ve stumbled across one that can add literally anything to the menu bar through a plug-in system. It’s called BitBar, and it’s both free and open source. I’ve tested and confirmed that it works in both macOS 10.14 Mojave and 10.15 Catalina.

When you first launch BitBar, it asks you to choose a directory to store plug-ins. I made a BitBar folder on my iCloud Drive so I can easily sync plug-ins between machines. That works well, and I presume Dropbox or Google Drive would work similarly.

The BitBar Web site contains a rich library of community-created plug-ins. There are plug-ins for displaying weather, stock prices, cryptocurrency prices, email messages, Slack notifications, and pretty much anything you can think of, such as whether your cat is in or out. Installing a plug-in is as easy as clicking the Add to BitBar button on a plug-in’s library page, which installs the plug-in into your specified plug-in folder.

The Add to BitBar button in context.

All BitBar plug-ins are simple text-based shell scripts. Removing one is as simple as moving it out of your plug-in folder, Control-clicking BitBar in the menu bar, and choosing Preferences > Refresh All (I disable plug-ins by putting them in an Archive folder in my plug-in folder). Likewise, you can drag any shell script you want into the plug-in folder and refresh to make it available. You can quickly get to your plug-in folder by Control-clicking BitBar and choosing Preferences > Open Plugin Folder.

The beauty of BitBar is that any script that works in Terminal can be a BitBar plug-in. It could be a simple shell script, a Python script, a Ruby script, or even an AppleScript if you put the right hooks in. It also means that you can easily modify all the available plug-ins in any text editor, and you may be able to figure out how to make small changes just by following the script’s example, even if you don’t know the scripting language well. In fact, that’s a common situation.

Modifying BitBar Plug-ins to Get What You Want

Imagine that you find a plug-in that does kind of what you want, but not quite. The good news is that it’s easy to modify a plug-in.

For instance, I took the Got Internet? plug-in, changed its ping_addressvariable from 8.8.8.8 (Google DNS) to tidbits.com, and changed the display text so that it shows “TidBITS 👍” when our Web site is reachable and “TidBITS 👎” when it’s not.

Here’s a little hack you might find useful. Install the Simple RSS Readerplug-in, open up its file, and change the FEED_URL variable, which is by default http://feedpress.me/sixcolors?type=xml, to https://tidbits.com/feed/. You can now access all the latest TidBITS articles from your menu bar!

BitBar showing the latest TidBITS headlines.

I also tweaked the Coinbase Prices plug-in. It shows a bunch of different cryptocurrencies by default, but not the one I mostly care about: Chainlink. So I simply copied the dash_price line of code, pasted it at the end of the block, renamed it to link_price and changed DASH-USD in the Coinbase URL to LINK-USD. Finally, I added an echo $link_price line at the end. This was just monkey scripting—the script is in Python, which I don’t know well, and my changes worked just fine, aside from not showing a fancy icon on the side of the price.

Code modified in the Coinbase Prices plug-in to show LINK.

The plug-in that I’ve modified the most heavily—and the one that actually led me to BitBar—is mpd-control. After getting fed up with iTunes/Music messing up my music library, I’ve been experimenting with the command-line-based mpd music daemon, which has a number of front ends. I like the Terminal-based ncmpcpp, but it’s awkward having to switch to a Terminal window to pause my music. The mpd-control plug-in, by default, shows the currently playing track and clicking it plays or pauses the music. It was simple enough to change it so that I have a menu of commands that let me play/pause, move to the next or previous track, and open ncmpcpp in a Terminal window.

The mpd-control plug-in modified to do what I want.

If you’re not as nerdy as I am, there’s also a nice iTunes Now Playingplug-in to control your music from the menu bar.

For an extreme example, I brought in a menu bar script I use in Linux to see the weather forecast. Amazingly, it worked pretty much out of the box. I modified it to add an option to open Terminal and curl wttr.in, which brings up a console-based weather forecast.

wttr.io in a Terminal window.

Creating Plug-ins in BitBar

Let me show you how easy it is to create a BitBar plug-in. If you don’t know any shell scripting, this is a great way to learn, since you can easily integrate your shell scripts with the native macOS interface without much risk. And if you don’t want to learn, you could sweet-talk or pay someone to create the plug-in you need.

You don’t have to start from scratch. The BitBar documentation includes several top-to-bottom examples, and the plug-in gallery even has tutorial plug-ins illustrating different concepts that you can modify to your heart’s content. But let me show you how I created a plug-in from scratch to demonstrate how simple it can be.

Open Terminal in /Applications/Utilities. Don’t worry, we’re not doing anything scary. Type date and press Return. You get the current date and time.

Output of "date" in the Terminal.

You can easily put that into BitBar. I opened BBEdit and made this simple script called date.sh.

#!/bin/bash
date

The first line, #!/bin/bash, tells the system that it’s a script. Once you make that text file executable, macOS will recognize it as a program (see below for instructions) and execute any Terminal commands that follow the first line. The next line simply invokes the date command. In other words, typing date in Terminal shows you the current date and time, and if you put that command in a BitBar script, it will do the same thing.

Making a script file executable can be a little intimidating if you’re not used to the Terminal, but it’s not hard. You have to do this so it can be run as a program.

  1. Open Terminal.
  2. Open the BitBar plug-in folder in Finder.
  3. In the Terminal, type chmod +x  (including the trailing space) to make the script executable.
  4. Drag the date.sh icon from the the Finder window into Terminal, which inserts its directory path.
  5. Press Return.

Refresh BitBar, and you now have a date and time display.

A simple "date" plug-in in BitBar.

If you type man date in Terminal to see the manual pages for the date command, you’ll see examples of how you can customize the output. Feel free to play around with it.

But what if we want to make it so clicking the date opens the Calendar app? That’s easy to do, but it takes some jiggering. There are special formatting hooks you can add to a script to add BitBar actions, listed on BitBar’s GitHub page. But they work only in the context of an echocommand, which is a simple Unix command to display text or the value of a variable in Terminal.

So first, I changed the script to assign the output of the date command to a MYDATE variable, like so: MYDATE=$(date) (note there are no spaces around the equals sign!). Don’t let the symbols intimidate you—you’re just creating a variable called MYDATE. The dollar sign and parentheses tell the system to assign the output of the date command to that variable.

Next, I created a line that tells BitBar to display the date and make it clickable. If I were to write just echo $MYDATE, it would show only the contents of the MYDATE variable—no different than before. But I then added the special BitBar hooks, so it looks like this:

echo "$MYDATE | href=file:///Applications/Calendar.app"

First, it echoes the MYDATE variable. (Note that in shell scripting, you refer to a variable by putting a dollar sign ($) in front of the variable name.) Inside the quotes, I put not only that MYDATE variable but also the BitBar-specific commands, delimited by the pipe symbol. If you are familiar with HTML, you’ll recognize the href command, which specifies a link. I use that to create a link to the Calendar app.

After a refresh, I could click the date to open the Calendar app. The full script looks like this:

#!/bin/bash

MYDATE=$(date)
echo "$MYDATE | href=file:///Applications/Calendar.app"

But what if you want a menu of commands? You can accomplish that by adding echo "---" anywhere in the BitBar plug-in script. However, with the way the plug-in is set up now, I won’t see the menu because it opens Calendar whenever I click it. So I need to change it back to make it not clickable:

#!/bin/bash

date
echo "---"

But what commands should we put in our menu? Let’s make a clickable link to the Calendar app, like this:

echo "Calendar | href=file:///Applications/Calendar.app"

All I’m doing here is echoing the word “Calendar” and using the code from the previous example to make it open the Calendar app when clicked.

So our final plug-in script looks like this and shows the date as a menu from which you can open the Calendar app:

#!/bin/bash

date
echo "---"
echo "Calendar | href=file:///Applications/Calendar.app"

You might notice that the time it reports is slightly off. By default, each BitBar plug-in refreshes once every minute. You can change this by simply adding a timestamp to the filename, like this: date.1s.sh. Change that to date.1h.sh to refresh every hour or date.1d.sh to refresh once per day. Check the BitBar documentation for more info.

You can take this basic example and expand it in a few ways:

  • Change the date format
  • Open other calendar apps like Fantastical or BusyCal
  • Create a shortcut to the Date & Time preference pane

Making a Bookmarks Plug-in

I often switch between Web browsers, and I get tired of trying to keep my bookmarks in sync. I could use Alco Blom’s URL Manager Pro, but in the interests of rolling my own, I’ve been thinking that a good solution would be a simple text file with one URL per line. Text editors like BBEdit usually let you easily follow URLs in text files (in BBEdit, Command-click a URL to open it in your default browser). While playing with BitBar, I thought: “Wait, maybe I could turn a list of links into a plug-in.” It turned out to be pretty easy, though I’m still working on perfecting it.

My first plug-in along these lines was extremely basic. I created a list of URLs and saved it in ~/Documents/urls.txt. (If you want to follow along, you can name it whatever you want and store it wherever you want.)

I then structured the script like so:

#!/bin/bash # Tell the system this is a script

LINKFILE=~/Documents/urls.txt
# Create a variable called LINKFILE that points to my URL list,
# which is cleaner than referring to the list directly in the code.

echo "URLs"
echo "---"

These two lines tell BitBar to make “URLs” the display name and echo --- indicates a submenu.

Here’s where things get interesting. We’re going to bring in our URL list and echo each line along with the command that tells BitBar it’s a link.

while read line; do
    echo "$line|href=$line"
done <$LINKFILE

The key part is at the end, <$LINKFILE, which tells the shell to import every line of that file into the while loop. while read line translates to “While this file has a line I have not processed, read the line in and store it in variable line.” And do just means do. While there are lines to process, it will echo that line, $line, along with the pipe character and href=, which tells BitBar that the following text is a link. The second $line simply tells BitBar to link the title $line to the URL $line. In this case, the link title and link URL are identical.

In practice, it looks like this:

A simple bookmarks plug-in with raw URLs.

That works, but I’d rather have descriptive names instead of raw URLs. Also, I have a lot of bookmarks and only so much screen real estate. So I tried my hand at creating a Markdown-based bookmark file that would contain descriptive names and a menu hierarchy. Such text processing necessitated using regular expressions, and the simplest way to do that in a shell script is the sed command, with which I’m not very familiar. So I Googled enough sed commands to make it sort of work:

#!/bin/bash

LINKFILE=~/Documents/SyncThing/links.md

echo "Bookmarks"
echo "---"

while read line; do
    [ -z "$line" ] && continue
    LINKNAME=`echo $line | sed 's/^.*\[//;s/\].*$//'`
    LINK=`echo $line | sed 's/^.*(//;s/).*$//'`
    echo "$LINKNAME|href=$LINK"
done <$LINKFILE

The above code isn’t that different from the first bookmark script. It cycles through a list of links, only in Markdown this time, and in each line it uses sed to extract either the title or the URL and add it to a variable. It then echoes the LINKNAME variable and tells BitBar to link it to the URL stored in LINKURL. The [ -z "$line" ] && continue line at the top of the while loop tells the shell to skip blank lines.

Note the backticks (`) around the commands assigned to LINKNAME and LINK. They tell macOS to assign the results of the commands to the variable instead of just the text.

I have one problem with this setup. The way BitBar does sub-menus is weird. In Markdown, headings work like this:

#Bookmarks
[TidBITS](https://tidbits.com/)
[TidBITS Talk](https://talk.tidbits.com/)
[The Prepared](https://theprepared.com/)

##News
[Slashdot](https://slashdot.org/)

A single # indicates a top-level header, ## is a subhead, and so on. However, to get it to work with BitBar, I have to format my Markdown URL list like this:

[TidBITS](https://tidbits.com/)
[TidBITS Talk](https://talk.tidbits.com/)
[The Prepared](https://theprepared.com/)

News
[--Slashdot](https://slashdot.org/)

So the subfolder name is “News,” and to indicate that an item goes into that subfolder, I have to put two dashes in front of the line. I’m sure it’s possible to create a script to count the number of lines under a Markdown header, strip away the # characters, and insert -- in front of each line under the header, but I don’t know sed and awk that well. (If you do, let me know, and you’ll earn my eternal regular expression appreciation.)

However, the result in BitBar is nice and tidy, and choosing one of these bookmarks from the menu opens it in my default Web browser. When I switch browsers, all I have to do is change which one is the default to make my bookmarks work with the new one instead.

The Markdown bookmark plug-in in action.

BitBar Can Be Anything You Want

I hope I’ve given you a good idea of some of the things you can do with BitBar, even if you’re not super comfortable with the Terminal or scripting. If you’ve ever thought, “I wish I could put that in my menu bar,” there is probably a way to do it with BitBar. You’re limited only by your imagination and what you or someone you know can do in a shell script.

If you want to download the BitBar plug-ins I’m using, creating, and editing, you can find them on my GitHub page.