我想从 Preferences -> Security & Privacy -> General
读取设置我的应用程序中的选项卡。我特别感兴趣的是,如果用户设置了密码,并且“在 sleep 或屏幕保护程序开始后”或经过一段时间后立即需要密码。
通过查看其默认值,我能够找到屏幕保护程序何时启动。
命令行:$ 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/