How can I check the idling time of the user with Applescript?

How can I check the idling time of the user with Applescript? I would like the script to quit an application (iTunes) if the user isn’t doing anything (moving mouse or typing keyboard) for x minutes.

yeah how do you use idles the closest i could figure out was


repeat
	display dialog "How Long do you want before i turn off itunes?" default answer "In Minutes"
	set idle_for to the text returned of the result as number
	--really bad grammar
	delay (idle_for * minutes)
	tell application "iTunes"
		quit
	end tell
	set again_maybe to the button returned of (display dialog "again?" buttons {"Sure", "No"} default button 1)
	if again_maybe = "no" then exit repeat
	if again_maybe = "sure" then
	end if
end repeat

That’s not exactly what I’m looking for, so let me state my purpose explicitly. What I want to do is to quit iTunes if the user isn’t on the computer. I made iCal to run an (wakeup) alarm script that runs iTunes and plays a playlist, simultaneously increasing the sound volume. So the dialog’s won’t help if there is no user at computer. You see, I would like that the script would quit iTunes if the user isn’t there.

Well oh, if I would like to do really something fancy, I should check whether the Internet connection is available, because my default playlist contains a web stream.

And all this for the lack of proper alarm clock. :o

This is a very simple script to do what you want (I think). It doesn’t test if the user is there using anything too advanced, it simply asks. If the user isn’t there (or doesn’t answer in 30 seconds), the script will quit iTunes and itself. If the user answers, it will ask again after an amount of time specified by the user depending on how they answer. As I said, this is a simple script and could be made much better. Save this script as a stay open application:

property the_delay : 10

on idle
	set the_results to (display dialog "Are you there? Ask again in:" default answer the_delay buttons {"Quit", "Minutes"} default button 2 with icon 1 giving up after 30)
	if gave up of the_results = true then
		tell application "iTunes" to quit
		quit
	else
		if button returned of the_results = "Quit" then
			quit
		else
			set the_delay to (text returned of the_results) as number
			return (the_delay * hours)
		end if
	end if
end idle

Jon

Hi,

Another way is to set the screen saver to go on after a certain time. Say you set it to go on after 5 minutes of inactivity. If you have your system never sleep and a time for display sleep say 10 minutes, then after 5 minutes the screen saver runs. So, you monitor your processes to check for when ScreenSaverEngine runs. You can test it with an idle handler. Something like this:

global process_list
-- 
on run
	tell application "Finder"
		set process_list to (name of every process)
	end tell
end run
-- 
on idle
	tell application "Finder"
		set temp_process_list to (name of every process)
	end tell
	if temp_process_list is not process_list then
		set new_processes to {}
		repeat with this_process in temp_process_list
			if this_process is not in process_list then
				set end of new_processes to this_process
			end if
		end repeat
		choose from list new_processes
		quit
	end if
	return 2
end idle

After you have your screen saver set in the System Preferences to run after 5 minutes of inactivity, run the above script. Note that it should be saved as stay open application. After running the script, wait for about 5 minutes when the screen saver goes off. Then, a notification will be posted by the script and you’ll see a choose from list dialog. This method can be used to turn off iTunes when there is no activity after five minutes.

gl,

If you are interested in Kel’s idea of hooking into the screen saver, you might want to take a look at ScriptSaver.

Description: “ScriptSaver is a screen saver module which executes an AppleScript of your choosing when it activates… you could use it to refresh a folder, log out a user, empty the trash, or relaunch an application. It’s limited only by what you come up with!”

d/l - http://homepage.mac.com/swannman/.cv/swannman/Public/ScriptSaver.sit-link.sit
Home - http://homepage.mac.com/swannman

So after you have tested the above script and found that “ScreenSaverEngine” comes on when there is no activity, you can use a simple script like this:

global idle_flag

on run
set idle_flag to false
end run

on idle
tell application “Finder”
if (exists process “ScreenSaverEngine”) then set idle_flag to true
end tell
if idle_flag then
quit application “iTunes”
quit
end if
return 2
end idle

OSX is not as versatile as OS9 so you have to try and think of these workarounds.

gl,