2012-04-02 28 views
4

我目前在Gjs上構建了一個簡單的應用程序,它將更改我的gnome-shell的背景圖像。有關如何使用gsettings-tool可以完成的解決方案可以在here找到。無法用GSettings更改dconf-entry

由於我想圍繞它構建一個桌面應用程序,因此我想通過使用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"; 

讀值不按預期方式工作, - 方法返回trueset_string() - 方法也返回trueis_writable()

我檢查過我沒有處於「延遲應用」模式,並且該鍵有字符串GVariantType,所以set_string()-方法應該可以工作。

使用正常的gsettings命令行工具(如鏈接文章中所述)工作得很好。

我找不出什麼問題,有沒有地方可以查找日誌或什麼?

回答

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!"); 
} 

感謝約翰·達林和his answer

+0

自昨天以來,我一直在爲這個問題而頭痛,並且很高興終於找到了解決方案。謝謝! – Serrano 2013-09-02 17:00:17