1

How would I go about making an indicator for the panel on Unity? I want a button that shows a menu and when you click on the menu items they run commands.

How do I do this?

marked as duplicate by Community Nov 28 '16 at 9:39

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    This might interest you: askubuntu.com/a/853065/72216 – Jacob Vlijm Nov 28 '16 at 6:59
  • Wait, I'll make an edited version of the answer for this one. Someone else will not easily find that answer as a match to this question, and I can make it more elegant, specific to this question. – Jacob Vlijm Nov 28 '16 at 7:39
  • Hi Christian, posted a slightly edited one, specifically to run commands rather than scripts. Another difference it that naming in this case may include spaces. – Jacob Vlijm Nov 28 '16 at 8:06
  • I wrote indicator-script-runner for that purpose. – Bernmeister Nov 28 '16 at 8:22
1

An indicator, running various commands

The answer below is an edited version of this one. Since this question is quite different from that one, decided to specify the answer there to serve in a flexible way to this question, reading both (menu) names and commands from a text file.

enter image description here

The solution

Is an indicator, reading lines from a file, named commands, in which both the name and the command are defined, one set per line. Name (as it appears in the menu) and command are separated by ||. An example:

Gedit || gedit 
Go Home || nautilus $HOME

The script

#!/usr/bin/env python3
import subprocess
import os
import signal
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('AppIndicator3', '0.1')
from gi.repository import Gtk, AppIndicator3

currpath = os.path.dirname(os.path.realpath(__file__))

class Indicator():
    def __init__(self):
        self.app = 'update_setting'
        iconpath = currpath+"/icon.png"
        self.indicator = AppIndicator3.Indicator.new(
            self.app, iconpath,
            AppIndicator3.IndicatorCategory.SYSTEM_SERVICES)
        self.indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE)       
        self.indicator.set_menu(self.create_menu())

    def getscripts(self):
        cmd_data = [l for l in open(
            os.path.join(currpath, "commands")
            ).read().splitlines()]
        cmd_data = [l.split("||") for l in cmd_data]  
        for cmd in cmd_data:
            menuitem = Gtk.MenuItem(cmd[0].strip())
            menuitem.connect("activate", self.run_script, cmd[1].strip())
            self.menu.append(menuitem)

    def create_menu(self):
        self.menu = Gtk.Menu()
        self.getscripts()
        # quit
        item_quit = Gtk.MenuItem('Quit')
        sep = Gtk.SeparatorMenuItem()
        self.menu.append(sep)
        item_quit.connect('activate', self.stop)
        self.menu.append(item_quit)
        self.menu.show_all()
        return self.menu

    def run_script(self, widget, script):
        subprocess.Popen(["/bin/bash", "-c", script])

    def stop(self, source):
        Gtk.main_quit()

Indicator()
signal.signal(signal.SIGINT, signal.SIG_DFL)
Gtk.main()

How to use

  1. Copy the script above into an empty file, save it as run_commands.py
  2. Copy the icon below and save it as (exactly) icon.png in one and the same folder as the script.

    enter image description here

  3. Create a text file, also in one and the same folder, (exactly) named: commands (no extension).
  4. Populate the file with your commands and their names, like:

    Gedit || gedit 
    Go Home || nautilus $HOME
    

    Both the names may include spaces and the commands may include arguments.

  5. Run the indicator by the command:

    python3 /path/to/run_commands.py
    

Not the answer you're looking for? Browse other questions tagged or ask your own question.