Ubuntu – How to hide Empathy’s mail indicator icon

empathyindicator

Since I have configured Empathy, I have a new icon shown in the indicator icon bar. This icon remains visible even if I close Empathy.

How can I have this icon disappear when not using Empathy?

enter image description here

Best Answer

Inspired by cl-netbox' answer, you can automatically remove the icon after empathy closes. This can be done by running (starting) empathy through a wrapper script. As far as I can see, this has no downside. The only possible downside would be that the right- click option to open with does not work, but that does not apply to empathy.

The wrapper would only run during, and only as long as, empathy is running, waiting for it to close, so the solution is very specific.

What it does

  • When empathy is run (through the wrapper), a small script is started, doing two things:

    • look (check) for the empathy indicator to be visible, by the command:

      gsettings get com.canonical.indicator.messages applications
      

      This will return a list of current indicators. If the empathy indicator is not included, the script adds it to the list and sets the altered list by the command:

      gsettings set com.canonical.indicator.messages applications <newlist>
      
    • Then the script looks for the existence of an empathy window
      If no empathy window exists any more, the script fetches the list of indicators again and removes the indicator in the same way.

  • Then the wrapper- script terminates itself, and your icon is removed from the panel.

The wrapper

#!/usr/bin/env python3
import subprocess
import time

key = "com.canonical.indicator.messages"

def hide_icon(icon, mode):
    # function to remove the targeted icon from the list
    current = eval(subprocess.check_output([
        "gsettings", "get", key, "applications"
        ]).decode("utf-8").strip())
    if mode == "h":
        try:
            current.remove(icon)
        except ValueError:
            pass
    elif mode == "s":
        if not icon in current:
            current.append(icon)
    subprocess.call([
        "gsettings", "set", key, "applications", str(current)
        ])

# run empathy
subprocess.Popen(["empathy"])
# make sure the icon shows
hide_icon('empathy.desktop', "s")

while True:
    time.sleep(3)
    try:
        # get the pid of empathy
        pid = subprocess.check_output(["pgrep", "empathy"]).decode("utf-8").strip()
    except subprocess.CalledProcessError:
        break
    else:
        try:
            # see if the pid of empathy is still in the window list...
            wlist = subprocess.check_output(["wmctrl", "-lp"]).decode("utf-8")
            # ...if not, remove the icon from the list and break
            if not pid in wlist:
                hide_icon('empathy.desktop', "h")
                break
        except subprocess.CalledProcessError:
            pass

How to set up

  • Copy the script into an empty file, save it as no_indicator.py

  • Test-run the script with the command:

    python3 /path/to/no_indicator.py
    

    The icon should disappear if you close empathy. If all works fine:

  • Copy the global empathy.desktop file to ~/.local/share/applications:

    cp /usr/share/applications/empathy.desktop ~/.local/share/applications
    
  • Open the locally copied file with gedit:

    gedit ~/.local/share/applications/empathy.desktop
    

    replace the line:

    Exec=empathy
    

    by:

    Exec=python3 /path/to/no_indicator.py
    
  • just before (above) the line, starting with Actions=, insert the line:

    StartupWMClass=empathy
    

    This is to prevent an extra icon in the Launcher when you run empathy

  • Log out and back in