-
-
Save buzztaiki/1492431 to your computer and use it in GitHub Desktop.
#!/usr/bin/gjs | |
// SPDX-License-Identifier: MIT | |
// usage: gjs shell_object_sample.js | |
const { GIRepository, GLib, Gio } = imports.gi; | |
function findLib(path, prefix) { | |
const libdir = Gio.File.new_for_path(path); | |
const files = libdir.enumerate_children('', Gio.FileQueryInfoFlags.NONE, null); | |
for (; ;) { | |
const finfo = files.next_file(null); | |
if (!finfo) break; | |
if (finfo.get_file_type() == Gio.FileType.DIRECTORY && finfo.get_name().startsWith(prefix)) { | |
return GLib.build_filenamev([path, finfo.get_name()]); | |
} | |
} | |
return null; | |
} | |
GIRepository.Repository.prepend_library_path('/usr/lib64/gnome-shell') | |
GIRepository.Repository.prepend_search_path('/usr/lib64/gnome-shell'); | |
GIRepository.Repository.prepend_library_path(findLib('/usr/lib64', 'mutter-')); | |
GIRepository.Repository.prepend_search_path(findLib('/usr/lib64', 'mutter-')); | |
const { Shell } = imports.gi; | |
const appSys = Shell.AppSystem.get_default(); | |
print(appSys.get_installed().map(x => x.get_name()).join('\n')); |
I think you should add /usr/lib64/mutter-9
to search path instead of /usr/lib64/mutter-5
for your environment.
Update to a generic version.
Thanks for the update.
We can load librarys directly by GIRepository.Repository.prepend_library_path()
instead of LD_LIBRARY_PATH
enviroment variable. There is a example, works with gnome-shell 42.3
#!/usr/bin/gjs
// chmod +x ./shell_object_sample.js && ./shell_object_sample.js
const res = imports.gi.GIRepository.Repository
// load library of Mutter project
res.prepend_search_path('/usr/lib/mutter-10')
res.prepend_library_path('/usr/lib/mutter-10')
// load library of gnome-shell project
res.prepend_search_path('/usr/lib/gnome-shell')
res.prepend_library_path('/usr/lib/gnome-shell')
const Shell = imports.gi.Shell;
const appSys = Shell.AppSystem.get_default();
print(appSys.get_installed().map(x => x.get_name()).join('\n'));
Looks really good. Updated.
Just tried your code.
Errors throw on my machine:
Gjs-CRITICAL **: JS ERROR: Error: Requiring Shell, version none: Typelib file for namespace 'Shell' (any version) not found
getApps.js:15:15
Gjs-CRITICAL **: Script getApps.js threw an exception
The line 15 is const Shell = imports.gi.Shell;
and I use gnome-shell 42.2.
@nlpsuge , It should works if you replace /usr/lib
with /usr/lib64
. I just test it in Fedora36 with gnome-shell 42.2.
Debian 12:
#!/usr/bin/gjs
// chmod +x ./shell_object_sample.js && ./shell_object_sample.js
const res = imports.gi.GIRepository.Repository
// load library of Mutter project
res.prepend_search_path('/usr/lib/x86_64-linux-gnu/mutter-11')
res.prepend_library_path('/usr/lib/x86_64-linux-gnu/mutter-11')
// load library of gnome-shell project
res.prepend_search_path('/usr/lib/gnome-shell')
res.prepend_library_path('/usr/lib/gnome-shell')
const Shell = imports.gi.Shell;
const appSys = Shell.AppSystem.get_default();
print(appSys.get_installed().map(x => x.get_name()).join('\n'));
Thank you very much.
If I replace get_installed
with get_running
then there are not output. why?
If I replace
get_installed
withget_running
then there are not output. why?
Do you have any apps with at least one window running while you run your script?
Yes. I opened many apps including gnome specific apps. Nothing.
Running Shell.AppSystem.get_default().get_running().map(x => x.get_name()).join('\n')
in lg
(alt+F2 then type lg
), is there output?
The output is r(0) = Firefox Alacritty ....
. I mean the command works if i run this way.
Not work for me.
GNOME Shell 41.1
(gjs:27581): Gjs-CRITICAL **: 10:56:04.899: JS ERROR: Error: Requiring Shell, version none: Typelib file for namespace 'Meta', version '9' not found
@test.js:6:15
(gjs:27581): Gjs-CRITICAL **: 10:56:04.899: Script test.js threw an exception