Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gnome Extensions - Run shell command

I am writing a simple extension to open browser by clicking the extension button. I would like to know if there is a function which can execute passed shell command as argument. Also, it'd be really helpful if anyone can suggest a good simple reference for extension development.

like image 752
Sam Fischer Avatar asked Apr 11 '17 22:04

Sam Fischer


People also ask

How do I start GNOME Shell?

To access GNOME Shell, sign out of your current desktop. From the login screen, click the little button next to your name to reveal the session options. Select the GNOME option in the menu and log in with your password.

How do I configure GNOME Shell extensions?

Install Gnome ExtensionsNavigate your Firefox browser to https://extensions.gnome.org/ and simply search for Gnome extensions you wish to install. Flip the ON switch to install the extension. Install extension by clicking on the ON switch. Click Install to confirm the gnome extension installation.

Where is GNOME Shell extensions?

Each GNOME Shell extension is identified by a unique identifier, the uuid. The uuid is also used for the name of the directory where an extension is installed. You can either install the extension per-user in ~/. local/share/gnome-shell/extensions/uuid , or machine-wide in /usr/share/gnome-shell/extensions/uuid .

How do I check my GNOME Shell extension?

You can determine the version of GNOME that is running on your system by going to the About panel in Settings. Open the Activities overview and start typing About. A window appears showing information about your system, including your distribution's name and the GNOME version.


1 Answers

From https://github.com/GNOME/gnome-shell/blob/master/js/misc/util.js:

// Runs @command_line in the background, handling any errors that
// occur when trying to parse or start the program.
function spawnCommandLine(command_line) {
    try {
        let [success, argv] = GLib.shell_parse_argv(command_line);
        trySpawn(argv);
    } catch (err) {
        _handleSpawnError(command_line, err);
    }
}

There are a few variations on that method in there. Save yourself mountains of headaches and just bookmark the GitHub repository.

Some quick links:

  • popupMenu.js: working with popup menus
  • panel.js: a good read for implementing "tray" icons
  • modalDialog.js: some UI elements were made to be reused, runDialog.js uses this for example
  • mpris.js: there are also good examples of using frameworks like DBus in gjs

I can't stress enough how much you'll get out of reading the gnome-shell source. Unfortunately, it's compiled into a resource file now so we don't have local copies to stumble upon.

UPDATE (2021)

If you're reading this, please instead see the documentation available on gjs.guide. Specifically the documentation on Spawning Subprocesses, which covers why this is a bad idea in extensions and how to do it slightly less bad.

like image 125
andy.holmes Avatar answered Nov 15 '22 09:11

andy.holmes