How to get OS name while writing gnome-extensions..
for example:
GLib.get_real_name()
I have gone through this post How can my GNOME Shell extension detect the GNOME version?
In case of getting the operating system name as found in /etc/os-release
, this not particularly related to GJS or extensions.
You could just open the /etc/os-release
file directly, but since GKeyFile
is not introspectable in GJS you would have to parse it manually. Alternatively you could use the org.freedesktop.hostname1
DBus interface to get the "pretty name", although I don't know if that's guaranteed to be available on all distributions.
const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;
let osName = 'Unknown';
try {
// NOTE: this is a synchronous call that will block the main thread
// until it completes. Using `Gio.DBus.system.call()` would be
// better, but I don't know if that works for your use case.
let reply = Gio.DBus.system.call_sync(
'org.freedesktop.hostname1',
'/org/freedesktop/hostname1',
'org.freedesktop.DBus.Properties',
'Get',
new GLib.Variant('(ss)', [
'org.freedesktop.hostname1',
'OperatingSystemPrettyName'
]),
null,
Gio.DBusCallFlags.NONE,
-1,
null
);
let value = reply.deep_unpack()[0];
osName = value.unpack();
} catch (e) {
logError(e, 'Fetching OS name');
}
// Example Output: "Fedora 32 (Workstation Edition)" or "Unknown" on failure
log(osName);