Detect battery percentage with applescript
Asked Answered
B

3

6

How would I detect what my computer's battery percentage is at and set it to a variable using applescript? All answers to similar questions say to install additional software, but I would like to do this with purely applescript. Is this possible? I've also tried searching through the applescript library with no success.

Brandeebranden answered 18/7, 2016 at 22:51 Comment(0)
S
11
on run
    set theBattString to (do shell script "pmset -g batt")
    -- the above will return something like...
    -- Now drawing from 'Battery Power' -InternalBattery-0  82%; discharging; 4:06 remaining
end run

Now you can parse theBattString to get the information you'd like from it.

Another option...

on run
    do shell script "ioreg -l | grep -i capacity | tr '\\n' ' | ' | awk '{printf(\"%.2f%%\\n\", $10/$5 * 100)}'"
    -- will output...
    -- 79.63%
end run
Spate answered 19/7, 2016 at 5:17 Comment(3)
Great options, but instead of the complicated second approach you can extract the percentage more easily from pmset -g batt as follows: set pctg to do shell script "pmset -g batt | awk -F '[[:blank:]]+|;' 'NR==2 { print $3 }'"Broeder
pmset -g batt | awk -F '[[:blank:]]+|;' 'NR==2 { print $3 }' get the battery id instead the percentage nowSeniority
As @Metropolis mentioned above, the command returns the battery ID. Just swap { print $3 } with { print $4 } to get the percentage again.Phalarope
B
0

The applescript unsigned variable can be used like a signed FileMaker variable... the ** can be removed I tried it Bold it

set batteryPercent to do shell script "pmset -g batt "

tell application "FileMaker Pro.app" tell table "Power Test Results" tell record 1 set cell "Test percentage via AppleScript Global" to batteryPercent end tell end tell end tell

Burmese answered 29/11, 2020 at 1:6 Comment(0)
S
-1

Thank ThrowBackDewd for his answer.

I made something a bit strange but it works for me.

Here is the AppleScript

[EDIT]

Here a more efficient code thanks to user @user3439894.

    set batteryPercent to word 6 of paragraph 2 of (do shell script "pmset -g batt")

if batteryPercent < 40 then

    beep

    repeat 3 times

        say "Attention " & batteryPercent & "%" & " before shut down."

        display notification "Attention " & batteryPercent & "%" & " of charge before shut down." sound name "Glass"

    end repeat

end if

idle application

    on idle

    set batteryPercent to word 6 of paragraph 2 of (do shell script "pmset -g batt")

    if batteryPercent < 40 then



        repeat 3 times

            beep

            say "Attention " & batteryPercent & "%" & " of charge before shut down."

            display notification "Attention " & batteryPercent & "%" & " of charge before shut down." sound name "Glass"

        end repeat

    end if

    return 60

end idle

[First post]

  set theBattString to (do shell script "pmset -g batt")
-- the above will return something like...
-- Now drawing from 'Battery Power' -InternalBattery-0  82%; discharging; 4:06 remaining

set batteryLevel to splitText(theBattString, space)

--set totalItembatLvl to length of batteryLevel


set remainingTime to item 9 of batteryLevel

if remainingTime < "2:40" then
    display alert "low battery " & remainingTime & " before shut down."
    --batteryLevel & " " & remainingTime
end if


on splitText(theText, theDelimiter)
    set AppleScript's text item delimiters to theDelimiter
    set theTextItems to every text item of theText
    set AppleScript's text item delimiters to ""
    return theTextItems
end splitText
on getPositionOfItemInList(theItem, theList)
    repeat with a from 1 to count of theList
        if item a of theList is theItem then return a
    end repeat
    return 0
end getPositionOfItemInList

You can add it in an idle statement and check every minute for your battery level.

Any suggestions or corrections will be appreciated.

Regards.

Stadia answered 1/1, 2019 at 18:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.