如何检测用户是否在 OS X 中离开? [英] How to detect if user is away in OS X?

查看:21
本文介绍了如何检测用户是否在 OS X 中离开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to start a script when user goes away or returns back to computer. Is there any built-in method in AppleScript to check user's state? If not what else can I use in OS X?

推荐答案

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

Here's a command that might work for you. This will tell you how long it's been since the mouse moved or a keystroke was pressed.

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

因此,您可能会假设,如果一段时间内未按下任何键或移动鼠标,则用户未在使用计算机.通过对 idleTime 的一些巧妙跟踪,您可以判断用户何时离开计算机以及何时返回.像这样的东西.

So you might assume that if no key was pressed or the mouse was moved for a period of time then the user is not using the computer. With some clever tracking of the idleTime you can tell when the user left the computer and also when he returned. Something like this.

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

Save this as an applescript application and check the "stay open after run handler" checkbox. You can quit it any time by right-clicking on the dock icon and choosing quit.

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

这篇关于如何检测用户是否在 OS X 中离开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆