3

我认为应该使用 D-Bus。基本上,我想要这样的东西——https: //wiki.gnome.org/Gjs/Examples/DBusClient——但反过来。

在扩展中,将有一个功能:

function f(s) { doSomethingWithS; }

运行后将调用此函数:

$ <something> "abc"

… 在终端中,带有s == "abc".


根据@Jasper 和@owen at #gnome-shellon 的建议irc.gnome.org,我改编了一些来自https://github.com/GNOME/gnome-shell/blob/master/js/ui/magnifierDBus.js的代码:

const St = imports.gi.St;
const Gio = imports.gi.Gio;
const Lang = imports.lang;
const Main = imports.ui.main;

let text;

function init() {
    text = new St.Label({ text: "0:0", style_class: 'panel-text' });
}

function enable() {
    Main.panel._rightBox.insert_child_at_index(text, 0);
}

function disable() {
    Main.panel._rightBox.remove_child(text);
}

const TextInTaskBarIface = '<node> \
<interface name="com.michalrus.TextInTaskBar"> \
<method name="setText"> \
    <arg type="s" direction="in" /> \
</method> \
</interface> \
</node>';

const TextInTaskBar = new Lang.Class({
    Name: 'TextInTaskBar',

    _init: function() {
        this._dbusImpl = Gio.DBusExportedObject.wrapJSObject(TextInTaskBarIface, this);
        this._dbusImpl.export(Gio.DBus.session, '/com/michalrus/TextInTaskBar');
    },

    setText: function(str) {
        text.text = str;
    }
});

现在,发出后:

% dbus-send --dest=com.michalrus.TextInTaskBar /com/michalrus/TextInTaskBar \
    com.michalrus.TextInTaskBar.setText string:"123"

… 什么都没发生。

4

1 回答 1

7

gnome-shell 扩展 D-Bus 服务器的最终工作版本:

const St = imports.gi.St;
const Gio = imports.gi.Gio;
const Lang = imports.lang;
const Main = imports.ui.main;

let text = null;
let textDBusService = null;

function init() {
    text = new St.Label({ text: "0:0", style_class: 'panel-text' });
    textDBusService = new TextInTaskBar();
}

function enable() {
    Main.panel._rightBox.insert_child_at_index(text, 0);
}

function disable() {
    Main.panel._rightBox.remove_child(text);
}

const TextInTaskBarIface = '<node> \
<interface name="com.michalrus.TextInTaskBar"> \
<method name="setText"> \
    <arg type="s" direction="in" /> \
</method> \
</interface> \
</node>';

const TextInTaskBar = new Lang.Class({
    Name: 'TextInTaskBar',

    _init: function() {
    text.text = "abc";
        this._dbusImpl = Gio.DBusExportedObject.wrapJSObject(TextInTaskBarIface, this);
        this._dbusImpl.export(Gio.DBus.session, '/com/michalrus/TextInTaskBar');
    },

    setText: function(str) {
    text.text = str;
    }
});

致电:

$ gdbus call --session --dest org.gnome.Shell --object-path /com/michalrus/TextInTaskBar --method com.michalrus.TextInTaskBar.setText 'some text'
于 2015-10-07T21:13:49.287 回答