Skip to content

Instantly share code, notes, and snippets.

@buzztaiki
Last active July 20, 2023 06:13
Show Gist options
  • Save buzztaiki/1492431 to your computer and use it in GitHub Desktop.
Save buzztaiki/1492431 to your computer and use it in GitHub Desktop.
Using shell object in gjs
#!/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'));
@blueray453
Copy link

If I replace get_installed with get_running then there are not output. why?

@nlpsuge
Copy link

nlpsuge commented Jul 15, 2023

If I replace get_installed with get_running then there are not output. why?

Do you have any apps with at least one window running while you run your script?

@blueray453
Copy link

Yes. I opened many apps including gnome specific apps. Nothing.

@nlpsuge
Copy link

nlpsuge commented Jul 19, 2023

Running Shell.AppSystem.get_default().get_running().map(x => x.get_name()).join('\n') in lg (alt+F2 then type lg), is there output?

@blueray453
Copy link

The output is r(0) = Firefox Alacritty ..... I mean the command works if i run this way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment