Hi,
I was wondering if it is possible to make an applescript that warns you when your battery power goes below a certain percentage?
Hi,
try this, save it as stay open application, it checks the battery status every minute.
The threshold (in percent) is defined in the property line
property lowBattery : 10
on idle
set battStatus to do shell script "pmset -g ps"
if battStatus contains "InternalBattery" then
set {TID, text item delimiters} to {text item delimiters, ";"}
set battStatus to text items of battStatus
set text item delimiters to space
set battStatus to text 1 thru -2 of last text item of item 1 of battStatus as integer
set text item delimiters to TID
if battStatus < lowBattery then say "Battery power is lower than " & lowBattery & " percent"
end if
return 60
end idle
When i open the application it says: Can’t make “-InternalBattery-0100” into type integer.
what is the result of this line on your machine?
set battStatus to do shell script "pmset -g ps"
“Currently drawing from ‘AC Power’
-InternalBattery-0 100%; AC attached; not charging”
ok, the syntax of Tiger is a little bit different
property lowBattery : 10
on idle
set battStatus to do shell script "pmset -g ps"
if battStatus contains "InternalBattery" then
set {TID, text item delimiters} to {text item delimiters, ";"}
set battStatus to text items of battStatus
set text item delimiters to tab
set battStatus to text 1 thru -2 of last text item of item 1 of battStatus as integer
set text item delimiters to TID
if battStatus < lowBattery then say "Battery power is lower than " & lowBattery & " percent"
end if
return 60
end idle
Yeah that worked, thank you very much…
pmset return values changed. Updated version.
property lowBattery : 10
idler()
on idler()
set battStatus to do shell script "pmset -g ps"
if battStatus contains "InternalBattery" then
set {TID, text item delimiters} to {text item delimiters, ";"}
set battStatus to text items of battStatus
set battCharging to item 2 of battStatus as string
display notification "(" & battCharging & ")"
set text item delimiters to tab
set battStatus to text 1 thru -2 of last text item of item 1 of battStatus as integer
set text item delimiters to TID
if battCharging = " charging" then
set whatToSay to "A/C attached. " & battStatus & " percent power left"
else
set whatToSay to "Running on battery " & battStatus & " percent power left"
end if
say whatToSay
if battStatus < lowBattery then say "Battery power is lower than " & lowBattery & " percent"
end if
return 60
end idler
Hi evilate. Welcome to MacScripter and thanks for posting your update.
Unfortunately, it says “Running on battery” when the machine’s running on mains with the battery “charged”.
Here’s another approach which simply says an edited version of what’s returned by ‘pmset’:
property lowBattery : 10
on idle
set battStatus to (do shell script "pmset -g ps | sed -E 's/Now |'\\''//g ; s/[^[:blank:]]+[[:blank:]]+([[:digit:]]+)%.+/\\1 per cent left/'")
say battStatus
-- (The integer to the left of '>' causes a silent coercion of the word to the right of it.)
if (lowBattery > (word 1 of paragraph 2 of battStatus)) then say "Battery power is lower than " & lowBattery & " percent"
return 60
end idle