# Battery-triggered Button

**URL:** <https://community.folivora.ai/t/battery-triggered-button/16321>\
**Category:** Configuration Help & General Discussion\
**Created:** [May 20, 2020, 3:38am UTC](https://community.folivora.ai/t/battery-triggered-button/16321 "2020-05-20T03:38:32Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Future\_Face](https://community.folivora.ai/user_avatar/community.folivora.ai/future_face/32/13410_2.png) [@Future\_Face](https://community.folivora.ai/u/Future_Face)\
**Post date:** [May 20, 2020, 3:38am UTC](https://community.folivora.ai/t/battery-triggered-button/16321/1 "2020-05-20T03:38:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Caliguvara](https://community.folivora.ai/user_avatar/community.folivora.ai/caliguvara/32/42570_2.png) [@Caliguvara](https://community.folivora.ai/u/Caliguvara)\
**Post date:** [May 20, 2020, 8:03am UTC](https://community.folivora.ai/t/battery-triggered-button/16321/2 "2020-05-20T08:03:22Z")

</div>

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 ⚠  
> _𝑥_% Remaining

Hope it helps!

---

<div class="post-metadata">

**Author:** ![Future\_Face](https://community.folivora.ai/user_avatar/community.folivora.ai/future_face/32/13410_2.png) [@Future\_Face](https://community.folivora.ai/u/Future_Face)\
**Post date:** [May 21, 2020, 11:17pm UTC](https://community.folivora.ai/t/battery-triggered-button/16321/4 "2020-05-21T23:17:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Future\_Face](https://community.folivora.ai/user_avatar/community.folivora.ai/future_face/32/13410_2.png) [@Future\_Face](https://community.folivora.ai/u/Future_Face)\
**Post date:** [May 21, 2020, 11:26pm UTC](https://community.folivora.ai/t/battery-triggered-button/16321/5 "2020-05-21T23:26:15Z")

</div>

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.

 ![btt scnsht](https://community.folivora.ai/uploads/default/original/2X/d/d9f1b06da87e7af77f56bce8143a1ddf948ad1f5.jpeg) ![btt scnsht 2](https://community.folivora.ai/uploads/default/original/2X/3/3ff853fd84d37855df4d312d3b7a8bddc90e5e00.png)

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

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?

 ![touchbar](https://community.folivora.ai/uploads/default/original/2X/e/e3adab667e48d0bb62f6d3bd5e1a2341cbc3ee9a.png)

---

<div class="post-metadata">

**Author:** ![Caliguvara](https://community.folivora.ai/user_avatar/community.folivora.ai/caliguvara/32/42570_2.png) [@Caliguvara](https://community.folivora.ai/u/Caliguvara)\
**Post date:** [May 22, 2020, 8:01am UTC](https://community.folivora.ai/t/battery-triggered-button/16321/6 "2020-05-22T08:01:56Z")

</div>

Upsi. I lost somewhere some "" on my way 😃

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 🙂

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 😉

## 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 "!

* * *

* * *

> [@Future\_Face](#):
>
> how do I make it so It shows despite the group being open?

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

> [@Future\_Face](#):
>
> 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?

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.

---

<div class="post-metadata">

**Author:** ![Future\_Face](https://community.folivora.ai/user_avatar/community.folivora.ai/future_face/32/13410_2.png) [@Future\_Face](https://community.folivora.ai/u/Future_Face)\
**Post date:** [May 22, 2020, 10:42am UTC](https://community.folivora.ai/t/battery-triggered-button/16321/7 "2020-05-22T10:42:40Z")

</div>

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 😃😃  
like seriously ur awesome

---

<div class="post-metadata">

**Author:** ![Caliguvara](https://community.folivora.ai/user_avatar/community.folivora.ai/caliguvara/32/42570_2.png) [@Caliguvara](https://community.folivora.ai/u/Caliguvara)\
**Post date:** [May 22, 2020, 1:28pm UTC](https://community.folivora.ai/t/battery-triggered-button/16321/8 "2020-05-22T13:28:28Z")

</div>

You're welcome.  
The preset you see there is a newer version of my [PHoeNiX](https://community.folivora.ai/t/phoenix-preset-c3-update-for-catalina-including-a-working-airpods-widget/12075) 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.

```auto
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à_. 🙂 That should be what you desire 😃

---

<div class="post-metadata">

**Author:** ![Future\_Face](https://community.folivora.ai/user_avatar/community.folivora.ai/future_face/32/13410_2.png) [@Future\_Face](https://community.folivora.ai/u/Future_Face)\
**Post date:** [May 23, 2020, 1:56am UTC](https://community.folivora.ai/t/battery-triggered-button/16321/9 "2020-05-23T01:56:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Future\_Face](https://community.folivora.ai/user_avatar/community.folivora.ai/future_face/32/13410_2.png) [@Future\_Face](https://community.folivora.ai/u/Future_Face)\
**Post date:** [May 23, 2020, 2:03am UTC](https://community.folivora.ai/t/battery-triggered-button/16321/10 "2020-05-23T02:03:44Z")

</div>

oh doesn't matter, now its saying this:

 ![touchbar](https://community.folivora.ai/uploads/default/original/2X/9/9e126b6b05b22a9b0b2079cdfb0af01e7a7580ed.png)

---

<div class="post-metadata">

**Author:** ![Future\_Face](https://community.folivora.ai/user_avatar/community.folivora.ai/future_face/32/13410_2.png) [@Future\_Face](https://community.folivora.ai/u/Future_Face)\
**Post date:** [May 23, 2020, 2:51am UTC](https://community.folivora.ai/t/battery-triggered-button/16321/11 "2020-05-23T02:51:59Z")

</div>

I am so sorry for all the questions 😂  
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

---

<div class="post-metadata">

**Author:** ![Caliguvara](https://community.folivora.ai/user_avatar/community.folivora.ai/caliguvara/32/42570_2.png) [@Caliguvara](https://community.folivora.ai/u/Caliguvara)\
**Post date:** [May 23, 2020, 7:49am UTC](https://community.folivora.ai/t/battery-triggered-button/16321/12 "2020-05-23T07:49:31Z")

</div>

> [@Future\_Face](#):
>
> oh doesn't matter, now its saying this:
> 
> ![touchbar](https://community.folivora.ai/uploads/default/original/2X/9/9e126b6b05b22a9b0b2079cdfb0af01e7a7580ed.png)

Did you change it to that custom message? 🙂 Nice!

> [@Future\_Face](#):
>
> 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)

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 🙂

 ![Touch Bar Shot 2020-05-23 at 10.43.46](https://community.folivora.ai/uploads/default/original/2X/c/c820748d26c556677ebeb0a6e7b3517946b2e90b.png) ![Touch Bar Shot 2020-05-23 at 10.45.06](https://community.folivora.ai/uploads/default/original/2X/8/87a3349d3e7c520b41922f31784bbb26b36cab6a.png) ![Touch Bar Shot 2020-05-23 at 10.45.10](https://community.folivora.ai/uploads/default/original/2X/4/4ac225f0aceceb8bb2abb2236071a253140fa404.png)

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!

---

<div class="post-metadata">

**Author:** ![Future\_Face](https://community.folivora.ai/user_avatar/community.folivora.ai/future_face/32/13410_2.png) [@Future\_Face](https://community.folivora.ai/u/Future_Face)\
**Post date:** [May 24, 2020, 9:05pm UTC](https://community.folivora.ai/t/battery-triggered-button/16321/13 "2020-05-24T21:05:03Z")

</div>

> [@Caliguvara](#):
>
> Did you change it to that custom message? 🙂 Nice!

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

---

<div class="post-metadata">

**Author:** ![Caliguvara](https://community.folivora.ai/user_avatar/community.folivora.ai/caliguvara/32/42570_2.png) [@Caliguvara](https://community.folivora.ai/u/Caliguvara)\
**Post date:** [May 25, 2020, 7:38am UTC](https://community.folivora.ai/t/battery-triggered-button/16321/14 "2020-05-25T07:38:21Z")

</div>

Tested, works fine here.

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

---

<div class="post-metadata">

**Author:** ![Future\_Face](https://community.folivora.ai/user_avatar/community.folivora.ai/future_face/32/13410_2.png) [@Future\_Face](https://community.folivora.ai/u/Future_Face)\
**Post date:** [May 25, 2020, 8:42am UTC](https://community.folivora.ai/t/battery-triggered-button/16321/15 "2020-05-25T08:42:52Z")

</div>

> [@Caliguvara](#):
>
> scripting the Script Editor Mac

what's that?

---

<div class="post-metadata">

**Author:** ![Caliguvara](https://community.folivora.ai/user_avatar/community.folivora.ai/caliguvara/32/42570_2.png) [@Caliguvara](https://community.folivora.ai/u/Caliguvara)\
**Post date:** [May 25, 2020, 10:47am UTC](https://community.folivora.ai/t/battery-triggered-button/16321/16 "2020-05-25T10:47:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Future\_Face](https://community.folivora.ai/user_avatar/community.folivora.ai/future_face/32/13410_2.png) [@Future\_Face](https://community.folivora.ai/u/Future_Face)\
**Post date:** [May 25, 2020, 10:12pm UTC](https://community.folivora.ai/t/battery-triggered-button/16321/18 "2020-05-25T22:12:12Z")

</div>

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

 ![eg6](https://community.folivora.ai/uploads/default/original/2X/0/0cc4378fbc0089140923af70509b7f33cde02e66.jpeg)

---

<div class="post-metadata">

**Author:** ![Future\_Face](https://community.folivora.ai/user_avatar/community.folivora.ai/future_face/32/13410_2.png) [@Future\_Face](https://community.folivora.ai/u/Future_Face)\
**Post date:** [May 26, 2020, 5:48am UTC](https://community.folivora.ai/t/battery-triggered-button/16321/19 "2020-05-26T05:48:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Caliguvara](https://community.folivora.ai/user_avatar/community.folivora.ai/caliguvara/32/42570_2.png) [@Caliguvara](https://community.folivora.ai/u/Caliguvara)\
**Post date:** [May 26, 2020, 6:29am UTC](https://community.folivora.ai/t/battery-triggered-button/16321/20 "2020-05-26T06:29:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Future\_Face](https://community.folivora.ai/user_avatar/community.folivora.ai/future_face/32/13410_2.png) [@Future\_Face](https://community.folivora.ai/u/Future_Face)\
**Post date:** [May 26, 2020, 6:54am UTC](https://community.folivora.ai/t/battery-triggered-button/16321/21 "2020-05-26T06:54:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Caliguvara](https://community.folivora.ai/user_avatar/community.folivora.ai/caliguvara/32/42570_2.png) [@Caliguvara](https://community.folivora.ai/u/Caliguvara)\
**Post date:** [May 26, 2020, 7:00am UTC](https://community.folivora.ai/t/battery-triggered-button/16321/22 "2020-05-26T07:00:19Z")

</div>

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
```

[Next page](https://community.folivora.ai/t/battery-triggered-button/16321.md?page=2)
