4

I would like to read settings from Preferences -> Security & Privacy -> General tab in my application. In particular I'm interested if user has set up password and if password is required immediately "after sleep or screen saver begins" or after some delay.Security & Privacy General tab

I was able to find when screensaver kicks in by looking at its defaults.

command line: $ defaults -currentHost read com.apple.screensaver

code:

CFPreferencesCopyValue(CFSTR("idleTime"),
      CFSTR("com.apple.screensaver"),
      kCFPreferencesCurrentUser,
      kCFPreferencesCurrentHost);

Using the same reasoning I tried to find plist file for "Security & Privacy" but I'm unable to retrieve this settings from any of the plist files in /Library/Preferences/ or ~/Library/Preferences/.

I'm interested only in reading values. So my question is, can this be done? If yes, how?

solgar
  • 4,312
  • 2
  • 26
  • 30
  • 1
    I'm a bit confused about your question; are you looking to only find the value of whether a password is required and what the delay might be once the screensaver saver kicks in? Using `defaults read com.apple.screensaver` *without* `-currentHost` should return that information. – l'L'l Jan 17 '17 at 20:34
  • Gosh, I got confused by this thing. Calling this with and without gives completely different result and I need both parts to get all my answers. So, do you want to write an proper answer so I can accept it? – solgar Jan 17 '17 at 21:35

1 Answers1

2

If you specify -currentHost then the information returned by defaults is restricted to preferences operations to the host the user is currently logged in on (these host preferences can be found in ~/Library/Preferences/ByHost).

• Operations on the defaults database normally apply to any host the user may log in on, but may be restricted to apply only to a specific host.

• If no host is provided, preferences operations will apply to any host the user may log in on.

 -currentHost
  Restricts preferences operations to the host the user is currently logged in on.

 -host hostname
  Restricts preferences operations to hostname.

So in order to get the information you've asked about:

$ defaults read com.apple.screensaver

By omitting the -currentHost option it should return:

{
    askForPassword = 1;
    askForPasswordDelay = 0;
}

If you wanted to use CFPrefs:

#import <CoreFoundation/CoreFoundation.h>

#define EX_KEY "askForPasswordDelay"
#define EX_ID "com.apple.screensaver"

extern CFDictionaryRef _CFPreferencesCopyApplicationMap(CFStringRef userName, CFStringRef hostName);

int main(int argc, char *argv[])
{
    @autoreleasepool
    {
        CFURLRef current_url;
        CFStringRef path;
        CFMutableStringRef plist_path;
        CFPropertyListRef value;

        CFDictionaryRef app_map = _CFPreferencesCopyApplicationMap(
                                   kCFPreferencesCurrentUser,
                                   kCFPreferencesAnyHost);
        CFArrayRef urls = CFDictionaryGetValue(app_map, CFSTR(EX_ID));

        current_url = CFArrayGetValueAtIndex(urls, 0);
        path = CFURLCopyPath(current_url);

        plist_path = CFStringCreateMutable(kCFAllocatorDefault, 0);
        CFStringAppend(plist_path, path);
        CFStringAppend(plist_path, CFSTR(EX_ID));

        CFPropertyListRef prefs = CFPreferencesCopyValue(
        CFSTR(EX_KEY),
        CFSTR(EX_ID),
        CFSTR("kCFPreferencesCurrentUser"),
        CFSTR("kCFPreferencesAnyHost"));

        printf("CFPreferencesCopyValue \"%s\" of %s via ApplicationMap at path:\n", EX_KEY, EX_ID);
        CFShow(plist_path);
        CFShow(prefs);

        CFRelease(prefs);
        CFRelease(plist_path);
        CFRelease(path);
        CFRelease(app_map);
    }
}

Output:

CFPreferencesCopyValue "askForPasswordDelay" of com.apple.screensaver via ApplicationMap at path:
/Users/Username/Library/Preferences/com.apple.screensaver
<CFNumber 0x47 [0x7fffbf0a9d80]>{value = +0.0, type = kCFNumberFloat32Type}

OSX Man Pages : defaults

l'L'l
  • 44,951
  • 10
  • 95
  • 146
  • I don't see anywhere in (e.g. `defaults read` or the ~/Library/Preferences/ByHost/* files where it stores which apps have been granted permissions such as camera access. I need a LaunchDaemon run script to have this permission, any ideas where to look? – Michael Mar 16 '20 at 19:15