<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: samselfridge</title>
    <description>The latest articles on DEV Community by samselfridge (@samselfridge).</description>
    <link>https://dev.to/samselfridge</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F480764%2F07d7aa46-ba7c-4869-9c78-b559255f7d59.jpeg</url>
      <title>DEV Community: samselfridge</title>
      <link>https://dev.to/samselfridge</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/samselfridge"/>
    <language>en</language>
    <item>
      <title>Magic Mouse Low Battery Alert</title>
      <dc:creator>samselfridge</dc:creator>
      <pubDate>Tue, 14 Sep 2021 00:45:55 +0000</pubDate>
      <link>https://dev.to/samselfridge/magic-mouse-low-battery-alert-4mdo</link>
      <guid>https://dev.to/samselfridge/magic-mouse-low-battery-alert-4mdo</guid>
      <description>&lt;p&gt;One of the worst things about the Apple Magic mouse is the fact that the OS doesn't give you any kind of low battery alert until it hits 2% battery.  Maybe mine is defective, but that's not enough to get through a full work day.  This wouldn't be so bad if not for the worst thing about the magic mouse: &lt;/p&gt;

&lt;p&gt;You can't charge it and use it at the same time....thanks Apple.&lt;/p&gt;

&lt;p&gt;This details a very basic work around.  We're going to add a cron job that runs every 15min that will check the battery level and show a notification if its below a certain threshold.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup your script
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Open a new terminal window&lt;/li&gt;
&lt;li&gt;enter the following commands, I'll explain what they all are later down

&lt;ul&gt;
&lt;li&gt;mkdir bin&lt;/li&gt;
&lt;li&gt;cd bin&lt;/li&gt;
&lt;li&gt;touch mouseCheck.sh&lt;/li&gt;
&lt;li&gt;chmod a+x mouseCheck.sh&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Copy all of this black text&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash 

# Edit cron tab
# env EDITOR=nano crontab -e
# */15 * * * *  ~/bin/mouseCheck.sh
#
#

BATT=`ioreg -c AppleDeviceManagementHIDEventService -r -l | grep -i mouse -A 20 | grep BatteryPercent | cut -d= -f2 | cut -d' ' -f2`
KEY_BATT=`ioreg -c AppleDeviceManagementHIDEventService -r -l | grep -i "Magic Keyboard" -A 20 | grep BatteryPercent | cut -d= -f2 | cut -d' ' -f2`

# defaults to warn at 12%; accepts other number as 1st argument (useful for testing)
COMPARE=${1:-12}
#COMPARE=101

if [ -z "$BATT" ]; then
  echo 'No mouse found.'
  exit 0 
fi

if (( BATT &amp;lt; COMPARE )); then
  osascript -e "display notification \"Mouse battery is at ${BATT}%.\" with title \"Mouse Battery Low\""
fi

if [ -z "$KEY_BATT" ]; then
  echo 'No Keyboard found.'
  exit 0
fi

if (( KEY_BATT &amp;lt; COMPARE )); then
  osascript -e "display notification \"Keyboard battery is at ${KEY_BATT}%.\" with title \"Keyboard Battery Low\""
fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Go back to your terminal and type the following:

&lt;ul&gt;
&lt;li&gt;cat &amp;gt; mouseCheck.sh&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Paste the above CMD + V&lt;/li&gt;
&lt;li&gt;Hit CMD + D to return to terminal prompt.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Set up the cronjob
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Type the following into the same terminal(capitals are important):

&lt;ul&gt;
&lt;li&gt;env EDITOR=nano crontab -e&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You should something that looks like this:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oruOnR7n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nc5re6ggh461tcxw28ez.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oruOnR7n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nc5re6ggh461tcxw28ez.png" alt="Nano Window"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;paste the following into it:&lt;br&gt;
&lt;code&gt;*/15 * * * *  ~/bin/mouseCheck.sh&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hit Ctrl + x&lt;/li&gt;
&lt;li&gt;Hit y&lt;/li&gt;
&lt;li&gt;Hit Enter&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EXszPnkf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hhdsvfysw7uyizgpcqca.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EXszPnkf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hhdsvfysw7uyizgpcqca.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You might get a warning saying 'Terminal wants to administer' your computer, Click Ok.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EXszPnkf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hhdsvfysw7uyizgpcqca.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EXszPnkf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hhdsvfysw7uyizgpcqca.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thats it! If you battery or keyboard get below 12% you should get an alert like this in the top right. I've found 12% is more than enough to get me through 2 days in case I forget after the first one.&lt;/p&gt;

&lt;p&gt;Mine are higher because when writing this article I changed the alert level to get a screen shot.&lt;/p&gt;

&lt;p&gt;Stick around and I'll explain what everything does if you're curious and what you can change on your own.&lt;/p&gt;
&lt;h2&gt;
  
  
  Commands explained:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;mkdir bin&lt;/code&gt; - create a folder called bin.  This is typically where executables go.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;cd bin&lt;/code&gt; - change current directory to be bin&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;touch mouseCheck.sh&lt;/code&gt; - create an empty file called mouseCheck.sh The .sh means this is a shell script, or will be at least.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;cat &amp;gt; mouseCheck.sh&lt;/code&gt; write something to mouseCheck.sh - in this case we don't tell it what to write, so it will prompt us for input afterwards, until it sees the End of File character (EoF)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ctrl + D&lt;/code&gt; send the EoF character to close input.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At this point you can see whats in the file with the command: &lt;code&gt;cat mouseCheck.sh&lt;/code&gt; cat is the basic command to read text files.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;env EDITOR=nano crontab -e&lt;/code&gt; - Open the cron tab for editing.  we need to add the &lt;code&gt;env EDITOR=nano&lt;/code&gt; before hand because it lets us make a temporary change to the ENVIRONMENT VARIABLES just for this command.  It's a good idea to not change things unless we specifically mean to.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you used lower case editor, or left it out you'll end up in the default text editor VIM, which is a very powerful editor but has a very steep learning curve and is famous for not being able to be easily quit.
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tfiCQsYB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ytcvjsprwcyqs7ww563x.png" alt="Alt Text"&gt;
&lt;/li&gt;
&lt;li&gt;If you end up here by accident you can either:

&lt;ul&gt;
&lt;li&gt;Close your terminal and start over (remember to cd bin)&lt;/li&gt;
&lt;li&gt;Attempt to exit vim by:&lt;/li&gt;
&lt;li&gt;Hitting &lt;code&gt;esc&lt;/code&gt; then type &lt;code&gt;:q&lt;/code&gt; and hit enter&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Crontab syntax isn't very complicated but its unique and weird the first time you see it, if you're interested I found this was a good read on it: &lt;a href="https://www.geeksforgeeks.org/crontab-in-linux-with-examples/"&gt;https://www.geeksforgeeks.org/crontab-in-linux-with-examples/&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;*/15 * * * *  ~/bin/mouseCheck.sh&lt;/code&gt; translates to run the script mouseCheck.sh located in the bin directory/folder in my home directory (~) every 15 minutes.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Script Explained
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This tells the program where to find the program to run this script.  In our case we're using the shell &lt;code&gt;bash&lt;/code&gt; thats in the &lt;code&gt;/bin&lt;/code&gt; directory.  This is different from the bin we put our script in.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Edit cron tab
# env EDITOR=nano crontab -e
# */15 * * * *  ~/bin/mouseCheck.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Honestly this isn't needed...the # at the beginning tell bash to ignore these lines, but I leave this here because I can never remember how to get to the cron editor.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;BATT=`ioreg -c AppleDeviceManagementHIDEventService -r -l | grep -i mouse -A 20 | grep BatteryPercent | cut -d= -f2 | cut -d' ' -f2`
KEY_BATT=`ioreg -c AppleDeviceManagementHIDEventService -r -l | grep -i "Magic Keyboard" -A 20 | grep BatteryPercent | cut -d= -f2 | cut -d' ' -f2`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This sets up 2 variables, one for mouse battery and one for keyboard battery by running several commands each.  Each time you see a | its taking the output from one command and using it as the input for the next one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# defaults to warn at 12%; accepts other number as 1st argument (useful for testing)
COMPARE=${1:-12}
#COMPARE=101

if [ -z "$BATT" ]; then
  echo 'No mouse found.'
  exit 0 
fi

if (( BATT &amp;lt; COMPARE )); then
  osascript -e "display notification \"Mouse battery is at ${BATT}%.\" with title \"Mouse Battery Low\""
fi

if [ -z "$KEY_BATT" ]; then
  echo 'No Keyboard found.'
  exit 0
fi

if (( KEY_BATT &amp;lt; COMPARE )); then
  osascript -e "display notification \"Keyboard battery is at ${KEY_BATT}%.\" with title \"Keyboard Battery Low\""
fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I've run out of time, but if you're very interested in the rest of these commands drop a comment and I can fill the rest in.&lt;/p&gt;

&lt;p&gt;Cheers.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
