macos - 如何使用命令行或以编程方式读取 "Security & Privacy"设置

标签 macos defaults

我想从 Preferences -> Security & Privacy -> General 读取设置我的应用程序中的选项卡。我特别感兴趣的是,如果用户设置了密码,并且“在 sleep 或屏幕保护程序开始后”或经过一段时间后立即需要密码。 Security & Privacy General tab

通过查看其默认值,我能够找到屏幕保护程序何时启动。

命令行:$ defaults -currentHost read com.apple.screensaver
代码:

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

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

我只对阅读值(value)观感兴趣。所以我的问题是,这能做到吗?如果是,如何?

最佳答案

如果您指定 -currentHost那么defaults返回的信息仅限于对用户当前登录的主机的首选项操作(这些主机首选项可以在 ~/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

关于macos - 如何使用命令行或以编程方式读取 "Security & Privacy"设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41702509/

相关文章:

ios - 根据环境变量调整 Xcode Build 设置值

mysql - 如何从 macOS 上的 MySQL Workbench 获取我忘记的 MySQL 密码?

mercurial - 在 Mercurial 中,如果存在默认参数,如何运行原始命令?

Python - 读取和/或写入 ini 文件的选项

php - 函数名($param, $line = __LINE__, $file = __FILE__) {};

macos - 为什么 WebGL 和 Cocoa 之间的 GL_ALIASED_POINT_SIZE 不同?

java - 阻止 OSX 自动调整 JComboBox 大小?

c++ - 在模板类的复制构造函数中使用默认值时出错

r - 您是否始终在write.csv中使用row.names = F?更改R(基本)函数中的默认值

excel - 即使拼写错误,如何在excel中比较文本