如果您指定-currentHost,则defaults 返回的信息仅限于对用户当前登录的主机的首选项操作(这些主机首选项可以在~/Library/Preferences/ByHost 中找到)。
• 默认数据库上的操作通常适用于用户可以登录的任何主机,但可能仅限于适用于特定的
主持人。
• 如果未提供主机,则首选项操作将应用于用户可以登录的任何主机。
-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