11

I have written a simple shell script to accomplish a common task, and I want to be able to run it whenever a button is clicked. I've used gnome-shell-extension-tool to create the Hello World example already, but now I need to know how to simply have it run an arbitrary command when clicked. There is no input or output to be concerned with; it just needs to run.

Jorenko
  • 2,594
  • 2
  • 21
  • 26

2 Answers2

14

After some more creative googling, I've found the solution:

const Util = imports.misc.util;
Util.spawn(['/path/to/program', 'arg1', 'arg2'])
Jorenko
  • 2,594
  • 2
  • 21
  • 26
  • Just a hint if anyone wants to execute a bash command with pipes. Use the whole command as `arg2` so the second line should look like this: `Util.spawn(['/bin/bash', '-c', "command 1 | command 2'"])` (Credits goes to Xavier Combelle: https://stackoverflow.com/a/33974011/2056125) – mhellmeier Sep 25 '19 at 23:23
  • Hey, it's a bit late i know but would you please tell me what is the difference between GLib.spawn_.. and misc.util.spawn ? Thanks!! – Murad Alm. Apr 04 '20 at 16:03
  • 1
    And how can i get the output of the command using misc.util.spawn? Thanks!! – Murad Alm. Apr 04 '20 at 16:17
5
const GLib = imports.gi.GLib;
let stuff = GLib.spawn_command_line_sync("cat hello.txt")[1].toString();

For those looking to read the output of the command, use this. The default working directory for Gnome shell extensions is the user's home directory.

Just thought I'd mention these things, because it took me a while to figure them out.

rusins
  • 309
  • 4
  • 8
  • 1
    Thanks, that one working in prefs.js (Util couldn't be imported, I don't know why, it's really such a pain without a good reference handbook). – Edvard Rejthar Nov 09 '17 at 13:00