如何检测计算机的电池百分比,并使用applescript将其设置为变量?所有类似问题的答案都说要安装额外的软件,但我想用纯applescript来完成。这个是可能的吗?我还尝试过在applescript库中进行搜索,但没有成功。
发布于 2016-07-19 05:17:20
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以获取您想要的信息。
另一个选择..。
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发布于 2020-11-29 01:06:24
applescript无符号变量可以像有符号的FileMaker变量一样使用。**可以移除--我试过了
将batteryPercent设置为执行shell脚本"pmset -g batt“
告诉应用程序"FileMaker Pro.app“告诉表"Power测试结果”将记录1组单元格“通过AppleScript全局测试百分比”转告到AppleScript end tell结束
发布于 2019-01-01 18:49:28
感谢ThrowBackDewd的回答。
我做了一些奇怪的东西,但对我来说很管用。
这是AppleScript
编辑
这里有一个更高效的代码,这要归功于user @user 3439894。
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闲置应用
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第一岗位
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您可以将其添加到空闲语句中,并每分钟检查一次电池电量。
如有任何建议或更正,将不胜感激。
致以问候。
https://stackoverflow.com/questions/38447131
复制相似问题