Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

El Capitan, low battery warning.

I frequently run out of battery because the warning window is in the top corner where I do not notice it. Has anyone found a way to move it center screen and/or make it bigger or more obvious?

Don Bullock

OS X El Capitan (10.11.6), MacBook Pro 5,4

Posted on May 29, 2018 10:35 AM

Reply
17 replies

Jun 1, 2018 11:26 AM in response to Don B

Steps:

  1. Select the scrollable contents of the bash_percent.sh and copy to the clipboard.
    1. BBEdit : New ▸ Text File
    2. BBEdit : Edit menu : Paste ▸ and Match Indentation
      1. You have the battery_percent.sh file open in BBEdit.
      2. I would only recommend selecting a different customBg, if you don't like the default gray alert dialog.
      3. Save battery_percent.sh into your local Library : LaunchAgents folder.
    3. Close document
  2. Select the scrollable contents of the com.local.battery_percent.plist and copy to the clipboard.
    1. BBEdit : File Menu : New ▸ Text Document
    2. BBEdit : Edit menu : Paste ▸ and Match Indentation
      1. Now you have the com.local.battery_percent.plist open
      2. Edits

        Replace viking with your own user name. Leave the 900 alone if you are ok with this running every 15 minutes. The 90 is the percentage of battery left that you want the Bash script to pop a warning dialog about remaining battery strength. I used 90 for testing, but you may want it set to 25.
        User uploaded file

      3. Once you have made the edits, Save com.local.battery_percent.plist into your local Library : LaunchAgents folder.
    3. Close document
    4. Quit BBEdit


Now you have both documents saved to the ~/Library/LaunchAgents folder. In Terminal, you should change directory to that location:


cd ~/Library/LaunchAgents


and make the battery_percent.sh executable:


chmod +x ./battery_percent.sh


Now quit Terminal. From the  menu, select Logout, and then sign back in. The .plist is now active, and will pop a warning alert every 15 minutes (if you left 900 intact) that your current battery level dips below the value you entered in the .plist. To stop the alerts, drag the com.local.battery_percent.plist out of the ~/Library/LaunchAgents folder.

May 31, 2018 5:47 PM in response to Don B

There are two files to the solution, and their source is posted in order.

  • com.local.battery_percent.plist
  • battery_percent.sh


Ultimately, they will be copied into your local Library LaunchAgents (~/Library/LaunchAgents) folder. You must logout/log in to activate the LaunchAgent.


You copy/paste each one into a separate editor session, and save with the above names. Before you save the .plist, replace my username with your own, and full paths are required. Edit the integer that is the lowest battery percentage that you will allow before receiving low battery prompts. If you don't want it running every 15 minutes, change the StartInverval integer value per the comment.


The only change that is necessary to the Bash script is possibly that you do not want a dull grey background, and I have provided other customBg color values if needed.


The following are scrollable panels, so click at the top, and then drag downward to allow selection of the entire code. The code will not display correctly while signed out of the community.


Source for com.local.battery_percent.plist

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Disabled</key> <false/> <key>RunAtLoad</key> <true/> <key>KeepAlive</key> <false/> <key>Label</key> <string>com.local.battery_percent.plist</string> <!-- 3600 hourly, 1800 every 30 min, 900 every 15 min --> <key>StartInterval</key> <integer>900</integer> <key>Program</key> <string>/Users/viking/Library/LaunchAgents/battery_percent.sh</string> <key>ProgramArguments</key> <array> <string>/Users/viking/Library/LaunchAgents/battery_percent.sh</string> <!-- minimum battery percentage to start warnings this value will be passed as argument to program --> <string>90</string> </array> </dict> </plist>


Source for battery_percent.sh

#!/bin/bash # calculate the percentage of remaining battery. # tested with High Sierra 10.13.4 on a 2014 MacBook Air BATTERY_INSTALLED= # returns "Battery Power" or "AC Power" CHARGING_STATUS="$(pmset -g ps | egrep -iow '(Battery Power|AC Power)')" # the LaunchAgent will pass this value into the script when it runs BATTERY_MIN=$1 battery_warn () { /usr/bin/python <<'EOF' - "$1" from Foundation import NSString, NSArray, NSMakeRect from AppKit import NSColor, NSAlert, NSStatusWindowLevel, NSFont, NSView import ast import sys # because it is an enumerator in Objective-C NSAlertStyleCritical = 2 if not len(sys.argv) == 2: sys.exit('Wrong number of arguments') # pass the numeric string in as an unmolested integer bat_remain = ast.literal_eval(sys.argv[1]) # make a custom blue background color # customBg = NSColor.colorWithDeviceRed_green_blue_alpha_(0.152, 0.671, 0.966, 1.0) # customBg = NSColor.yellowColor() # customBg = NSColor.redColor() customBg = NSColor.systemGrayColor() # start with conservative dialog alert = NSAlert.alloc().init() alert.window().setTitle_("Battery Charge Monitor") alert.window().setLevel_(NSStatusWindowLevel) alert.window().setBackgroundColor_(customBg) alert.setAlertStyle_(NSAlertStyleCritical) msg_font = NSFont.boldSystemFontOfSize_(18.0) inform_font = NSFont.boldSystemFontOfSize_(18.0) bat_text = NSString.alloc().init() bat_text = "Battery Remaining: {} Percent".format(bat_remain) inform_text = NSString.alloc().initWithString_("Time to Recharge") # now override the message and informative text fields of the alert panel view = NSArray.array() view = alert.window().contentView().subviews() view[4].setFont_(msg_font) view[4].setStringValue_(bat_text) view[5].setFont_(inform_font) view[5].setStringValue_(inform_text) # make the alert window slightly wider than default accview = NSView.alloc().initWithFrame_(NSMakeRect(0, 0, 350, 0)) alert.setAccessoryView_(accview) alert.runModal() EOF } # returns Yes or No BATTERY_INSTALLED="$(ioreg -l -k "AppleSmartBattery" | \ egrep -io '("BatteryInstalled") = \w+' | awk '{print $3}')" [[ $BATTERY_INSTALLED = "" ]] || [[ $BATTERY_INSTALLED = "No" ]] && \ echo "No battery Installed." && exit 1 # only produce warning dialogs when on battery power [[ $CHARGING_STATUS = "AC Power" ]] && exit 1 # remaining battery percentage using Apple's calculations BATTERY_PERCENT=$(pmset -g ps | perl -lne '$_ =~ /(\d+)(?=%;)/s && print $1;') [[ $BATTERY_PERCENT > $BATTERY_MIN ]] && exit 0 battery_warn "${BATTERY_PERCENT}" exit 0

May 30, 2018 7:31 PM in response to Don B

Without code, you can click on the battery indicator in the menubar, and select Show percentage. This may not be easily seen with large high-resolution monitors.


I have a working solution tested with High Sierra, but it should be ok with El Capitan. It gets your remaining battery charge every 15 minutes (time configurable) and pops an alert dialog center screen with the remaining charge percent. As long as it is above the minimum percentage (configurable) then no alert appears.


Here is the alert dialog. I have the Mac on the charger, and had to set a higher than normal minimum percentage to get the dialog output.

User uploaded file


I will post the solution in the morning.

Jun 1, 2018 10:39 AM in response to VikingOSX

I loaded BBedit and pasted the "com.local.battery_percent.plist" file. I noted the time logic based on 3600 secs and your integer set to 900 = 15 mins. Further on, I see "<string>90</string>", should this be 900?

Having loaded it, do I just run it? I see that option on a drop down window. I assume the same for the second program. Pasting that quote in the first line caused a font change and I could not find any way to correct it.

Thank you so much for doing this little program, one of the nice things about being in the Mac world is meeting good people like you.

May 31, 2018 6:34 PM in response to VikingOSX

Errata.


The hosting software is very unforgiving when posting code that it does not understand how to parse correctly, and as a result, it strips linefeeds, or trashes entire posted code. That is what happened above with the .plist file.


Here it is again, reposted.

Source for com.local.battery_percent.plist

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Disabled</key> <false/> <key>RunAtLoad</key> <true/> <key>KeepAlive</key> <false/> <key>Label</key> <string>com.local.battery_percent.plist</string> <!-- 3600 hourly, 1800 every 30 min, 900 every 15 min --> <key>StartInterval</key> <integer>900</integer> <key>Program</key> <string>/Users/viking/Library/LaunchAgents/battery_percent.sh</string> <key>ProgramArguments</key> <array> <string>/Users/viking/Library/LaunchAgents/battery_percent.sh</string> <!-- minimum battery percentage to start warnings this value will be passed as argument to program --> <string>90</string> </array> </dict> </plist>


I had to replace all of the xml syntax with the HTML code equivalents to get it to post correctly.

May 31, 2018 2:22 PM in response to Don B

It is done. Running some final testing now, before I post it. Had to add some logic so that it would not present a dialog when you were on charger.


It will be posted here.


It would be a good idea to have a programmer's editor handy to paste into as TextEdit, or a word processing application could mess with the code formatting. The free BBEdit editor is a good choice, unless you are already using something else.

Jun 2, 2018 8:31 PM in response to VikingOSX

I successfully loaded both programs into LaunchAgents and I am waiting for my battery to get discharged to try it out.

I opened both programs from inside LaunchAgent and printed them so I could check them very carefully against your original code. They were fine, but as I read the code (which I kind of understand) a couple of questions came up.

1. I found I had only changed one "viking"; did not notice the second one.

2. I realized I did not know what is the name I should have substituted, I used DonaldBullock.

When I open my drive to find LaunchAgents , it is at Macintosh HD/Library/LaunchAgents. Should I have used "Macintosh HD" to substitute "viking"

3. I did not change the color from gray because I did not know how to do it.

4. I suppose I can fix all these errors by opening the files in BBedit, editing and resaving?


Thanks, Don Bullock

Jun 3, 2018 6:14 AM in response to Don B

Don,


The instructions were to place the two files within your home directory's Library LaunchAgents folder. You get there by copy/pasting the following into the Go To Folder panel that you invoke in the Finder with the shift+command+G keyboard shortcut:


~/Library/LaunchAgents


Move the two files from the incorrect /System/Library/LaunchAgents location to the above, local account location. The tilde (~) is UNIX shorthand for home directory.


Launch Terminal, and at the command prompt, type the following and a return:


id -un


This returns your short user name that would replace both instances of the original viking in the com.local.battery_percent.plist file — which you would edit with BBEdit.


In the bash_percent.sh file, I use the variable customBg to signify the color of the alert dialog background. In Python terminology, a preceding '#' character followed by a space is a comment, so the customBg line that has no preceding '#' character is the active background. You would simply insert the '# ' characters in front of the active customBG variable, and remove the '#' character from the beginning of a different customBg variable to try that alert panel background color.


Once you have the above done, logout, and sign back in. If there are no issues with the files, the .plist will fire immediately and run the script. You will have no knowledge of this until your battery remaining drops below the value you set in the .plist file — at which time you will receive an alert dialog.

Jun 3, 2018 10:38 AM in response to VikingOSX

I did everything as you instructed and I just got my first alert. I changed it to RED and it really stands out.🙂 I would like to thank you for having so much patience with me. What is straightforward to you is a bit challenging for an 89 year old!

I would think you could promote this little program, it is so much better than Apples built-in warning.

Jul 8, 2018 5:52 PM in response to VikingOSX

Just wanted to tell anyone interested in this nice little program that it works beautifully and I just love it. My years old MacBookPro is showing signs of an aging battery and does not last all day on a charge. Since I installed it I have never been caught with a dead battery. It comes up with a big bright message in the center of the screen, impossible to miss. I can not think why Apple did not do something like this in the first place. Many thanks to my mentor who wrote it for me.

Don Bullock

El Capitan, low battery warning.

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.