【问题标题】:How to detect if user is away in OS X?如何检测用户是否在 OS X 中?
【发布时间】:2013-07-31 07:53:17
【问题描述】:

我想在用户离开或返回计算机时启动脚本。 AppleScript 中是否有任何内置方法来检查用户的状态?如果不是,我还能在 OS X 中使用什么?

【问题讨论】:

    标签: macos applescript


    【解决方案1】:

    这是一个可能对您有用的命令。这将告诉您自从鼠标移动或按键被按下以来已经过了多长时间。

    set idleTime to do shell script "ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print $NF/1000000000; exit}'"
    

    因此,您可能会假设,如果一段时间内没有按下任何键或移动鼠标,那么用户就没有使用计算机。通过对 idleTime 的一些巧妙跟踪,您可以知道用户何时离开计算机以及何时返回。像这样。

    将其保存为 AppleScript 应用程序并选中“运行处理程序后保持打开状态”复选框。您可以随时通过右键单击停靠图标并选择退出来退出它。

    global timeBeforeComputerIsNotInUse, computerIsInUse, previousIdleTime
    
    on run
        set timeBeforeComputerIsNotInUse to 300 -- 5 minutes
        set computerIsInUse to true
        set previousIdleTime to 0
    end run
    
    on idle
        set idleTime to (do shell script "ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print $NF/1000000000; exit}'") as number
    
        if not computerIsInUse then
            if idleTime is less than previousIdleTime then
                set computerIsInUse to true
                say "User is using the computer again."
            end if
        else if idleTime is greater than or equal to timeBeforeComputerIsNotInUse then
            set computerIsInUse to false
            say "User has left the computer."
        end if
    
        set previousIdleTime to idleTime
        return 1
    end idle
    

    【讨论】:

    • 这几乎正是我想要的,但是当我看视频/YouTube(这不应该算作空闲)时,它是否有效?
    • @RichardFu 尝试使用“pmset -g”,例如stackoverflow.com/questions/20054722/…
    【解决方案2】:

    看看iAway,这是一个小应用程序,当您离开 Mac 并返回时,它允许您运行 Apple Script。我用它来设置我的 Messenger 应用程序的在线状态,并在离开和返回时启动我的屏幕保护程序以重新设置我的在线状态。该应用程序还具有其他很酷的功能,可以利用计算机视觉实际检测您是否在身体外。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-02
      • 1970-01-01
      • 2016-03-27
      • 2015-10-14
      • 2010-10-15
      • 1970-01-01
      • 2010-10-07
      • 2011-06-28
      相关资源
      最近更新 更多