【问题标题】:Can't change dconf-entry with GSettings无法使用 GSettings 更改 dconf-entry
【发布时间】:2012-04-16 15:03:42
【问题描述】:

我目前正在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()-方法返回trueset_string()-方法也返回true

我检查了我没有处于“延迟应用”模式,并且密钥有一个GVariantType 字符串,所以set_string()-方法应该可以工作。

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

我不知道是什么问题,有什么地方可以查找日志什么的吗?

【问题讨论】:

    标签: javascript gio gnome-shell gjs


    【解决方案1】:

    在这里没有得到任何回复后,我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

    【讨论】:

    • 从昨天开始我就一直在为这个问题头疼,很高兴终于找到了解决方案。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 2020-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多