【问题标题】:Programmatically changing screen lock timeout以编程方式更改屏幕锁定超时
【发布时间】:2012-05-23 04:54:33
【问题描述】:

在我的 Cocoa 应用程序中,我想访问和更改计算机的屏幕锁定超时设置。在系统偏好设置中更改它不需要用户输入管理员密码。

很遗憾,我在文档中找不到任何信息,而且我不确定应该研究什么主题(安全设置/prefPane 编程)。
任何帮助将不胜感激。

【问题讨论】:

  • 你是想改变值还是想阻止睡眠?
  • @CRD 尝试更改值

标签: objective-c cocoa system-preferences


【解决方案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

【讨论】:

  • 我认为至少在 OS X 10.9 上不会立即生效。睡眠后,操作系统仍使用以前的值而不是新值。看起来它需要一些触发器。
【解决方案2】:

上述答案显然适用于某些人,但在 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];
}

【讨论】:

    猜你喜欢
    • 2012-05-31
    • 1970-01-01
    • 2019-07-22
    • 1970-01-01
    • 1970-01-01
    • 2012-12-13
    • 2011-02-28
    • 1970-01-01
    • 2011-09-08
    相关资源
    最近更新 更多