使用applescript检测电池百分比

使用applescript检测电池百分比,applescript,osx-yosemite,Applescript,Osx Yosemite,如何检测计算机的电池百分比,并使用applescript将其设置为一个变量?所有类似问题的答案都是安装额外的软件,但我想用纯applescript来实现这一点。这可能吗?我也尝试过在applescript库中搜索,但没有成功 on run set theBattString to (do shell script "pmset -g batt") -- the above will return something like... -- Now drawing from

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

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
现在,您可以解析battstring,从中获取所需的信息

另一个选择

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

谢谢斯洛巴克德的回答

我做了一件有点奇怪的事,但它对我有用

这是苹果书

[编辑]

感谢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
将batteryPercent设置为第2段的单词6(执行shell脚本“pmset-g batt”)
如果电池电量<40,则
嘟嘟声
重复3次
关机前说“注意”&BATTYPERCENT&“%”和“注意”
关机前显示充电通知“注意”和电池电量和“%”和“声音名称”玻璃
结束重复
如果结束
空闲应用程序

    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
处于空闲状态
将batteryPercent设置为第2段的单词6(执行shell脚本“pmset-g batt”)
如果电池电量<40,则
重复3次
嘟嘟声
关机前请说“注意”&电池电量&“%”和“充电”
关机前显示充电通知“注意”和电池电量和“%”和“声音名称”玻璃
结束重复
如果结束
返回60
结束空闲
[第一篇文章]

  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
将battstring设置为(执行shell脚本“pmset-g batt”)
--以上将返回类似于。。。
--现在从“电池电源”中提取——内部电池-0 82%;放电;剩余4:06
将BATTYLEVEL设置为拆分文本(BATTSTRING,空格)
--将totalItembatLvl设置为batteryLevel的长度
将剩余时间设置为batteryLevel的第9项
如果剩余时间<“2:40”,则
显示警报“电池电量不足”和“关机前剩余时间”
--电池级别和剩余时间(&R)
如果结束
在拆分文本时(文本,删除器)
将AppleScript的文本项分隔符设置为Delimiter
将文本项设置为文本的每个文本项
将AppleScript的文本项分隔符设置为“”
返回文本项
结束拆分文本
在getPositionOfItemInList(ITItem,theList)上
重复此步骤,从1到列表计数
如果列表中的项目a为ITITEM,则返回a
结束重复
返回0
结束getPositionOfItemInList
您可以将其添加到idle语句中,并每分钟检查一次电池电量

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


注意。

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

设置BATTYPERCENT执行shell脚本“pmset-g batt”

告诉应用程序“FileMaker Pro.app” 告诉表“功率测试结果” 告诉记录1 将单元格“通过AppleScript Global测试百分比”设置为batteryPercent 结束语 结束语
结束告诉

很好的选择,但是代替复杂的第二种方法,您可以更容易地从
pmset-g batt
中提取百分比,如下所示:
set pctg to do shell script“pmset-g batt | awk-F'[[:blank:]+|''NR==2{print$3}'
pmset-g batt | awk-F'[:blank:]+|''NR==2{print$3}
获取电池id,而不是上面提到的@Metropolis的百分比,命令返回电池id。只需将
{print$3}
{print$4}
交换即可再次获取百分比。