简体   繁体   中英

How do I show the battery percentage on a MacBook Pro by code? (AppleScript, do shell script, etc.)

There is a button called Show Percentage when we click the battery icon, how do I enable the button using code? thx.

Edit:

So, I found a way to do this by UI control, but this is not ideal, anyone?

Go to System Preferences -> Security & Privacy -> Accessibility, check Script Editor.app

tell application "System Events" to tell process "SystemUIServer"
    try
        set bettaryIcon to first menu bar item of menu bar 1 whose description starts with "Battery"
        click bettaryIcon
        click (the first menu item of menu 1 of bettaryIcon whose name = "Show Percentage")
    end try
end tell

You can get the information about the Power state from System Profiler via

do shell script "system_profiler SPPowerDataType

Then you need to extract the line containing the percentage information (I don't have a portable MAC so I can't test it) with awk

do shell script "system_profiler SPPowerDataType | awk '/Hibernate Mode/ {print $NF}'"

Replace Hibernate Mode with the text of the line containing the required information.

pmset -g batt(终端)具有一些有关电池的信息。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM