Selectively disabling the screensaver when certain programs are active

In case anyone is interested, I came up with a way to selectively disable the screensaver when certain applications are running. This works under Snow Leopard and probably under Leopard. Although this is principally done with an AppleScript, you’ll also need Xcode to create a small auxiliary executable. All this executable does is invoke the UpdateSystemActivity() routine to reset the system’s idle time counter. The AppleScript invokes this periodically (at an interval less than the screensaver’s idle time) whenever the programs of interest are running.

First of all open up a file called update_activity.m and put the following code into it:

Then, compile it as follows:

Install the resulting update_activity executable in some place like /usr/local/bin.

Then, create the following AppleScript:

property idlePause : 60 -- how long to wait between checks for programs' status (in seconds)
property resetSystemIdleTimeProgram : "/usr/local/bin/update_activity"
property programList : {"QuickTime Player", "QuickTime Player 7"}

on idle
	tell application "System Events"
		repeat with theProgram in programList
			try
				if (exists process theProgram) then
					do shell script resetSystemIdleTimeProgram
					exit repeat
				end if
			end try
		end repeat
	end tell
	return idlePause
end idle

Put the full pathname of the executable created above into the resetSystemIdleTimeProgram property, and put the list of the applications that you want to monitor into the programList property.

Save this as an Application, and be sure to select Stay Open.

Then, set this app to run as one of your Login Items, and you’re all set.

Some day when I have more spare time, I’ll figure out how to store the list of programs externally, so that the AppleScript can read it at run time and it doesn’t have to be changed every time this list is altered.

I have been using this for a while, and it works fine for me.

I put a copy of that update_activity utility on a web page, if anyone is interested in it and doesn’t want to go through the trouble of installing Xcode in order to build it. The version I put there is for Mac OS 10.6.x (Snow Leopard) running on an Intel-based machine. I don’t have any other versions available, unfortunately.

Here’s the location of the page where I put it: http://yadda-yadda-yadda.com/stuff.

I plan to keep that page up and running, although there’s no guarantee that it will still be there after a huge amount of time has passed.

OK. I have improved the AppleScript so that it gets the list of apps to monitor by reading a file. It re-reads this file during every call to the idle handler, so you can modify this file, and the changes will be recognized by the already-running screensaver monitor utility.

The file takes one application name per line.

For example:

Here’s the new and improved AppleScript:

property appListPath : "/path/to/SS-Monitor-App.list" -- can be in either Apple or POSIX form
property fileDelimiters : {"
", return}
property idlePause : 60 -- how long to wait between checks for the apps' running status (in seconds)
property resetSystemIdleTimeProgram : "/usr/local/bin/update_activity"

on idle
	set appList to getAppList(appListPath)
	tell application "System Events"
		repeat with theApp in appList
			try
				if (exists process theApp) then
					do shell script resetSystemIdleTimeProgram
					exit repeat
				end if
			end try
		end repeat
	end tell
	return idlePause
end idle

on getAppList(fileLocation)
	try
		set appList to read file fileLocation using delimiter fileDelimiters -- Apple form
	on error
		try
			set appList to read POSIX file fileLocation using delimiter fileDelimiters -- POSIX form
		on error
			set appList to {}
		end try
	end try
	return appList
end getAppList

Just set appListPath to the full pathname of the file that contains the app names. This pathname can either be in Apple or POSIX form.