macos – 如何使用命令行或以编程方式阅读“安全和隐私”设置

我想阅读首选项中的设置 – >安全与安全隐私 – >我的应用程序中的常规选项特别是我感兴趣的是,如果用户设置了密码,并且在“睡眠或屏幕保护程序开始后”或者在延迟一段时间之后立即需要密码.

通过查看默认设置,我能够找到屏幕保护程序.

命令行:$defaults -currentHost读取com.apple.screensaver

码:

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

使用相同的推理我试图找到“安全和隐私”的plist文件,但我无法从/ Library / Preferences /或〜/ Library / Preferences /中的任何plist文件中检索此设置.

我只对阅读价值感兴趣.所以我的问题是,这可以做到吗?如果有,怎么样?

最佳答案 如果指定-currentHost,则默认返回的信息仅限于对用户当前登录的主机的首选项操作(这些主机首选项可在〜/ 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.

因此,为了获得您所询问的信息:

$defaults read com.apple.screensaver

通过省略-currentHost选项,它应该返回:

{
    askForPassword = 1;
    askForPasswordDelay = 0;
}

如果您想使用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);
    }
}

输出:

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

点赞