How can I hide/show the Wi-Fi menu item from the Terminal in OS X?

9,311

A big difficulty here is that the menuExtras key in com.apple.systemuiserver is an array that's reordered when you drag your Menu Extras around in the menu bar (and add and remove them).

In your case cited above, the Wi-Fi Menu Extra is the second item (which is array index 1 because these arrays are zero-indexed like C arrays), so you could remove it with PlistBuddy, which isn't in your $PATH because it's hidden in /usr/libexec. You have to use PlistBuddy because the defaults command doesn't have syntax for specifying an array index. Then kill SystemUIServer to get it to reload:

/usr/libexec/PlistBuddy -c 'Delete :menuExtras:1' ~/Library/Preferences/com.apple.systemuiserver.plist
killall SystemUIServer

To add it back in, do something like this:

/usr/libexec/PlistBuddy -c 'Add :menuExtras:1 string "/System/Library/CoreServices/Menu Extras/AirPort.menu"' ~/Library/Preferences/com.apple.systemuiserver.plist
killall SystemUIServer

Note that this will break if you ever remove or add Menu Extras such that the Wi-Fi Menu Extra is not the second Menu Extra from the left in your menu bar. With a little more scripting work you could walk the menuExtras array looking for the AirPort (Wi-Fi) menu extra, note its index, then feed that into your PlistBuddy command.

Share:
9,311

Related videos on Youtube

ggustafsson
Author by

ggustafsson

Updated on September 18, 2022

Comments

  • ggustafsson
    ggustafsson almost 2 years

    I want to be able to hide and show the Wi-Fi menu item from the Terminal. Can I do this with the defaults command?

    I've narrowed it down to com.apple.systemuiserver. It looks like this:

    {
        "NSWindow Frame NoTimeLeft" = "471 437 475 163 0 0 1366 746 ";
        "__NSEnableTSMDocumentWindowLevel" = 1;
        menuExtras =     (
            "/System/Library/CoreServices/Menu Extras/Script Menu.menu",
            "/System/Library/CoreServices/Menu Extras/AirPort.menu",
            "/System/Library/CoreServices/Menu Extras/TimeMachine.menu",
            "/System/Library/CoreServices/Menu Extras/Battery.menu",
            "/System/Library/CoreServices/Menu Extras/Clock.menu"
        );
    }
    

    Preferences Screenshot

  • ggustafsson
    ggustafsson over 12 years
    This does edit com.apple.systemuiserver.plist correctly but airport is still there after killing SystemUIserver. It just moves to the left. This is odd.
  • Spiff
    Spiff over 12 years
    @ggustafsson Huh, it's working for me on Mac OS X Lion v10.7.3. You didn't do both the Delete and Add at the same time, did you? You're only supposed to do the Delete when you want to hide it, and the Add when you want to show it.
  • ggustafsson
    ggustafsson over 12 years
    It works half of the time. I can't find a pattern here. I'll try it out for a while and see what happens. There's a typo btw, it's should be "killall SystemUIServer" in the add box.