macosdefaults

How to read "Security & Privacy" settings using command line or programatically


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?


Solution

  • 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