Run script when battery is 10%

Very similar to Battery-triggered Button

I tried getting the code from that post and putting it in my own code, but I couldn't do it.
So does anyone know how to make the code below run when my battery is 10% remaining, and only once (and not while charging)?

set buttonSet to {"OK"}

display alert "Warning." message "Battery is 10%. Please charge your Macbook." buttons buttonSet as critical

Thanks

I use this script to provide an alert on the touchbar when the battery drops to 10% and not plugged in. The entire touchbar will be red in order to catch my attention.

set batteryStatus to do shell script "pmset -g batt"
set powerSource to word 4 of batteryStatus
set percentage to word 11 of batteryStatus as string
set charging to word 12 of batteryStatus
set remaininghours to word 8 of paragraph 2 of batteryStatus as string
set remainingminutes to word 9 of paragraph 2 of batteryStatus as string


if powerSource is "AC" then
	return ""
	
else if percentage is "100" then
	return ""
	
else if percentage is less than "11" then
	return "                        ⚠️     " & percentage & "% Remaining                      Warning: Low Battery!!                        " & " " & remaininghours & "h " & remainingminutes & "m" & "     ⚠️ "
	
else
	return ""
	
	
end if
1 Like

ill try to change this code to show what I want. Thanks!

1 Like
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
		set remainingminutes to word 9 of paragraph 2 of (do shell script "pmset -g batt")
		say "Warning: " & batteryPercent & "% Remaining. Please charge MacBook. " & remainingminutes & "min until lockdown" using "Alex" with stopping current speech without waiting until completion
		set buttonSet to {"OK"}
		
		display alert "Warning." message "Battery is " & batteryPercent & "%. Please charge your Macbook." buttons buttonSet as critical
		
		return "Warning: " & batteryPercent & "% Remaining. Please charge MacBook. " & remainingminutes & "min until lockdown"
	else
		return ""
	end if
end if

thanks, but I don't want it to say anything. also, does this line show the warning on the touchbar? I don't want that either...
return "Warning: " & batteryPercent & "% Remaining. Please charge MacBook. " & remainingminutes & "min until lockdown"

Delete the lines starting with "say" and "return"

1 Like

when I press run script, it works, but when the battery reaches the set percentage (set to 16% for testing) it didn't come up.

[
{
"BTTWidgetName" : "battery",
"BTTTriggerType" : 639,
"BTTTriggerTypeDescription" : "Apple Script / JavaScript Widget",
"BTTTriggerClass" : "BTTTriggerTypeTouchBar",
"BTTPredefinedActionType" : -1,
"BTTPredefinedActionName" : "No Action",
"BTTEnabled2" : 1,
"BTTRepeatDelay" : 0,
"BTTUUID" : "A3C99C29-EFC8-42C4-8B44-E569DAFBEEAA",
"BTTNotesInsteadOfDescription" : 0,
"BTTEnabled" : 1,
"BTTModifierMode" : 0,
"BTTOrder" : 39,
"BTTDisplayOrder" : 0,
"BTTMergeIntoTouchBarGroups" : 0,
"BTTTriggerConfig" : {
"BTTTouchBarAppleScriptString" : "set powersource to word 4 of paragraph 1 of (do shell script "pmset -g batt")\rif powersource is "AC" then\r\treturn ""\relse\r\tset batteryPercent to word 6 of paragraph 2 of (do shell script "pmset -g batt")\r\tif batteryPercent is "100" then\r\t\treturn ""\r\telse if batteryPercent is less than "17" then\r\t\tset remainingminutes to word 9 of paragraph 2 of (do shell script "pmset -g batt")\r\t\tset buttonSet to {"OK"}\r\t\t\r\t\tdisplay alert "Warning." message "Battery is " & batteryPercent & "%. Please charge your Macbook." buttons buttonSet as critical\r\t\t\r\telse\r\t\treturn ""\r\tend if\rend if",
"BTTTouchBarButtonColor" : "75.323769, 75.323769, 75.323769, 255.000000",
"BTTTouchBarItemPlacement" : 0,
"BTTTouchBarAppleScriptStringRunOnInit" : 1,
"BTTTouchBarAlwaysShowButton" : false,
"BTTScriptType" : 0,
"BTTTouchBarScriptUpdateInterval" : 0.10000000149011612,
"BTTTouchBarButtonWidth" : 100,
"BTTTouchBarItemIconHeight" : 22,
"BTTTouchBarButtonTextAlignment" : 0,
"BTTTouchBarAlternateBackgroundColor" : "75.323769, 75.323769, 75.323769, 255.000000",
"BTTTouchBarAppleScriptUsePath" : 0,
"BTTTBWidgetWidth" : 400,
"BTTTouchBarItemIconWidth" : 22,
"BTTTouchBarButtonName" : "battery",
"BTTTouchBarButtonFontSize" : 15,
"BTTTouchBarIconTextOffset" : 5,
"BTTTouchBarOnlyShowIcon" : false,
"BTTTouchBarFreeSpaceAfterButton" : 5,
"BTTTouchBarItemPadding" : 0
}
}
]

bump.