以编程方式更改屏幕保护程序设置

时间:2015-03-01 01:38:50

标签: macos swift core-foundation

我尝试以编程方式更改已配置的屏幕保护程序。

如果我这样做

$ defaults -currentHost read com.apple.screensaver
{
    CleanExit = YES;
    PrefsVersion = 100;
    idleTime = 600;
    moduleDict =     {
        moduleName = XXXX;
        path = "/Users/juanjo/Library/Screen Savers/XXXX.saver";
        type = 0;
    };
    showClock = 0;
}

我可以看到该信息,甚至可以使用write参数更新它。

此设置不能通过NSUserDefaults访问,但它们是通过CF。

我能够通过代码

来做到这一点
var moduleDict = CFPreferencesCopyAppValue("moduleDict", "com.apple.screensaver") as CFDictionary

但是当我试图从那个" dict"程序崩溃在这一行:

var saverName = CFDictionaryGetValue(moduleDict, "moduleName")

EXC_BAD_ACCESS (code=EXC_I386_GPFLT)

那么,你知道如何读取这些值并回写吗?

1 个答案:

答案 0 :(得分:3)

正如我在Cocoa-Dev列表中提到的,我能够这样做。

读:

var moduleDict = CFPreferencesCopyAppValue("moduleDict", "com.apple.screensaver") as NSDictionary
var saverName = moduleDict["moduleName"] as String!

写作:

var moduleDict = CFPreferencesCopyAppValue("moduleDict", "com.apple.screensaver") as NSDictionary
var mutable = moduleDict.mutableCopy() as NSMutableDictionary
mutable["moduleName"] = "MyScreenSaver"
mutable["path"] = mySaverPath
CFPreferencesSetValue("moduleDict", mutable as CFPropertyList, "com.apple.screensaver", kCFPreferencesCurrentUser, kCFPreferencesCurrentHost)
CFPreferencesAppSynchronize("com.apple.screensaver")