Use of Shell object in Gjs

落爺英雄遲暮 提交于 2019-12-03 12:33:01

You can't run gnome-shell extensions via gjs, they have to be loaded by gnome-shell itself. For development, this usually means putting them in ~/.local/share/gnome-shell/extensions/YOUR-EXTENSION-ID and restarting the shell.

$ apt-file search -x "Shell.*typelib"
gnome-shell: /usr/lib/gnome-shell/Shell-0.1.typelib
gnome-shell: /usr/lib/gnome-shell/ShellJS-0.1.typelib
gnome-shell: /usr/lib/gnome-shell/ShellMenu-0.1.typelib

$ sudo apt-get install gnome-shell

Call org.gnome.Shell.Eval via dbus.

As gfxmonk points out, the JavaScript code should be run by the shell itself. If you’re not writing an extension, the way to do that is via dbus, for instance using systemd’s busctl. (I’m sure it’s also possible via dbus-send, I just prefer busctl’s syntax. And it has tab completion!)

For example, this logs all window titles:

busctl --user call org.gnome.Shell /org/gnome/Shell org.gnome.Shell Eval s '
    for (const actor of global.get_window_actors()) {
        const window = actor.get_meta_window(),
              title = window.get_title();
        log(title);
    }
'

You can see the log messages with journalctl /usr/bin/gnome-shell 'GLIB_DOMAIN=GNOME Shell'. (You probably want to add -b too to only see messages from the current boot, or --since '5 minutes ago', … – see journalctl(1) for more options.)

Alternatively, this GitHub gist describes how to get at the Shell module in gjs (add /usr/lib/gnome-shell to LD_LIBRARY_PATH and to GIRepository.Repository’s search path), but I haven’t managed to get access to a global object using that.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!