To make your GNOME Shell extension run a shell command when the enable() method is called, you need to use GNOME Shell's utilities to execute shell commands. The most common way is to use the Gio.Subprocess or GLib.spawn_command_line_async functions.
Here’s how you can modify your enable() method to run a shell command:
GLib.spawn_command_line_asyncThis is the simplest way to run a shell command asynchronously:
javascriptimport GLib from 'gi://GLib'; export default class QuickSettingsExampleExtension extends Extension { enable() { // Run your shell command here GLib.spawn_command_line_async('your-shell-command-here'); // Rest of your enable code this._indicator = new ExampleIndicator(); Main.panel.statusArea.quickSettings.addExternalIndicator(this._indicator); } ... }
Example:
If you want to run notify-send "Hello, World!", you would do:
javascriptGLib.spawn_command_line_async('notify-send "Hello, World!"');
Gio.Subprocess (more control)If you need more control (e.g., handling output, errors, or waiting for completion), use Gio.Subprocess:
javascriptimport Gio from 'gi://Gio'; export default class QuickSettingsExampleExtension extends Extension { enable() { let proc = new Gio.Subprocess({ argv: ['/bin/sh', '-c', 'your-shell-command-here'], flags: Gio.SubprocessFlags.NONE, }); proc.init(null); // Rest of your enable code this._indicator = new ExampleIndicator(); Main.panel.statusArea.quickSettings.addExternalIndicator(this._indicator); } ... }
Example:
javascriptlet proc = new Gio.Subprocess({ argv: ['/bin/sh', '-c', 'notify-send "Hello, World!"'], flags: Gio.SubprocessFlags.NONE, }); proc.init(null);
'your-shell-command-here' with the actual command you want to run.pkexec or another privilege escalation method.GLib.spawn_command_line_sync instead, but be aware that this can freeze the UI if the command takes too long.journalctl /usr/bin/gnome-shell for errors if the command doesn’t run as expected.$PATH or use the full path to the binary.Would you like help with a specific command or debugging an issue?