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

查看:56
本文介绍了如何在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}'"

因此,您可能会假设如果没有按键被按下或鼠标移动了一段时间,则用户将不使用计算机.通过对空闲时间的一些巧妙跟踪,您可以知道用户何时离开计算机以及何时返回计算机.像这样的东西.

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天全站免登陆