3

I think D-Bus should be used. Basically, I want something like that — https://wiki.gnome.org/Gjs/Examples/DBusClient — but the other way round.

In the extension, there would be a function:

function f(s) { doSomethingWithS; }

And this function would be called after running:

$ <something> "abc"

… in the terminal, with s == "abc".


After suggestions from @Jasper and @owen at #gnome-shell on irc.gnome.org, I adapted some code from 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;
    }
});

Now, after issuing:

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

… nothing happens.

Michal Rus
  • 1,796
  • 2
  • 18
  • 27

1 Answers1

8

Final, working version of a gnome-shell extension D-Bus server:

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

Call with:

$ gdbus call --session --dest org.gnome.Shell --object-path /com/michalrus/TextInTaskBar --method com.michalrus.TextInTaskBar.setText 'some text'
Michal Rus
  • 1,796
  • 2
  • 18
  • 27