Gjs中Shell对象的使用
我正在使用 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.Gio
, imports.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法通过 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.通过 dbus 调用
org.gnome.Shell.Eval
。正如 gfxmonk 指出的那样,JavaScript 代码应该由 shell 本身运行。如果您不编写扩展,则可以通过 dbus 来实现,例如使用 systemd 的
busctl
。 (我确信也可以通过dbus-send
,我只是更喜欢busctl
的语法。并且它具有制表符补全功能!)例如,这会记录所有窗口标题:
您可以使用
journalctl /usr/bin/gnome-shell 'GLIB_DOMAIN=GNOME Shell'
查看日志消息。 (您可能还想添加-b
以便仅查看当前启动的消息,或--since '5 分钟前'
,... – 请参阅 journalctl< /strong>(1) 了解更多选项。)或者,此 GitHub 要点 描述了如何获取在
gjs
中的Shell
模块(将/usr/lib/gnome-shell
添加到LD_LIBRARY_PATH
和GIRepository.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 viadbus-send
, I just preferbusctl
’s syntax. And it has tab completion!)For example, this logs all window titles:
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 ingjs
(add/usr/lib/gnome-shell
toLD_LIBRARY_PATH
and toGIRepository.Repository
’s search path), but I haven’t managed to get access to aglobal
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.