如何使用命令行或以编程方式读取 "Security & Privacy" 设置
How to read "Security & Privacy" settings using command line or programatically
我想从我的应用程序的 Preferences -> Security & Privacy -> General
选项卡中读取设置。我特别感兴趣的是用户是否设置了密码,以及是否立即需要密码 "after sleep or screen saver begins" 或延迟一段时间后才需要。
我可以通过查看其默认设置来确定屏幕保护程序何时启动。
命令行:$ defaults -currentHost read com.apple.screensaver
代码:
CFPreferencesCopyValue(CFSTR("idleTime"),
CFSTR("com.apple.screensaver"),
kCFPreferencesCurrentUser,
kCFPreferencesCurrentHost);
使用相同的推理,我试图找到 "Security & Privacy" 的 plist 文件,但我无法从 /Library/Preferences/
或 ~/Library/Preferences/
中的任何 plist 文件中检索此设置。
我只对读取值感兴趣。所以我的问题是,这可以做到吗?如果是,怎么做?
如果您指定 -currentHost
,则由 defaults
编辑的信息 return 仅限于对用户当前登录的主机的首选项操作(这些主机首选项可以在在 ~/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
选项,它应该 return:
{
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}
我想从我的应用程序的 Preferences -> Security & Privacy -> General
选项卡中读取设置。我特别感兴趣的是用户是否设置了密码,以及是否立即需要密码 "after sleep or screen saver begins" 或延迟一段时间后才需要。
我可以通过查看其默认设置来确定屏幕保护程序何时启动。
命令行:$ defaults -currentHost read com.apple.screensaver
代码:
CFPreferencesCopyValue(CFSTR("idleTime"),
CFSTR("com.apple.screensaver"),
kCFPreferencesCurrentUser,
kCFPreferencesCurrentHost);
使用相同的推理,我试图找到 "Security & Privacy" 的 plist 文件,但我无法从 /Library/Preferences/
或 ~/Library/Preferences/
中的任何 plist 文件中检索此设置。
我只对读取值感兴趣。所以我的问题是,这可以做到吗?如果是,怎么做?
如果您指定 -currentHost
,则由 defaults
编辑的信息 return 仅限于对用户当前登录的主机的首选项操作(这些主机首选项可以在在 ~/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
选项,它应该 return:
{
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}