[英]How to read “Security & Privacy” settings using command line or programatically
我想從我的應用程序中的Preferences -> Security & Privacy -> General
選項卡中讀取設置。 特別是我感興趣的是,如果用戶設置了密碼,並且“在睡眠或屏幕保護程序開始之后”或者在延遲之后立即需要密碼。
通過查看默認設置,我能夠找到屏幕保護程序。
命令行: $ defaults -currentHost read com.apple.screensaver
碼:
CFPreferencesCopyValue(CFSTR("idleTime"),
CFSTR("com.apple.screensaver"),
kCFPreferencesCurrentUser,
kCFPreferencesCurrentHost);
使用相同的推理我試圖找到“安全和隱私”的plist文件,但我無法從/Library/Preferences/
或~/Library/Preferences/
任何plist文件中檢索此設置。
我只對閱讀價值感興趣。 所以我的問題是,這可以做到嗎? 如果有,怎么樣?
如果指定-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手冊頁:默認值
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.