Friday, July 20, 2018

#1 2009-09-12 07:09:58 pm

HippoMan
Member
Registered: 2008-09-18
Posts: 114

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:

#import <ApplicationServices/ApplicationServices.h>
int main(int argc, char *argv[]) {
  UpdateSystemActivity(OverallAct);
  return (0);
}


Then, compile it as follows:

gcc -framework ApplicationServices -o update_activity update_activity.m


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

Then, create the following AppleScript:

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.

Last edited by HippoMan (2009-09-13 05:48:09 am)

Offline

 

#2 2009-09-12 08:03:04 pm

HippoMan
Member
Registered: 2008-09-18
Posts: 114

Re: Selectively disabling the screensaver when certain programs are active

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.

Last edited by HippoMan (2009-09-12 09:38:43 pm)

Offline

 

#3 2009-09-12 09:32:14 pm

HippoMan
Member
Registered: 2008-09-18
Posts: 114

Re: Selectively disabling the screensaver when certain programs are active

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:

QuickTime Player
QuickTime Player 7


Here's the new and improved AppleScript:

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.

Last edited by HippoMan (2009-09-14 07:20:01 am)


Filed under: System

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)