我认为应该使用D-Bus。基本上,我想要这样的东西- https://wiki.gnome.org/Gjs/Examples/DBusClient -但反过来。
在扩展中,有一个函数:
function f(s) { doSomethingWithS; }
这个函数将在运行后被调用:
$ <something> "abc"
…在终端,与s == "abc"
.
根据@Jasper和@owen在#gnome-shell
上对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"
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'