2012-05-23 131 views
1

在我的Cocoa應用程序中,我想要訪問和更改計算機的屏幕鎖定超時設置。在系統偏好設置中更改它不需要用戶輸入管理員密碼。以編程方式更改屏幕鎖定超時

System Preferences screenshot - Screen lock timeout

不幸的是我無法找到的文檔中的任何信息,我不知道我應該考慮(安全設置/ prefPane編程)什麼話題。
任何幫助,將不勝感激。

+0

你們是不是要改變值或你想防止睡眠? – CRD

+0

@CRD嘗試更改值 – antalkerekes

回答

1
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/Users/new/Library/Preferences/com.apple.screensaver.plist"]; 
[plistDict setObject:@"1" forKey:@"askForPassword"]; 
[plistDict setObject:@"3600" forKey:@"askForPasswordDelay"]; 
[plistDict writeToFile:@"/Users/new/Library/Preferences/com.apple.screensaver.plist" atomically:YES]; 

或從終端

defaults write com.apple.screensaver askForPasswordDelay 5 
+0

我認爲它至少在OS X 10.9上不會立即發揮作用。睡眠後,操作系統仍然使用先前的值而不是新的值。看起來它需要一些觸發器。 – Nobu

1

以上回答顯然適用於一些,但它10.8如果您正在使用FileVault的失敗。該設置將保持不變,但只有啓動系統偏好設置後纔會生效。幸運的是,有一種方法可以「觸摸」設置在完成後:

- (void)touchSecurityPreferences; 
{ 
    NSAppleScript *kickSecurityPreferencesScript = [[[NSAppleScript alloc] initWithSource: @"tell application \"System Events\" to tell security preferences to set require password to wake to true"] autorelease]; 
    [kickSecurityPreferencesScript executeAndReturnError:nil]; 
} 

編輯原來,這隻適用於來自非零設置要置零。我認爲這是一個安全的事情。換個角度來說,啓動系統偏好是唯一的方法。

編輯2這裏的下水系統偏好設置代碼,你應該想這樣做:

- (void)launchAndQuitSecurityPreferences; 
{ 
    // necessary for screen saver setting changes to take effect on file-vault-enabled systems when going from a askForPasswordDelay setting of zero to a non-zero setting 
    NSAppleScript *kickSecurityPreferencesScript = [[[NSAppleScript alloc] initWithSource: 
                @"tell application \"System Preferences\"\n" 
                @"  tell anchor \"General\" of pane \"com.apple.preference.security\" to reveal\n" 
                @"  activate\n" 
                @"end tell\n" 
                @"delay 0\n" 
                @"tell application \"System Preferences\" to quit"] autorelease]; 
    [kickSecurityPreferencesScript executeAndReturnError:nil]; 
} 
相關問題