Ubuntu – How to add a custom item to the Sound Indicator (and make it clickable more than once)

12.04customizationdbusindicator-sound

The original question

Sound indicator

One of the strength of Unity are the various standardized indicators. I want to customize the sound indicator with an additional menu entry that runs a small shell script. I'm not afraid of a little Python code and I hope someone can point me to the right subroutine in the right file. I suspect that will be fairly easy but all the indicators are just so bloated that I can't look through their code in a reasonable time.

Any help is appreciated. I know it is possible as the marvelous Skype-Wrapper does it.


Edit 2 – Now a dirty DBus hack

The one click problem from one edit before has now turned into a DBus problem. Basically we have to tell the sound indicator that our bogus player has terminated now. A dirty hack navigates around that problem:

#!/bin/bash
# This is '/home/confus/bin/toggleSpeaker.sh'

notify-send "Toggle Speaker" "$(date)"
qdbus \
    com.canonical.indicator.sound  \
    /org/ayatana/indicator/service \
    org.ayatana.indicator.service.Shutdown
exit 0

Help from the community is appreciated as I don't have experience any with DBus whatsoever.


Edit 1 – Takkat found a solution but only clickable once?

For some reason the solution proposed by Takkat has the drawback that the resulting entry in indicator sound can only be clicked once per session. If someone has a fix for, than please comment or answer, you will be upvoted. Here you can see the result:

Sound indicator with script entry

I strongly suspect the issue is related to the .desktop-file in /home/confus/.local/share/application/toggleSpeaker.desktop, which is this:

[Desktop Entry]
Type=Application

Name=toggleSpeaker
GenericName=Toggle Speaker

Icon=gstreamer-properties

Exec=/home/confus/bin/toggleSpeaker.sh
Terminal=false

And here is a minimal example of the script in /home/confus/bin/toggleSpeaker.sh for your consideration:

#!/bin/bash
# This is '/home/confus/bin/toggleSpeaker.sh'

notify-send "Toggle Speaker" "$(date)"
exit 0

Best Answer

By running dconf-editor from the dconf-tools Install dconf-tools we may incorporate an MPRIS complatible media player application to the sound menu with the key "com.canonical.indicator.sound.interested-media-players".

enter image description here

In the example here test was included which points to a test.desktop file in ~/.local/share/applications where we can run our appplication from the Exec= field. Restart your session to take effect.

enter image description here

As an entry in this menu is closely related to a media player, application control is done by DBus session bus com.canonical.indicator.sound. This is why we will not be able to re-run a script until a reset of the indicator was done. I succeeded to restart the test application after performing Shutdown() on the org/ayatana/indicator/service object but this is quite a hack and definitely not the way the indicator sound menu should be used.

It may however point to the right direction. i.e. we need to make use of DBus for our script or application we want to reside in the sound indicator menu.