Hide/Show Battery Icon in Menu Bar with AppleScript

applescriptmenu bar

Is there any method to display the battery menu bar status item with AppleScript? I’ve tried to browse the script library but unable to find one that matches this capability.

I simply want to toggle the state of menu bar item found in System Preferences » Energy Saver » Power Adapter setting. There is no need to build an AppleScript application for it.

OS X Yosemite » Energy Saver » Power Adapter

Best Answer

Update: This updated script wouldn't flash through the screen, i.e runs in the backgound. The old version is still valid.

tell application "System Events"
    tell process "System Preferences"
        activate
    end tell
    tell application "System Preferences"
        set current pane to pane "com.apple.preference.energysaver"
    end tell
    tell process "System Preferences"
        click checkbox 1 of window 1
    end tell
    tell application "System Preferences"
        quit
    end tell
end tell

From Oct. 18th:

-- Allow Script Editor in Accessibility <- Privacy <- Security & Privacy <-  System Preferences. For testing, at the very least.

tell application "System Preferences"
    activate
    set current pane to pane "com.apple.preference.energysaver"
end tell


tell application "System Events"
    tell process "System Preferences"
        tell window 1
            click checkbox 1
        end tell
    end tell
end tell

tell application "System Preferences"
    quit
end tell

-- Create an Automator Service with this, so that you can bind the action to a keyboard shortcut.