ajbabcock

Q: How do you check battery level using a speakable command in OSX?

I am trying to assist a student of ours at my school who is using a MacBook Air as part of our one-to-one initiative.  In trying to help her get all of her Accessibility features working for her, we ran into a stumper: How can you get a MacBook Air to tell a user (who is completely blind) what the status of his/her battery is?  VoiceOver does not pick it up from the Menu Bar.  I don't see a Speakable Command for it.  We cannot find the status or another menu that has speakable text to tell the user about the percentage or hours remaining... this is very frustrating for a student who is used to simply asking Siri to tell her the status of her battery!

 

In trying to find a solution, I thought of creating our own speakable command (through Automator and then creating the command), but, again, I just can't find the clicks to get us a percentage or hours that can be read.  I hope Apple reps take note that my previous praise of OSX as a more accessible platform has a very serious hole in it when the user cannot tell when his/her device is about to die.

 

Sidebar:

When on earth will Apple embed Siri into OSX instead of relying on its library of speakable commands (which falls short in this case!)?

MacBook Air, Mac OS X (10.7.4)

Posted on Dec 4, 2013 8:46 AM

Close

Q: How do you check battery level using a speakable command in OSX?

  • All replies
  • Helpful answers

  • by VikingOSX,

    VikingOSX VikingOSX Dec 4, 2013 1:22 PM in response to ajbabcock
    Level 7 (21,535 points)
    Mac OS X
    Dec 4, 2013 1:22 PM in response to ajbabcock

    Well, here is the AppleScript:

     

     

    do shell script "pmset -g batt | pbcopy"
    say (get the clipboard)
    
    
  • by mns579,

    mns579 mns579 Dec 4, 2013 2:47 PM in response to VikingOSX
    Level 4 (1,155 points)
    Dec 4, 2013 2:47 PM in response to VikingOSX

    . . and the rest . . .

     

    Make a small application with Automator. Just one action: Run AppleScript. Use VikingOSX's script to replace the entire sample script in the action's window. Save. Let's call it "battery level." Drop this application in the Applications folder.

     

    Activate Speakable Items. Select/highlight the new "battery level" application. Tell Speakable Items to "make this speakable."

     

    Test: Ask Speakable Items for "battery level."

  • by VikingOSX,

    VikingOSX VikingOSX Dec 4, 2013 5:20 PM in response to VikingOSX
    Level 7 (21,535 points)
    Mac OS X
    Dec 4, 2013 5:20 PM in response to VikingOSX

    Updated AppleScript that now works.

    on run
              try
                   tell application "System Events"
                             do shell script "pmset -g batt | pbcopy"
                   end tell
                   tell application "SpeechRecognitionServer"
                          say (the clipboard)
                   end tell
              end try
    end run
    
    

     

    Press shift+command+U and launch the AppleScript Editor. Copy the above code into the AppleScript editor and press Run. Save as a Script (with extension hidden) to login directory/Library/Speech/Speakable Items. I called mine “What battery,” but you can be more creative if you choose.

     

    I found that I had to pronounce the above speakable item several times with varying loudness and speed before it actually worked. If the Mac is connected to AC power, it will tell you that, and if on battery power it will tell you that too, as well as percentage of battery consumed and remaining time left on charge.

  • by Barney-15E,

    Barney-15E Barney-15E Dec 4, 2013 7:11 PM in response to VikingOSX
    Level 9 (50,793 points)
    Mac OS X
    Dec 4, 2013 7:11 PM in response to VikingOSX

    Why not just:

    say (do shell script "pmset -g batt")

  • by VikingOSX,

    VikingOSX VikingOSX Dec 5, 2013 5:28 AM in response to Barney-15E
    Level 7 (21,535 points)
    Mac OS X
    Dec 5, 2013 5:28 AM in response to Barney-15E

    Thanks Barney. AppleScript is not my first language and it shows. I am however, all for simplicity.

     

    The following revised script, that I renamed “Battery level,” is tested and works.

     

    on run
         try
           say (do shell script "pmset -g batt")
         end try
    end run
    
    
  • by VikingOSX,Solvedanswer

    VikingOSX VikingOSX Dec 6, 2013 6:21 PM in response to ajbabcock
    Level 7 (21,535 points)
    Mac OS X
    Dec 6, 2013 6:21 PM in response to ajbabcock

    Here is a fully functional example of a Speakable Items battery level checker. It determines if you are on a desktop, or laptop. In the latter case, it speaks to you differently if you have a power cord attached, or are strictly on battery power.

     

    You will need to enable Speakable Items in System Preferences > Accessibility (Mavericks), or in the Speech panel (Lion). Haven't tested this on Mountain Lion. In either case, a new Speech folder will be created in your local Library folder.

     

    Copy and paste the following code into the AppleScript Editor, initially save it as text (battery-level.applescript) for safe-keeping, and then with the option key pressed, choose File > Save As... and save it as a script (with no file extension) into your login directory/Library/Speech/Speakable Items folder.

     

    (* 
        Speakable items addition that recites current battery info.
        Open in AppleScript Editor and Save As… script without extension
        into login directory/Library/Speech/Speakable Items folder.
       
        Name suggestion: Battery level.
    
        Works exactly as coded. Though intended for visually challenged, it will
        output a text banner containing the speech content if audio is off.
    
        Tested on OS X 10.7.5 and 10.9
        VikingOSX, December 2013, Apple Support Community
     *)
    
    set theList to {}
    set thePct to ""
    set theHr to ""
    set theMin to ""
    set status to ""
    set expression to ""
    
    set theList to paragraphs of text of (do shell script "pmset -g batt")
    set itemCnt to count of items in theList
    
    if (itemCnt = 1) then
              say "Desktop Device not equipped with battery."
              return
    end if
    
    set thePct to word 3 of theList's item 2
    set theHr to word 5 of theList's item 2
    set theMin to word 6 of theList's item 2
    
    --trim leading zero
    if (theMin begins with "0") then set theMin to text 2 thru -1 of theMin
    
    if theList's item 1 contains "AC Power" and ¬
              theList's item 2 contains "; charging" then
              set status to "power cord"
              if (theHr is "0") then
                        set expression to "Currently on " & status & ¬
                                  " with the battery at " & thePct & ¬
                                  " percent, and  " & theMin & " minutes to full charge."
      
              else if (theHr is "1") then
                        set expression to "Currently on " & status & ¬
                                  " with the battery at " & thePct & ¬
                                  "percent , and  " & theHr & ¬
                                  " hour, and " & theMin & " minutes to full charge."
              else
                        set expression to "Currently on " & status & ¬
                                  " with the battery at " & thePct & ¬
                                  "percent , and  " & theHr & ¬
                                  " hours, and " & theMin & " minutes to full charge."
              end if
      
    else
              set status to "battery power"
              if (theHr is "0") then
                        set expression to "Currently on " & status & ¬
                                  " with " & thePct & ¬
                                  " percent charge, and  " & theMin ¬
                                  & " minutes remaining."
              else if (theHr is "1") then
                        set expression to "Currently on " & status & ¬
                                  " with " & thePct & ¬
                                  "percent charge, and  " & theHr & ¬
                                  " hour, and " & theMin & " minutes remaining."
              else
                        set expression to "Currently on " & status & ¬
                                  " with " & thePct & ¬
                                  "percent charge, and  " & theHr & ¬
                                  " hours, and " & theMin & " minutes remaining."
              end if
      
    end if
    
    say expression
    
    
  • by VikingOSX,

    VikingOSX VikingOSX Dec 10, 2013 9:26 AM in response to ajbabcock
    Level 7 (21,535 points)
    Mac OS X
    Dec 10, 2013 9:26 AM in response to ajbabcock

    Ok. Have included additional tests including those for when pmset changes its output under certain conditions and impairs proper hour and minute settings. Also, I have eliminated conditional code redundancy from the previous version.

    (* 
        Speakable items addition that recites current battery info.
        Open in AppleScript Editor and Save As… script without extension
        into login directory/Library/Speech/Speakable Items folder.
       
        Name suggestion: Battery level.
    
        Works exactly as coded. Though intended for visually challenged, it will
        output a text banner containing the speech content if audio is off.
    
        Tested on OS X 10.7.5 and 10.9
        VikingOSX, December 2013, Apple Support Community
       Version 1.2
     *)
    
    set theList to {}
    set thePct to ""
    set theHr to ""
    set theMin to ""
    set expression to ""
    
    set theList to paragraphs of text of (do shell script "pmset -g batt")
    set itemCnt to count of items in theList
    
    if (itemCnt = 1) then
              say "Desktop Device not equipped with battery."
              return
    end if
    
    set thePct to word 3 of theList's item 2
    if theList's item 2 contains "finishing charge" then
              set theHr to word 6 of theList's item 2
              set theMin to word 7 of theList's item 2
    else
              set theHr to word 5 of theList's item 2
              set theMin to word 6 of theList's item 2
    end if
    --display dialog thePct & return & theHr & return & theMin
    
    --trim leading zero
    if (theMin begins with "0") then set theMin to text 2 thru -1 of theMin
    
    if theList's item 1 contains "AC Power" then
              set powerStatus to "power cord"
              set chargeStatus to "full charge."
    else
              set powerStatus to "battery power"
              set chargeStatus to "battery exhaustion."
    end if
    
    if (thePct is greater than or equal to "99") then
              set expression to "Currently on " & powerStatus & ¬
                        " at full charge."
      
    else if (theHr is "0") and (theMin is not "0") then
              set expression to "Currently on " & powerStatus & ¬
                        " with the battery at " & thePct & ¬
                        " percent, and  " & theMin & " minutes to " & chargeStatus
      
    else if (theHr is "1") and (theMin is greater than or equal to "0") then
              set expression to "Currently on " & powerStatus & ¬
                        " with the battery at " & thePct & ¬
                        "percent , and  " & theHr & ¬
                        " hour, and " & theMin & " minutes to " & chargeStatus
    else
              set expression to "Currently on " & powerStatus & ¬
                        " with the battery at " & thePct & ¬
                        "percent , and  " & theHr & ¬
                        " hours, and " & theMin & " minutes to " & chargeStatus
    end if
    
    say expression
    
    
  • by sheaj13,

    sheaj13 sheaj13 Oct 19, 2016 6:50 PM in response to ajbabcock
    Level 1 (4 points)
    Oct 19, 2016 6:50 PM in response to ajbabcock

    Funny reading this 3 years later we finally have Siri

  • by sheaj13,

    sheaj13 sheaj13 Oct 19, 2016 6:51 PM in response to ajbabcock
    Level 1 (4 points)
    Oct 19, 2016 6:51 PM in response to ajbabcock

    I have a case for the actual power supply

     

    I want the computer (using apple script i guess) to tell one of those iPhone controllable power points to

    turn off when battery at 80 % and turn on at 40%

  • by sheaj13,

    sheaj13 sheaj13 Oct 19, 2016 6:56 PM in response to ajbabcock
    Level 1 (4 points)
    Oct 19, 2016 6:56 PM in response to ajbabcock

    Im posting this here as it may be helpful with your student

    My current guess is something like IFTT could help with some apple script