How to run a shell command when the following GNOME extension button is pressed?

```javascript
import GObject from 'gi://GObject';
import Gio from 'gi://Gio';

import * as Main from 'resource:///org/gnome/shell/ui/main.js';

import {Extension, gettext as _} from 'resource:///org/gnome/shell/extensions/extension.js';
import {QuickToggle, SystemIndicator} from 'resource:///org/gnome/shell/ui/quickSettings.js';

const ExampleToggle = GObject.registerClass(
class ExampleToggle extends QuickToggle {
constructor() {
super({
title: _('Split VPN'),
iconName: 'network-vpn-symbolic',
toggleMode: true,
});
}
});

const ExampleIndicator = GObject.registerClass(
class ExampleIndicator extends SystemIndicator {
constructor() {
super();

this._indicator = this._addIndicator();
this._indicator.iconName = 'network-vpn-symbolic';

const toggle = new ExampleToggle();
toggle.bind_property('checked',
this._indicator, 'visible',
GObject.BindingFlags.SYNC_CREATE);
this.quickSettingsItems.push(toggle);
}
});

export default class QuickSettingsExampleExtension extends Extension {
async enable() {
this._indicator = new ExampleIndicator();
Main.panel.statusArea.quickSettings.addExternalIndicator(this._indicator);
}

disable() {
this._indicator.quickSettingsItems.forEach(item => item.destroy());
this._indicator.destroy();
}
}
```
Feb 10, 11:45pm

To run a shell command when your GNOME extension's button is pressed, you need to connect a signal handler to the toggled signal of your QuickToggle class. This signal is emitted when the toggle button is clicked. Inside the handler, you can use Gio.Subprocess or GLib.spawn_command_line_async to execute your shell command.

Here’s how you can modify your code to achieve this:


Updated Code Example

javascript
import GObject from 'gi://GObject';
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';

import * as Main from 'resource:///org/gnome/shell/ui/main.js';

import {Extension, gettext as _} from 'resource:///org/gnome/shell/extensions/extension.js';
import {QuickToggle, SystemIndicator} from 'resource:///org/gnome/shell/ui/quickSettings.js';

const ExampleToggle = GObject.registerClass(
class ExampleToggle extends QuickToggle {
    _init() {
        super._init({
            title: _('Split VPN'),
            iconName: 'network-vpn-symbolic',
            toggleMode: true,
        });

        // Connect the 'toggled' signal to your handler
        this.connect('toggled', () => this._onToggled());
    }

    _onToggled() {
        // Replace with your shell command
        const command = 'your-shell-command-here';

        try {
            // Run the command asynchronously
            GLib.spawn_command_line_async(command);
            log(`Command executed: ${command}`);
        } catch (e) {
            logError(e, `Failed to execute command: ${command}`);
        }
    }
});

const ExampleIndicator = GObject.registerClass(
class ExampleIndicator extends SystemIndicator {
    _init() {
        super._init();

        this._indicator = this._addIndicator();
        this._indicator.iconName = 'network-vpn-symbolic';

        const toggle = new ExampleToggle();
        toggle.bind_property('checked',
            this._indicator, 'visible',
            GObject.BindingFlags.SYNC_CREATE);
        this.quickSettingsItems.push(toggle);
    }
});

export default class QuickSettingsExampleExtension extends Extension {
    enable() {
        this._indicator = new ExampleIndicator();
        Main.panel.statusArea.quickSettings.addExternalIndicator(this._indicator);
    }

    disable() {
        this._indicator.quickSettingsItems.forEach(item => item.destroy());
        this._indicator.destroy();
        this._indicator = null;
    }
}

Key Changes and Explanations

  • Signal Connection: The toggled signal is connected to the _onToggled method in the ExampleToggle class. This method is called every time the toggle button is pressed.
  • Command Execution: Inside _onToggled, GLib.spawn_command_line_async is used to run the shell command asynchronously. Replace 'your-shell-command-here' with the actual command you want to run.
  • Error Handling: Basic error handling is included to log any issues that might occur when running the command.

Notes

  • Permissions: Ensure your extension has the necessary permissions to run shell commands. GNOME Shell extensions run in a sandboxed environment, and some commands may require elevated privileges or specific permissions.
  • Debugging: Use log() and logError() to debug and check if the command is being executed as expected.
  • Security: Be cautious when executing shell commands, especially if the command includes user input or external data, to avoid security risks.

Feb 10, 11:45pm

This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.