我目前正在Gjs上构建一个简单的应用程序,它应该会改变我的gnome外壳的背景图像。有关如何使用gsettings-tool完成此操作的解决方案,可以在here中找到。
因为我想围绕它构建一个桌面应用程序,所以我想使用Gio的GSettings-class来更改org.gnome.desktop.background.picture-uri-key。但使用set_X()-method不会更改密钥的值。
这是我用来更改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()-method返回true,set_string()-method也返回true。
我已经检查到我没有处于"delay-apply“模式,并且密钥的GVariantType为string,所以set_string()-method应该可以工作。
使用普通的gsettings命令行工具(如链接的文章中所述)工作得很好。
我不知道问题出在哪里,有没有地方可以找到日志之类的?
发布于 2012-04-03 22:57:44
在这里没有得到任何回应后,我使用asked the same question on the gjs-mailing list。
结果是,当我的脚本退出时,对dconf的写入还不在磁盘上,所以它们从未真正应用过。
解决方案是在set_string()函数之后立即调用g_settings_sync() function (JsDoc),以确保所有写入都已完成。
if (background.set_string("picture-uri", "file:///path/to/some/pic.jpg")){
gio.Settings.sync()
print("Success!");
}感谢Johan Dahlin和his answer。
https://stackoverflow.com/questions/9985140
复制相似问题