This week's sponsor

SaneBox

Clean Up Your Inbox Today (and Keep It That Way Forever)


TextBar Puts Your Text into the Menu Bar

TextBar is an awesome app which solves a problem that I have been struggling with for at least three years: “How can I put some text into the menu bar, such as the output of a shell script?” TextBar can do that, but it does it even better than I had hoped, because it also:

  • Lets you configure multiple items
  • Copies the item to the clipboard when you click it
  • Easily enables/disables individual items
  • Updates each item on its own time interval (some items might need to be updated every minute, some every 5, some 10, etc)

Here are some ways that I have been using TextBar.

My TextBar app looks like this:

which gives me a menu bar which looks like this:

When my SSID or public IP changes, TextBar gives me a notification via OS X’s notification system.

Note: You don’t have to put your commands into script files. You can enter the Unix commands directly into TextBar (see image at the top of this article). I prefer putting them in script files because I find it easier to edit a script, and because I use TextBar on several Macs and want them all to have the same commands, and I keep my /usr/local/scripts/ folder sync’d between them.

In the examples below I have formatted these shell commands using multiple lines for easier reading online, but you should use one line for TextBar. Remove any \ at the end of a line and make it all one line.

“What Wi-Fi network am I connected to?”

Using the airport command, I can show the SSID of the current Wi-Fi network with this:

    airport -I | awk -F': ' '/ SSID/{print $NF}'

That will handle SSIDs which have spaces in them too. I like having this shown because sometimes my Mac will connect to my regular Wi-Fi network instead of my 5 GHz network, which is obviously not what I want most of the time.

Note: This example is a actually a little oversimplified. To use this, you would have to use the full path of the `airport` command which is System » Library » PrivateFrameworks » Apple80211.framework » Versions » Current » Resources » airport and yes! you are correct, that is an absurd place for Apple to put it, but it has been there since Snow Leopard (maybe earlier) and shows no signs of moving.

To deal with this, I actually wrote a small shell script which looks for the `airport` command in that location. It also tells the user if no SSID was found. You can download my script from GitHub if you would prefer to use it.

“How much disk space do I have available?”

I used to use an app called FreeSpace Tab for this, which worked great, but is no longer available for some reason. Now I don’t need to install that app at all, I can just use this:

    df -H / \
    | egrep '/$' \
    | awk '{print $4}'

“What is my public IP address?”

What nerd doesn’t want to know this?

    curl -s -H 'Accept: application/json' ipinfo.io \
    | fgrep  '"ip":' \
    | tr -dc '[0-9].'

“How much battery do I have left?”

I stole this one from TextBar’s home page because it’s so cool I had to include it:

    ioreg -n AppleSmartBattery -r \
    | awk '$1~/Capacity/{c[$1]=$3} \
    END{OFMT="%.2f%%"; max=c["\"MaxCapacity\""]; \
    print (max>0? 100*c["\"CurrentCapacity\""]/max: "?")}'

That’s another complex command, so if you have any problems using it, you might want to download it as a gist from GitHub instead.

“What time is it?”

Ok, now I know what you’re going to say: "You can have the time shown using OS X’s own Date & Time Preference pane!" That’s true, but you are limited to the formatting that OS X chooses for you, or you have to muck around with the "Language & Region" settings, but then that changes how dates are displayed in other places in OS X, which might not be what you want.

For years I have used MagiCal to get the date/time formatting to show up the way that I want it to appear. Now I can just use date . And if you have the GNU utilities installed, you can use gdate which has even better controls.

    gdate +'%-I:%M %P (%a %b %d)'

gives me:

    4:06 pm (Sat Apr 18)

which is exactly what I wanted, even down to using "pm" instead of "PM" (Did I mention that I’m rather… particular? Well, now you know.)

I have this set to update every 30 seconds, which means that the time might be a minute behind real time (i.e. it shows the time as 12:02 when actually it’s 12:03). Honestly, nothing in my life requires accuracy of the time shown in my Mac’s menu bar. You could even use a fuzzy clock instead!

“What time is it in another time zone?

You can also use TextBar to show you the current time in other time zones.

For example, imagine if you lived in Memphis, Tennessee, but had a podcast with someone from the U.K. and and someone from Italy, and needed to know what time it is where they live. TextBar can help! Just use this:

    UKTIME=`TZ=GB date +"%H:%M"` ; echo "UK: $UKTIME"

and this

    CETTIME=`TZ=CET date +"%H:%M"` ; echo "Italy: $CETTIME"

and voilà! Entirely theoretical problem of coordinating podcast hosts from multiple timezones solved!

“How many drives are mounted?”

If you use a lot of external drives, you probably want to make sure they are unmounted before you put your Mac to sleep, lest you get that dreaded “Disk was not ejected properly” admonition.

This will show you a count of the currently mounted drives (excluding your boot drive):

    echo "Mounts: `/sbin/mount \
    | egrep '^/' \
    | fgrep -v ' on /' \
    | wc -l \
    | tr -dc '[0-9]'`"

I expanded this idea and turned it into a shell script: download “textbar-mountcount.sh” as a gist.

“Which computer is this and who am I logged in as?”

I often use Screen Sharing to connect to different Macs, and I can’t always remember which one I am using, especially if I am in full-screen mode. Put this in TextBar to show the hostname:

    scutil --get LocalHostName

You can also use ComputerName and LocalHostName which may give different results, depending on your configuration.

If you want to show your username too, use this:

    ME=`whoami`; WHERE=`scutil --get LocalHostName` ; echo "$ME@$WHERE"

Write a note to yourself

If you still have space left, you can use it to write yourself a note that you'll see whenever you look at your menu bar. Maybe there's an appointment or to-do you want an extra reminder for (in addition to your calendar, etc). Maybe you want to put together a shell script that shows you a "Message of the Day" or some other kind of daily affirmation.

To put a message into TextBar, just use the `echo` command:

    echo "Hello world!"

Anything else you can think of (Almost)

There are tons of other possibilities, but pretty much anything that can be expressed by a shell script, whether in zsh, bash, python, ruby, or even AppleScript, can be worked into TextBar.

You can download TextBar here. There is a free demo available, and you can buy it for $3, which is way cheaper than it should be, so I encourage you to buy licenses for your friends and family.

TextBar developer Rich Somerfield has other apps available on http://www.richsomerfield.com including several cool “freebies” so check those out too.

If you have trouble getting any of these scripts to work in TextBar, give me shout on Twitter and I’ll see what I can do to help.

Unlock MacStories Extras

Club MacStories offers exclusive access to extra MacStories content, delivered every week; it’s also a way to support us directly.

Club MacStories will help you discover the best apps for your devices and get the most out of your iPhone, iPad, and Mac. Plus, it’s made in Italy.

Starting at $5/month, with an annual option available. Join the Club.

A Club MacStories membership includes:

  • MacStories Weekly newsletter, delivered every week on Friday with app collections, tips, iOS workflows, and more;
  • Monthly Log newsletter, delivered once every month with behind-the-scenes stories, app notes, personal journals, and more;
  • Access to occasional giveaways, discounts, and free downloads.