[英]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. 我想从我的应用程序中的
Preferences -> Security & Privacy -> General
选项卡中读取设置。 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. 特别是我感兴趣的是,如果用户设置了密码,并且“在睡眠或屏幕保护程序开始之后”或者在延迟之后立即需要密码。
I was able to find when screensaver kicks in by looking at its defaults. 通过查看默认设置,我能够找到屏幕保护程序。
command line: $ defaults -currentHost read com.apple.screensaver
命令行:
$ 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/
. 使用相同的推理我试图找到“安全和隐私”的plist文件,但我无法从
/Library/Preferences/
或~/Library/Preferences/
任何plist文件中检索此设置。
I'm interested only in reading values. 我只对阅读价值感兴趣。 So my question is, can this be done?
所以我的问题是,这可以做到吗? If yes, how?
如果有,怎么样?
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
). 如果指定
-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.
So in order to get the information you've asked about: 因此,为了获得您所询问的信息:
$ defaults read com.apple.screensaver
By omitting the -currentHost
option it should return: 通过省略
-currentHost
选项,它应该返回:
{
askForPassword = 1;
askForPasswordDelay = 0;
}
If you wanted to use CFPrefs
: 如果您想使用
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 ↳OSX手册页:默认值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.