访问内置电源管理器?状态

时间:2015-04-16 20:29:52

标签: applescript

我试图编写一个超级简单的AppleScript,它将启动OneDrive App,或者确保它是开放的,只要机器的电源设置为插入,并且将退出,或确保是当电源设置为电池时关闭。

我无法找到如何访问内置"电源指示灯"在约塞米蒂。我的所有searches导致old,与多年前无关results

编辑:我想我必须使用pmset -g batt

在applescript中使用do shell脚本
Now drawing from 'AC Power'
 -InternalBattery-0 100%; charged; 0:00 remaining

解析这个结果,但我不确定如何。

编辑:未来的任何人都可能想要类似的东西:

global appName

on appIsRunning()
    tell application "System Events" to (name of processes) contains appName
end appIsRunning

on acIsConnected()
    return (do shell script "system_profiler SPPowerDataType | grep -q 'Connected: Yes' && echo \"true\" || echo \"false\"") as boolean
end acIsConnected

on toggleApp()
    if my acIsConnected() then
        if not my appIsRunning() then
            tell application "Finder"
                open application file (appName & ".app") of folder "Applications" of startup disk
            end tell
        end if
    else
        tell application appName
            quit
        end tell
    end if
end toggleApp

-- This will only be executed once.
on run
    set appName to "OneDrive"
end run

-- This will be executed periodically, specified in seconds, every return.
on idle
    my toggleApp()
    -- Execute every 2 minutes.
    return 120
end idle

-- Not mandatory, but useful for cleaning up before quiting.
on quit
    -- End handler with the following line.
    continue quit
end quit

4 个答案:

答案 0 :(得分:3)

以下是民意调查连接状态的单线程,因为我猜你可以少于100%且仍然连接(充电)。

set acConnected to (do shell script "system_profiler SPPowerDataType |grep -q 'Connected: Yes' && echo \"true\" || echo \"false\"") as boolean

答案 1 :(得分:2)

这是另一个班轮......

set acConnected to last word of paragraph 1 of (do shell script "ioreg -w0 -l | grep ExternalChargeCapable")

答案 2 :(得分:1)

如果您乐意使用第三方工具,则可以避免轮询电池状态。这将使您的脚本更有效。

Power Manager可以在电池状态发生变化时运行AppleScripts。 How to Run a Command When Switching to Battery Power,介绍如何为脚本设置此内容。

在脚本中换出#!/bin/sh的{​​{1}},然后就可以使用AppleScript了。

Power Manager - run a script on switch to battery

免责声明:我写过电源管理器,可以回答有关其工作原理的评论。

答案 3 :(得分:1)

如果屏幕右上方有电池图标:

tell application "System Events" to tell process "SystemUIServer" ¬ 
to value of attribute "AXDescription" of ¬
(first menu bar item whose value of attribute "AXDescription" ¬
begins with "Battery") of menu bar 1

您获得"Battery: Charged""Battery: Calculating Time Remaining… "或其他