如何检测用户是否是客场OSX? [英] How to detect if user is away in OSX?
问题描述
我要开始脚本时用户会消失或返回到计算机。是否有任何的AppleScript内置的方法来检查用户的状态呢?如果没有什么别的可在OSX我用?
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 OSX?
推荐答案
下面是可能会为你工作的命令。这会告诉你,因为鼠标移动或击键为pressed它多长时间了。
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}'"
所以,你可能会认为,如果没有关键是pressed或鼠标进行移动,则用户不会使用电脑一段时间。随着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
这篇关于如何检测用户是否是客场OSX?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!