1

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

2
  • 1
    Perhaps "Hide/Show Battery Icon in Menu Bar with Applescript" is a more fitting title. Commented Oct 18, 2014 at 7:27
  • @fartheraway thanks! I was wondering what’s wrong with my question title. 😁
    – sayzlim
    Commented Oct 18, 2014 at 7:29

3 Answers 3

2

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.
2
  • This is exactly what I’m looking for (despite that I prefer it to run in background without opening System Preferences). Is there any way to get the value of checkbox to see if it’s enabled/disabled?
    – sayzlim
    Commented Oct 18, 2014 at 8:46
  • Never mind, I’ve found the answer on StackOverflow on this thread.
    – sayzlim
    Commented Oct 18, 2014 at 9:21
1

The question is vague. Do you mean an applescript to display the dropdowm menu from the battery icon on the right hand side of menu bar?

tell application "System Events" to tell process "SystemUIServer"
    click (first menu bar item whose value of attribute "AXDescription" contains "Battery") of menu bar 1
end tell
4
  • No. Just want to toggle the battery state from the Energy Saver setting.
    – sayzlim
    Commented Oct 18, 2014 at 7:14
  • It can be done through accessibility. In other words, you will see system pref flash through your screen. And then the setting is changed. I'm too spread out to write out the code now. Commented Oct 18, 2014 at 7:25
  • I’ve opened Accessibility but unable to find the setting you mentioned. Can it still be done with AppleScript (I’m also welcome idea using shell script.
    – sayzlim
    Commented Oct 18, 2014 at 7:28
  • It's not a setting. The Accessibility Panel in SysPref simply grant/deny permission for apps to take over you mouse and keyboard, eg accessibility. Applescript can intact with UI. Shell cannot. Look for Accessibility Inspector in Xcode if you want to code. Commented Oct 18, 2014 at 7:33
1
tell application "System Preferences"
    activate
    set current pane to pane "com.apple.preference.energysaver"
end tell

tell application "System Events" to set bool to value of attribute "AXValue" of checkbox 1 of window 1 of process "System Preferences"

tell application "System Preferences"
    quit
end tell

if bool is 1 then
    display notification "Battery icon is visible" with title "Put a title here" subtitle "Subtitle here"
else
    display notification "Battery icon is hidden" with title "Put a title here" subtitle "Subtitle here"
end if

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .