šŸ”‹ Laptop Battery Checker by Nick #SummerSideProjects #1 #2021

Nick Wu
2 min readJun 19, 2021

--

Welcome to Nickā€™s first #SummerSideProject, today we have the highly anticipated background macbook laptop battery checker in 55 lines of code šŸ™Œ. In the future, we hope to see more come to life šŸŒ…, or for him to bring back some of his old ones. šŸ˜†

https://www.reddit.com/r/ProgrammerHumor/comments/j2oj5e/cool_side_project/

Whatā€™s the problem?

My macbook is often in the ā€œchargingā€ state but is actually not charging,
and sometimes I just forget to plug in my charger šŸ¤¦. This causes my laptop to run out of battery while Iā€™m playing video games such as League of Legends or Steam games šŸ˜­. Unfortunately this has happened multiple timesā€¦

How do we solve this?

Of course, the answer is letā€™s code something to solve this. So weā€™ve created a script thatā€™s always monitoring the battery level since we definitely canā€™t trust the charging symbol in the macā€™s user interface. Weā€™ll make my laptop say ā€œbattery is drainingā€ if the battery is not increasing, and ā€œcharger is not plugged inā€ if I didnā€™t even plug in my charger.

After coding this up, we just need to run one command to make sure that our battery is always being checked when we are gaming:

nohup ./batteryd.py -a Steam|LeagueClient >/dev/null 2>&1 &

Whereā€™s your code?

We had to google to figure out how to:

  1. Get the battery stats from the computer, and determine if the battery is charging or not,
  2. Determine if an application is running or not because I only want to this script to check the battery if Iā€™m gaming,
  3. Get the laptop to talk to us.

Code is at https://github.com/nickwu241/battery-checker or see the gist below:

Whatā€™s next?

Next up on #SummerSideProjects is a surprise so subscribe to Nick Codes on YouTube or follow nickwu241 on Github to be the first to the fun šŸ˜‰ #newyearnewnick

--

--

Nick Wu
Nick Wu

Written by Nick Wu

Software Engineer @Affirm. Previously @Shopify, @Hootsuite.

No responses yet