如何在编写 gnome-extensions 时获取 OS 名称

How to get OS name while writing gnome-extensions

如何在编写 gnome-extensions 时获取 OS 名称..

例如:

GLib.get_real_name()

我经历过这个post

在获取 /etc/os-release 中找到的操作系统名称的情况下,这与 GJS 或扩展没有特别的关系。

您可以直接打开 /etc/os-release 文件,但由于 GKeyFile 在 GJS 中不可自省,您必须手动解析它。或者,您可以使用 org.freedesktop.hostname1 DBus 接口来获取“漂亮的名字”,尽管我不知道这是否保证在所有发行版中都可用。

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