Down the rabbit hole: Toggle Visibility for Date/Time in macOS menu bar

Where do I even start? I record a lot of content for Channel 9 and YouTube and I always turn off the date and time in my menu bar when recording (makes for cleaner editing). I have a dedicated demo system, but every now and then, I have to use my workhorse for demos and content creation, which means turning the date and time on/off. Though it’s a relatively easy task (System Preferences > Date & Time > checkbox), it annoys me because #noclickyclicky! So, I set out to write a function to toggle it on and off and I assumed it would be easy. *laughs*

Apple has made quite a bit of changes to macOS with Sierra (System Integrity Protection being one of them) and I am running High Sierra; a lot of the old hacks I Googled didn’t work for me. So I wrote my own…

I have made modifications to my System Preferences in the past using defaults and thought, “No problem, I’m sure there’s a .plist I can tap into and write a true/false one-liner.” *laughs*

I ran defaults read com.apple.menuextra.clock, but there is no working visibility boolean or string I can access. So my next step was to check out defaults read com.apple.systemuiserver menuExtras, which let me find the ‘Clock.menu’ entry I was searching for; but then simply removing that line from the .plist file did not successfully remove the clock (even with killing the sytemuiserver). So the rabbit hole just continued spiraling and my curious ass couldn’t help but fall deeper and deeper into the abyss.

The first working attempt at a show_clock/no_clock set of function commands for my badass terminal dotfiles was less than ideal for what will be obvious reasons if you choose to test it out:

Not only was the comand slow, I didn’t like that I was now copying multiple menu extra items back and forth with my “toggle.” However, this was leading me somewhere.

I then stumbled upon PlistBuddy and decided to get creative there. I also opted to turn the function into a toggle rather than separating it out:

This was definitely cleaner than my previous attempt, but still slow in actual use. I couldn’t understand why such a simple checkbox was requiring so much code to automate.

Final thought! Abandon the bash shell and use AppleScript like we did back in the 90’s! Going retro with my rabbit hole. I knew if I could get Apple Script to work I would solve the slow issue I created by running killall SystemUIServer.

Well, I got it to work in Script Editor and thought, “No problem, I’ll add it into a var or function and run it as a command.” I use an Apple Script to also get my Spotify information in my tmux so my plan was to just repeat that logic. Wrong. Since I call System Events in this script, I would get the following error indicating I need to allow assistive access.

Now, I set out on this mission so I wouldn’t have to do any manual steps – #noclickyclicky! So I figured, yet again, no problem! I’ll just run a command to allow the necessary access. Again wrong, and here’s where SIP, or System Integrity Protection, comes in. I found this blog post which looked promising – I could simply use sqlite3 or tccutil to make a one-line change (brew install tccutil; sudo tccutil --insert /usr/bin/osascript) to the database! Only – that method stopped working in 10.12 Sierra and still doesn’t work in 10.13 High Sierra due to the addition of SIP. There’s an article here where Dropbox actually had run into this issue too.

There’s an open issue in tccutil’s repo here and from the sound it the issue will be resolved in 10.14 Mojave. Hallelujah! There is a workaround I have not tried in this open issue here, but it involves a series of restarts to enable/disable SIP and I can’t automate that very well since it uses the recovery console.

So, for the time being, my final function looks like this:

And the toggleClock.sh is the previously mentioned applescript. Upon first launch, I do need to allow whatever program (terminal in VS Code or iTerm) is running that function acccessibilty access manually through System Preferences. The good news is I only have to do this ONCE, from then on, the function and code works great.

While it’s not 100% automated (and therefore is still in major alpha for public use of badass terminal), it’s MUCH better than me having to open System Preferences, click Date & Time, check a box, and close a Window. That would take 10 seconds each time, which how often I’ve done it (and will continue to have to do it), who knows how much time I just saved by spending 8+ hours going down this rabbit hole! *sarcasm* Hahaha! But I love it, I love these kinds of hacks and learning more and more about backend under-the-hood shit.

Reminder: All of my posts are provided "AS IS", imply no warranties, and confer no rights or special privileges. Use of included postings, code samples and other works are subject to the terms specified at Microsoft. For more information, click here.

Comments 3

  • Hey Jessica,
    Check out the app “Bartender 3” (macbartender.com)

    It’s handy for recording demos, but also useful if you use a ton of menubar apps – at some point they can take more than 50% of the menubar space, so bartender helps manage that.

    • Thank you! I use bartender 3! Have for years! But I wanted one command that I could toggle one simple built in feature with. I just don’t feel we should have to use a 3rd party tool to manage a built in feature like the clock.

  • Totally agree, and add to that, I wish bartender had multiple configs so we could have one for presenting, one for day to day use, one for laptop only, etc..

    Also have you checked out Alfred? I use it for launching stuff, but it has a whole workflow engine too, I wonder if that shave an extra half second off your invoking of your script…

    Will you be at Ignite?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.