Battery-triggered Button

Hi,
Is there a way I can make a touch bar button only show when my Mac battery is below a certain percentage? For example, if my battery is below 5%, I want it to show "Warning: 5% Remaining".

Thanks.

Create a new Apple Script Widget, running this code:

set batteryPercent to word 6 of paragraph 2 of (do shell script "pmset -g batt")
if batteryPercent is less than 6 then
return "Warning ⚠️" & "
" & batteryPercent & "% Remaining"
else
return ""
end if

It will appear on your Touch Bar only if the Battery Level drops below 6%, and displays

Warning :warning:
𝑥% Remaining

Hope it helps!

and also, what do I need to change to make this come up when I have 10% remaining?

I tried to test it and it said "Warning: 100% Remaining".
It is just showing my current battery percentage.
how do I make it so it shows only when my laptops on 10%?

also, I want it to stay open until I plug my laptop back in. how do I do that?
ALSO...I use a Touch Bar group for my apps, instead of the app widget. how do I make it so It shows despite the group being open? I've added a "close Touch Bar group", but will that work? and then how can I make it so it opens back up when I plug in my laptop?
thanks heaps (like soooo much)
I also want it to perform haptic feedback on my trackpad.

edit: I've never used javascript or apple script, so im not good at it. treat me like a baby for this :joy:

edit 2: I also want it to say how many minutes until it goes flat. this is already a feature on GoldenChoas, so I tried copy and pasting the coding but it didn't work. how do I do this?

edit 3 (sorry): I alsssoooo wanted to make is cover my entire Touch Bar. i currently have this, but how do I make the red go over the menu bar?

Upsi. I lost somewhere some "" on my way :smiley:

Here is an updated script that will

  • Display only once the battery drops below a certain Level
  • Show in a second line the remaining time until your Laptop dies
  • Disappear if the Laptop is charging, regardless the Battery Percentage

Explanations to customize below the script.
But first of all, create an Apple Script Widget in BTT.

  • In Widget Specific, paste the script below
  • Make it run every x seconds (5, even 10s, as you want)
  • Give it a red background etc, whatever you want :slightly_smiling_face:

Now the script itself:

set powersource to word 4 of paragraph 1 of (do shell script "pmset -g batt")
if powersource is "AC" then
	return ""
else
	set batteryPercent to word 6 of paragraph 2 of (do shell script "pmset -g batt")
	if batteryPercent is less than "11" then
		set remaininghours to word 8 of paragraph 2 of (do shell script "pmset -g batt")
		set remainingminutes to word 9 of paragraph 2 of (do shell script "pmset -g batt")
		return "Warning: " & batteryPercent & "% Remaining. Please charge MacBook." & "
" & remaininghours & "h" & remainingminutes & " 'till Lockdown"
	else
		return ""
	end if
end if

To change the Percentage when the widget appears

In the 6th line, you find "11". The widget will appear if the battery drops below 11, so from 10% on. If you want to have it at 5 only, change "11" to "6". You got it :wink:

To change the "'till lockdown" line

Go to the 5th line from the end (" & remaininghours & "h" & remainingminutes & " 'till Lockdown"). Change the " 'till Lockdown" to whatever you want. Attention to keep the two " and a space after the first "!



Select the widget, go to the "common" on the right, scroll down and hit "Show in any open Touch Bar Group"

Unfortunately, you can’t make the other buttons on the right “hover” about the red background. You would have to change every button to a widget, which would just be battery consuming as hell.

Thanks soooo much!!!!!

just a random question; what is that preset you are using in the video? how can I get it, just to have a look, and possible use it.

also, will this perform haptic feedback on my trackpad? I want it to do the 'Triple Strong Feedback', and make it repeat every 10 seconds until plugged in

thanks for all the help and time u spent towards this :smiley::smiley:
like seriously ur awesome

1 Like

You're welcome.
The preset you see there is a newer version of my PHoeNiX Preset. I have to update it, since I made some major changes, but I didn't find the time yet.


Ok the haptic feedback makes it a bit more complicated. You have to go to the Named & Other Triggers section, make a new Reusable Named Trigger (f.eg "Battery Feedback"), and give it the haptic feedback action. With a right click on the Named Trigger, select "Copy Selected Item UUID".

Go back to the widget. The following has to be pasted between the 6th and 7th line of the script I posted above.

	tell application "BetterTouchTool"
		execute_assigned_actions_for_trigger "PASTE_THE_UUID_HERE"
	end tell

The final result should look like that.

set powersource to word 4 of paragraph 1 of (do shell script "pmset -g batt")
if powersource is "AC" then
	return ""
else
	set batteryPercent to word 6 of paragraph 2 of (do shell script "pmset -g batt")
	if batteryPercent is less than "11" then
    	tell application "BetterTouchTool"
			execute_assigned_actions_for_trigger "PASTE_THE_UUID_HERE"
		end tell
		set remaininghours to word 8 of paragraph 2 of (do shell script "pmset -g batt")
		set remainingminutes to word 9 of paragraph 2 of (do shell script "pmset -g batt")
		return "Warning: " & batteryPercent & "% Remaining. Please charge MacBook." & "
" & remaininghours & "h" & remainingminutes & " 'till Lockdown"
	else
		return ""
	end if
end if

Now set the widget to refresh every 10sec in order to make you trackpad vibrate every 10s.

Et voilà. :slightly_smiling_face: That should be what you desire :smiley:

IT WORKED!!!
Thank you soo much, i think our very long conversation is finally done :slight_smile:
have a nice day or night or whatever it is for you

oh doesn't matter, now its saying this:

I am so sorry for all the questions :joy:
going very off topic of my original question:
I had a look at your Phoenix preset. it isn't for me, but the quick quit thing is a great addition. is there a way you can give me the code so that if I hold 'control' then that will come up? (im using option for window snapper)
thanks

Did you change it to that custom message? :slightly_smiling_face: Nice!

You can navigate to the group (should be the last one before the gestures, last one of the Dock section) and using the menu bar item move it to your preset. (attention there, I recommend to create a new preset where this will be placed in, if you add it to Golden Chaos it will get lost with the next GC update - same thing for the battery btw).
Otherwise it's easy to create it on your own: add an App Switcher widget (or a Dock widget showing only running apps), and add two associated actions:

  1. Delay next action by 0.4
  2. ⌘Q

Otherwise than AQT or GC, my preset isn't really meant to be a stand alone - everyone has to adapt it to his/her own needs. In the next update will come a precise notification counter for app badges, a more reliable Music artwork fetcher, a new AirPods widget, possibility to control the internal microphone, and a new Server and hard Drive widget. I also retouched the code for connecting quickly to your personal Hotspot, to connect to your VPN or you iPad by Sidecar. But all this is just there to help in the Dock groups, the main strip that you see al; the time is for you to put your most used apps, documents, websites etc. It drove me crazy not to have these always right under my fingers :slightly_smiling_face:

In addition, I really recommend haste, there is a shortcut in my Preset for it. It allows to quickly launch web searches directly on your fav website, wether it's DuckDuckGo, Google, Amazon, the Mac App Store etc. It's an amazing tool!

ok, I dunno why, but the coding no longer works. I copy and pasted the exact coding u sent me, without my own editing, but it still doesn't work anymore. do u know why?
thanks

Tested, works fine here.

If you copy paste the scripting the Script Editor Mac, do you get any error message?

what's that?

The Application (found in Utilities folder) "Script Editor". Paste the script there and run it.

nope, I don't, but the result is ""

update: I found out that the script works... only when the battery is 100%. it stops working at 99%.

When the Battery is at 100% the output should be "". Same at 99%. Same until 11. At 10 it should display something. And as soon as the computer is on charge it should return "" again.

this is not happening.
At 100%, my touchbar shows the 'Warning: 100% remaining' thing.
At 99% onwards, there's nothing.

Ok, my PC is far away from 100% RN.

Try this code

set powersource to word 4 of paragraph 1 of (do shell script "pmset -g batt")
if powersource is "AC" then
	return ""
else
	set batteryPercent to word 6 of paragraph 2 of (do shell script "pmset -g batt")
	if batteryPercent is "100" then
		return ""
	else if batteryPercent is less than "11" then
		tell application "BetterTouchTool"
			execute_assigned_actions_for_trigger "PASTE_THE_UUID_HERE"
		end tell
		set remaininghours to word 8 of paragraph 2 of (do shell script "pmset -g batt")
		set remainingminutes to word 9 of paragraph 2 of (do shell script "pmset -g batt")
		return "Warning: " & batteryPercent & "% Remaining. Please charge MacBook." & "
" & remaininghours & "h" & remainingminutes & " 'till Lockdown"
	else
		return ""
	end if
end if