无法使用GSettings更改dconf-entry

时间:2012-04-02 23:19:41

标签: javascript gio gnome-shell gjs

我目前正在Gjs上构建一个简单的应用程序,它应该改变我的gnome-shell的背景图像。可以找到使用gsettings工具完成此操作的解决方案here

由于我想围绕它构建一个桌面应用程序,我想通过使用Gio的GSettings-class更改org.gnome.desktop.background.picture-uri - 密钥。但是使用set_X() - 方法不会更改密钥的值。

这是我更改gsettings值的代码:

var gio = imports.gi.Gio;

// Get the GSettings-object for the background-schema:
var background = new gio.Settings({schema: "org.gnome.desktop.background"});

// Read the current Background-Image:
print( "Current Background-Image: "+background.get_string("picture-uri") );

if (background.is_writable("picture-uri")){
    // Set a new Background-Image (should show up immediately):
    if (background.set_string("picture-uri", "file:///path/to/some/pic.jpg")){
        print("Success!");
    }
    else throw "Couldn't set the key!";
} else throw "The key is not writable";

读取值确实按预期工作,is_writable() - 方法返回trueset_string() - 方法也返回true

我已经检查过我没有处于“延迟应用”模式,并且密钥的字符串为GVariantType,因此set_string() - 方法应该有效。

使用普通gsettings命令行工具(如链接帖子中所述)工作正常。

我无法弄清楚问题是什么,有什么地方可以查找日志或其他东西吗?

1 个答案:

答案 0 :(得分:5)

在此处未收到任何回复后asked the same question on the gjs-mailing list

事实证明,当我的脚本退出时,对dconf的写入还没有在磁盘上,因此它们从未真正应用过。

解决方法是在set_string()函数之后立即调用g_settings_sync() functionJsDoc),以确保所有写入都已完成。

if (background.set_string("picture-uri", "file:///path/to/some/pic.jpg")){
    gio.Settings.sync()
    print("Success!");
}

感谢Johan Dahlin和his answer