我目前正在 Gjs 上构建一个简单的应用程序,这应该会更改我的 gnome-shell 的背景图像。可以找到关于如何使用 gsettings
工具完成此操作的解决方案 here .
因为我想围绕它构建一个桌面应用程序,所以我想使用 Gio 的 GSettings
-class 更改 org.gnome.desktop.background.picture-uri
-key .但是使用 set_X()
方法不会更改 key 的值。
这是我更改 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()
方法返回 true
并且 set_string()
方法也返回 真
。
我已经检查过我没有处于“延迟应用”模式并且该键具有字符串的 GVariantType
,因此 set_string()
方法应该有效.
使用普通的 gsettings
命令行工具(如链接帖子中所述)工作得很好。
我不知道是什么问题,有什么地方可以找日志之类的吗?
最佳答案
在这里没有得到任何回应后,我 asked the same question on the gjs-mailing list .
事实证明,当我的脚本退出时,对 dconf 的写入尚未在磁盘上,因此它们从未真正应用过。
解决方案是调用 g_settings_sync()
function ( JsDoc ) 紧跟在 set_string()
函数之后,以确保所有写入都已完成。
if (background.set_string("picture-uri", "file:///path/to/some/pic.jpg")){
gio.Settings.sync()
print("Success!");
}
感谢 Johan Dahlin 和 his answer .
关于javascript - 无法使用 GSettings 更改 dconf-entry,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9985140/