Paepoi

スポンサードリンク
Paepoi » Gjs Tips » GSettings(Gjs) Tips

GSettings(Gjs) Tips

# 最終更新日 2023.11.19

GSettings 基本
動かす毎に GNOME デスクトップ時計の曜日表示の ON/OFF を切り替えます
1
2
3
4
5
6
7
8
9
#!/usr/bin/gjs -m
 
import Gio from 'gi://Gio';
 
let settings = new Gio.Settings({
    schema: 'org.gnome.desktop.interface'
});
let b = settings.get_boolean('clock-show-weekday');
settings.set_boolean('clock-show-weekday', !b);

GSettings のバリアント値
動かす毎に Eye of GNOME のプラグインを弄ります
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/gjs -m
 
import GLib from 'gi://GLib';
import Gio from 'gi://Gio';
 
let settings = new Gio.Settings({
    schema: 'org.gnome.eog.plugins'
});
// 配列化、unpack では JavaScript 配列にならない
let plugins = settings.get_value('active-plugins').deep_unpack();
print(plugins);
 
if (plugins.includes('reload')) {
    print('EoG プラグインに reload が含まれています、取り除きます');
    plugins.splice(plugins.indexOf('reload'), 1);
    // GVariant 作成、引数はオブジェクトではないので注意
    let a = new GLib.Variant('as', plugins);
    settings.set_value('active-plugins', a);
} else {
    print('EoG プラグインに reload が含まれていますせん、追加します');
    plugins.push('reload');
    let a = new GLib.Variant('as', plugins);
    settings.set_value('active-plugins', a);
}

GSettings のバインド
理解り易くするため GUI にしています
Gedit の行番号表示をこのアプリ、Gedit 自体の設定、 dconf-editor すべてで同期します
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/gjs -m
 
 
import GObject from 'gi://GObject';
import Gtk from 'gi://Gtk';
import Gio from 'gi://Gio';
 
var Win = GObject.registerClass({
    GTypeName: 'Win'
}, class Win extends Gtk.ApplicationWindow {
    _init(a) {
        super._init({
            application: a,
            title: 'Gedit 行番号表示切り替え'
        });
        this.settings = new Gio.Settings({
            schema: 'org.gnome.gedit.preferences.editor'
        });
        let button = new Gtk.CheckButton({
            label: 'Gedit の行番号を表示する',
        });
        this.settings.bind('display-line-numbers', button, 'active', 0);
        this.child = button;
    }
});
 
const app = new Gtk.Application();
app.connect('activate', ()=> {new Win(app).present();});
app.run(null);

Copyright(C) sasakima-nao All rights reserved 2002 --- 2025.