Gjs中Shell对象的使用

发布于 2024-12-23 12:45:04 字数 1071 浏览 3 评论 0原文

我正在使用 Gjs (Gnome JavaScript 绑定)编写 gtk+ 应用程序 由于没有可用的文档,我正在阅读 gnome-shell JavaScript 的源代码。 在我的应用程序中,我需要访问 global.userdatadir

我正在尝试将 Shell 对象添加到我的脚本中:

const Shell = imports.gi.Shell;

并使用 #gjs myscript.js 运行它 但是当我这样做时,它会抛出一个错误:

JS ERROR: !!!   Exception was: Error: Requiring Shell, version none: Typelib file for namespace 'Shell' (any version) not found
    JS ERROR: !!!     lineNumber = '0'
    JS ERROR: !!!     fileName = '"gjs_throw"'
    JS ERROR: !!!     stack = '"("Requiring Shell, version none: Typelib file for namespace 'Shell' (any version) not found")@gjs_throw:0
@manager.js:5
"'
    JS ERROR: !!!     message = '"Requiring Shell, version none: Typelib file for namespace 'Shell' (any version) not found"'
Error: Requiring Shell, version none: Typelib file for namespace 'Shell' (any version) not found

我无法理解它出了什么问题,它与 Gnome-shell 源文件中的完全相同。 其他对象可以使用 imports.gi.Gioimports.gi.GLib ,工作正常。

在 Ubuntu 11.10 x64 上工作

I am writing a gtk+ app using Gjs ( Gnome JavaScript bindings )
As there are no documents available i am reading the sources of gnome-shell JavaScript's .
In my app i need to get access to global.userdatadir .

I am trying to add the Shell object to my script :

const Shell = imports.gi.Shell;

and run it with #gjs myscript.js
but when i do this it throws me an error saying :

JS ERROR: !!!   Exception was: Error: Requiring Shell, version none: Typelib file for namespace 'Shell' (any version) not found
    JS ERROR: !!!     lineNumber = '0'
    JS ERROR: !!!     fileName = '"gjs_throw"'
    JS ERROR: !!!     stack = '"("Requiring Shell, version none: Typelib file for namespace 'Shell' (any version) not found")@gjs_throw:0
@manager.js:5
"'
    JS ERROR: !!!     message = '"Requiring Shell, version none: Typelib file for namespace 'Shell' (any version) not found"'
Error: Requiring Shell, version none: Typelib file for namespace 'Shell' (any version) not found

I cant understand what's wrong with it , it is exactly as in Gnome-shell source files.
Other objects are fine using imports.gi.Gio , imports.gi.GLib , works OK .

Working on Ubuntu 11.10 x64

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

还在原地等你 2024-12-30 12:45:04

您无法通过 gjs 运行 gnome-shell 扩展,它们必须由 gnome-shell 本身加载。对于开发来说,这通常意味着将它们放入 ~/.local/share/gnome-shell/extensions/YOUR-EXTENSION-ID 中并重新启动 shell。

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.

反目相谮 2024-12-30 12:45:04

通过 dbus 调用org.gnome.Shell.Eval

正如 gfxmonk 指出的那样,JavaScript 代码应该由 shell 本身运行。如果您编写扩展,则可以通过 dbus 来实现,例如使用 systemd 的 busctl。 (我确信也可以通过dbus-send,我只是更喜欢busctl的语法。并且它具有制表符补全功能!)

例如,这会记录所有窗口标题:

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

您可以使用 journalctl /usr/bin/gnome-shell 'GLIB_DOMAIN=GNOME Shell' 查看日志消息。 (您可能还想添加 -b 以便仅查看当前启动的消息,或 --since '5 分钟前',... – 请参阅 journalctl< /strong>(1) 了解更多选项。)

或者,此 GitHub 要点 描述了如何获取在gjs 中的 Shell 模块(将 /usr/lib/gnome-shell 添加到 LD_LIBRARY_PATHGIRepository.Repository 的搜索路径),但我还没有设法使用它来访问 global 对象。


请注意,从 Gnome 41 开始,调用 Eval 受到限制,因此你需要跑首先在Looking Glass (Alt+F2 lg) 中global.context.unsafe_mode = true

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.


Note that since Gnome 41, calling Eval is restricted, so you’ll need to run global.context.unsafe_mode = true in Looking Glass (Alt+F2 lg) first.

情归归情 2024-12-30 12:45:04
$ 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
$ 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文