首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用applescript检测电池百分比

用applescript检测电池百分比
EN

Stack Overflow用户
提问于 2016-07-18 22:51:11
回答 3查看 3K关注 0票数 4

如何检测计算机的电池百分比,并使用applescript将其设置为变量?所有类似问题的答案都说要安装额外的软件,但我想用纯applescript来完成。这个是可能的吗?我还尝试过在applescript库中进行搜索,但没有成功。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-07-19 05:17:20

代码语言:javascript
复制
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

现在,您可以解析theBattString以获取您想要的信息。

另一个选择..。

代码语言:javascript
复制
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
票数 10
EN

Stack Overflow用户

发布于 2020-11-29 01:06:24

applescript无符号变量可以像有符号的FileMaker变量一样使用。**可以移除--我试过了

batteryPercent设置为执行shell脚本"pmset -g batt“

告诉应用程序"FileMaker Pro.app“告诉表"Power测试结果”将记录1组单元格“通过AppleScript全局测试百分比”转告到AppleScript end tell结束

票数 0
EN

Stack Overflow用户

发布于 2019-01-01 18:49:28

感谢ThrowBackDewd的回答。

我做了一些奇怪的东西,但对我来说很管用。

这是AppleScript

编辑

这里有一个更高效的代码,这要归功于user @user 3439894。

代码语言:javascript
复制
    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

闲置应用

代码语言:javascript
复制
    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

第一岗位

代码语言:javascript
复制
  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

您可以将其添加到空闲语句中,并每分钟检查一次电池电量。

如有任何建议或更正,将不胜感激。

致以问候。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38447131

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档