How to determine whether the user is in OSX?

I want to run a script, when a user leaves or comes back to the computer. Is there a built-in method in AppleScript to check the state of the user? If not, what else can I use in OSX?

+4
source share
2 answers

Here is a team that can work for you. This will show you how much time has passed since moving the mouse or pressing a key.

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

Thus, you can assume that if no key has been pressed, or the mouse has not been moved for a certain period of time, the user is not using the computer. With some clever tracking idleTime you can tell when the user leaves the computer, and when he returned. Something like that.

Save it as an applescript application and check the box "to remain open after the launch." You can withdraw from it at any time by right-clicking the Dock icon, and selecting "Exit".

 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 ioreg -c IOHIDSystem | awk '/ HIDIdleTime / {print $ NF / 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 ." 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 to timeBeforeComputerIsNotInUse then 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 " 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 
+10
source

Look iAway , a small application that allows you to run Apple Script, when you are away from your Mac and back. I use it to set your online status of my application to exchange messages and run a screen saver when you leave and when you return to get my online status. The application comes with other interesting features to actually determine if you are physically using computer vision.

+1
source

All Articles