使用 applescript 检测电池百分比
Detect battery percentage with applescript
我如何检测我的计算机的电池百分比并使用 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
现在您可以解析 theBattString
以从中获取您想要的信息。
另一种选择...
on run
do shell script "ioreg -l | grep -i capacity | tr '\n' ' | ' | awk '{printf(\"%.2f%%\n\", / * 100)}'"
-- will output...
-- 79.63%
end run
感谢 ThrowBackDewd 的回答。
我做了一些有点奇怪但对我有用的东西。
这是 AppleScript
[编辑]
感谢用户@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
空闲应用程序
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
[第一个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
您可以将其添加到空闲语句中,并每分钟检查一次电池电量。
如有任何建议或更正,我们将不胜感激。
此致。
applescript 无符号变量可以像带符号的 FileMaker 变量一样使用...可以删除 ** 我试过加粗
设置batteryPercent执行shell脚本“pmset -g batt”
告诉应用程序“FileMaker Pro.app”
告诉table“电源测试结果”
告诉记录 1
将单元格“通过 AppleScript Global 测试百分比”设置为 batteryPercent
结束告诉
结束告诉
结束告诉
我如何检测我的计算机的电池百分比并使用 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
现在您可以解析 theBattString
以从中获取您想要的信息。
另一种选择...
on run
do shell script "ioreg -l | grep -i capacity | tr '\n' ' | ' | awk '{printf(\"%.2f%%\n\", / * 100)}'"
-- will output...
-- 79.63%
end run
感谢 ThrowBackDewd 的回答。
我做了一些有点奇怪但对我有用的东西。
这是 AppleScript
[编辑]
感谢用户@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
空闲应用程序
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
[第一个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
您可以将其添加到空闲语句中,并每分钟检查一次电池电量。
如有任何建议或更正,我们将不胜感激。
此致。
applescript 无符号变量可以像带符号的 FileMaker 变量一样使用...可以删除 ** 我试过加粗
设置batteryPercent执行shell脚本“pmset -g batt”
告诉应用程序“FileMaker Pro.app” 告诉table“电源测试结果” 告诉记录 1 将单元格“通过 AppleScript Global 测试百分比”设置为 batteryPercent 结束告诉 结束告诉 结束告诉