Search code examples
macoscocoabattery

Cocoa : Displaying battery percentage in the menu bar


In terminal the following command enables the battery percentage in the menu bar :

defaults write com.apple.menuextra.battery ShowPercent -string "YES"

How should this be done programatically?


Solution

  • You can run any program (such as defaults) using the NSTask class.

    Alternatively you could search the plist file in ~/Libraries/Preferences, read it into an NSMutableDictionary, change the ShowPercent value and write it back.