【问题标题】:How to get OS name while writing gnome-extensions如何在编写 gnome-extensions 时获取操作系统名称
【发布时间】:2020-11-07 06:35:25
【问题描述】:

如何在编写 gnome-extensions 时获取操作系统名称..

例如:

GLib.get_real_name()

我已经浏览过这篇帖子How can my GNOME Shell extension detect the GNOME version?

【问题讨论】:

  • 你所说的“操作系统名称”到底是什么意思?分发名称、主机名或...?

标签: gnome-shell-extensions


【解决方案1】:

如果获取在/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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-15
    • 1970-01-01
    • 2016-02-01
    • 1970-01-01
    • 2015-05-25
    • 1970-01-01
    • 1970-01-01
    • 2019-01-19
    相关资源
    最近更新 更多