![]() |
Automatically turn on/off Password Protection for the Screensaver
I have the following problem:
I'd like my computer to automaticly switch on/off password protection with the screensaver. I use the network configuration to determine where my laptop currently is (like my home-network: no password; network at work or no network at all: password required). The script is launched every time the network configuration changes (via the Kicker.bundle). This seems to work. The problem is to tell the screensaver that a password is required or not. I figured out, that this is stored at the com.apple.screensaver preference file. So setting it manually with this command should change the preference: Code:
defaults -currentHost write com.apple.screensaver askForPassword 1But it does not work with me. The plist-file is definitly changed and the preference pane does display the new status. But the screensaver seems to ignore my change completly. If I change the setting via the preference pane it does work. So I assume the Preference Pane somehow tells the screensaver the preferences have changed (I also checked: the PreferencePane does edit the same plist file and key). Anyone knows something about this? Could this be some kind of security issue? Could there be a workaround? I'm using OS X 10.4.3 with an iPhoto Picture Screensaver. |
If the low-level technical details don't scare you too much, you might be able to figure out what is going on by using the 'ktrace' command while you switch on the password in the preferences panel.
But beware that 'ktrace' generates a lot of output. Perhaps start by using 'fs_usage' to see what files are affected. |
Well, the low level technical details didn't scare me too much:
I tried ktrace with the fs_usage command and it did indeed bring some new information: There is only one file actually written by System Preferences. It's a file called "~/Library/Preferences/ByHost/cf#b6ERY" which obviously is a temporary file. It is renamed afterwards, and it seems it is the "...ByHost/com.apple.screensaver. ... .plist" file, since no other file is modified. But the interesting thing is: after the file is written and renamed, the loginwindow process does read the com.apple.screensaver file (it does read some more than this, it looks for com.apple.screensaver in different places and also re-reads the "../ByHost/.GlobalPreferences. ..." file). loginwindow is the process that owns the Screensaver, so obviously it is also reads the preferences for the Screensaver. I also checked what happens when the Screensaver itself is launched. The plist file is read by ScreensaverEngine and by loginwindow, but obviously it ignores the askForPassword key. So since the askForPassword seems to be ignored on Screensaver-Start, there must be some way for SystemPreferences to tell loginwindow to re-read the askForPassword key. I never used ktrace before, so I don't know if there is a way to check with it (or some other tool). Also loginwindow seems to be poorly documented, there's no man-page for it on mac os x. And all I could find on the net where some old NEXTSTEP man-pages ... |
Quote:
|
Oh, I obviously misunderstood you there. Never used ktrace or fs_usage before. This time I started by reading the manual first ...
I tried now as you said. I think I understood most of the things displayed by fs_usage: when I click on the option, system preferences does write to some kind of temporary file, named with some random characters. then it does a chmod on the file, then renames it to com.apple.screensaver. .. .plist (where .. is the host id). afterwards loginwindow reads the file. So, then there's ktrace: I don't really understand all it does there (also it is harder to find out where the acutal process I like to understand beginns and ends). The ktrace.out file is quite huge and if you convert it with kdump it's more than 10mb of text. So I killed ktrace (and the system preferences) after I changed the preference, so the information I seek should be right at the end. (I checked, if it is important if system preferences is killed or quit the normal way, but it's not) And: the information I was looking for is right at the end. I could find the same information I found with fs_usage. System Preferences does write to some random filename, then renames it to com.apple.screensaver ... and then ... nothing. .. nearly. the last thing it does, before my SIGINT hits it, is a call of ppc_gettimeofday, which looks for me like some kind of time-function. I can't figure out if that has anything to do with my problem. but it's weird. I checked parallel with fs_usage, and loginwindow did re-read the plist file after it was modified by system preferences. but I can't find a call from system preferences to loginwindow. I'm stuck there. PS: if anyone can really read the ktrace-dump I can post the part I thing is important, but it's quite a lot of text ... |
something to note.
if you have the password ticked on in the sys. prefs (security panel). And then close sys. prefs. Run the default write .... 0 . it will still be on until you open sys. prefs (screensaver panel) and do something like click the sleep slider. And close sys. prefs. It seem this forces sys. prefs to write and read from the byhost/com.apple.screensaver. .. .plist. Also note that if you open the (security panel) the tick is Now un-ticked. But I have found no way of turning it back on or without the interaction with the (screensaver panel). And using default write .... 1 If you have sys prefs open and run default write ... 1, and then default read you will see that the setting is set to 1 . close sys prefs. and run default read you will see it is reset to 0 |
I tested your suggestion and I can confirm it. I could turn the Password off, but not on with it. I also tried to automate the switching off, with the following applescript:
Code:
tell application "System Preferences"PS: I also tried setting the askForPassword checkbox via System Events, but I couldn't even find the according checkbox to change ... it seems it is some kind of "invisible" to System Events Scripting ... |
Well if thats what you want to do then this works.
Code:
tell application "System Preferences" |
Well, as written above, I already tried System Events. I don't know what you did, but I always get a "NSReceiverEvaluationScriptError: 4", which usually appears if you try click something, that doesn't exist. I tried your script, which is a little different from mine (also I'm not on an english mac, so my checkbox is named different), but I still get that error.
|
well, I tried it again, now with this script:
Code:
tell application "System Preferences" |
Quote:
|
A old thread but anyway this worked for me (10.4.7):
Had to add the "int" (integer) Quote:
|
I had the same problem some time ago to have my computer wake me up… and not go to sleep before having, as nobody had answered to its password-asking dialog within ten seconds of wake up (the computer's, not mine).
Giving gdb a try, I finally found that System Preferences first make the change then send a notification to tell the latent screen saver to reread the preferences. Here is the simple C program I wrote to mimic that: Code:
#include <CoreFoundation/CoreFoundation.h>Code:
cc -o /tmp/anywhereyouwantit/notif notif.c -framework CoreFoundationCode:
defaults -currentHost write com.apple.screensaver askForPassword -int 0 |
After using this technique, the screen saver stopped asking me for a password what so ever! Activating it from the prefpane has no effect, I'm stucked with a password-less screensaver! I know others are having the same issue (namely because many utilities rely on this trick)!
Please help! I've tried using gdb to see what went on behind the scene, but I didn't seem to be able to analyse anything (yeah I'm a gdb newb!). |
I've been meaning to try this out.
It seems to work for me. You have to use the notif on both commands $ defaults -currentHost write com.apple.screensaver askForPassword -int 1;~/notif $ defaults -currentHost write com.apple.screensaver askForPassword -int 0;~/notif |
I believe I have tracked this down to APE. Adding loginwindow and ScreenSaverEngine to the exclude list fixed the problem!
Hope this helps others. I wished APE worked the other way arround and had an include list... Unsanity are you reading? |
Update
I was looking on the apple site today and found this
Code:
tell application "System Events"Works in my test(as expected) The page I found this on is here applescript system-prefs. I assume this is new for Leopard. |
Setting the "require password to wake:false" property executes fine for me, but it doesn't actually change the value of the property. I test this by doing another "get properties" after I try to set it -- it's still set to "true". Here's the output from the Event Log:
Code:
tell application "System Events" |
The "require password to wake" works fine for me,
tell application "System Events" get properties of security preferences-->{require password to wake:true, class:security preferences object, secure virtual memory:false, require password to unlock:false, automatic login:true, log out when inactive:false, log out when inactive interval:5}set properties of security preferences to {require password to wake:false, secure virtual memory:false, require password to unlock:false, automatic login:false, log out when inactive:false, log out when inactive interval:60} get properties of security preferences -->{require password to wake:false, class:security preferences object, secure virtual memory:false, require password to unlock:false, automatic login:true, log out when inactive:false, log out when inactive interval:5}end tell But I do notice that the automatic login:false is not reported changed, But when I actually look in the prefpane it is ticked to be disabled? Hopefully 10.5.2 will fix this flakiness. |
Quote:
Code:
cc -arch ppc -arch i386 -o /tmp/anywhereyouwantit/notif notif.c -framework CoreFoundation |
Just wanted to confirm that Guillaume's code is still working with 10.5.7. I'm using it with a Bluetooth proximity program that unlocks/locks the computer when my iPhone is in/out of range.
Thank you! |
| All times are GMT -5. The time now is 04:31 PM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.