Lock the keychain when the screensaver activates

Feb 09, '04 10:02:00AM

Contributed by: jakacmar

I like to have my keychain lock itself when I'm away from the computer so others can't go snooping around where they don't belong. I've tried using the preferences available in Keychain Access, but none of them were able to do what I want, which is to lock the keychain when the screensaver is activated. In Keychain Access, the only option is to have the keychain lock itself after some minutes of inactivity. Unfortunately, this means inactivity within the keychain itself, not inactivity of your computer in general. I didn't want to require a password to exit the screensaver, because sometimes people do need to use my computer (for the net, etc.) so I wrote this little script to lock the default keychain whenever the ScreenSaver is activated. I saved my script as an "application bundle" called "Keychain Locker" (go figure) and then used this hint to hide its dock icon. The script follows...

Enter this in script editor:


on idle
  try
    tell application "Keychain Scripting"
      set KeyName to the name of the current keychain as string
      if locked of keychain KeyName is true then
        return 60
      else if locked of keychain KeyName is false then
        tell application "System Events"
        set prog_list to (get name of every process) as list
        repeat with i from 1 to (count of items in prog_list)
          if item i of prog_list contains "ScreenSaver" then
            tell application "Keychain Scripting"
              lock keychain KeyName
              return 60
            end tell
          end if
        end repeat
      end tell
      return 20
    end if
  end tell
  on error
    display dialog "An error has occured in Keychain Locker" buttons ¬
    {"Quit"} default button 1
    if the button returned of the result is "Quit" then
      tell me to quit
    end if
  end try
end idle
I also set it as a startup item. I've been using it for weeks and have had no problems. And even at its worst, it takes no more than 2.5% of CPU on my 800 Mhz G4 iMac. Hopefully this can help others who are looking for this functionality.

Comments (9)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20040203002020660