How to make an indicator to run various commands? [duplicate]

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?

Answer

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
    

Attribution
Source : Link , Question Author : cheesits456 , Answer Author : Community

Leave a Comment