Skip to content

Instantly share code, notes, and snippets.

@yrps
Created July 31, 2016 04:47
Show Gist options
  • Save yrps/7979f415a7c1c2ec7d2b19f96599eb6a to your computer and use it in GitHub Desktop.
Save yrps/7979f415a7c1c2ec7d2b19f96599eb6a to your computer and use it in GitHub Desktop.
ncmpcpp: execute_on_song_change
#!/usr/bin/env python
"""
Get a notification summary and body from mpc, then send it through dbus.
https://developer.gnome.org/notification-spec/#protocol
"""
from subprocess import check_output
from dbus import SessionBus, Interface
mpc = check_output("mpc --format='%album%\n%artist%\t%title%'", shell=True)\
.decode("utf-8")
try:
summary, body = mpc.splitlines()[:2]
except ValueError:
exit(127)
bus = {
"path": "/org/freedesktop/Notifications",
"name": "org.freedesktop.Notifications"
}
notif_args = [
"ncmpcpp", # app_name
227788, # replaces_id
"audio-x-generic", # app_icon
summary, # summary
body.translate(body.maketrans("\t", "\n")), # body
[], # actions
{"bgcolor": "#222222", "fgcolor": "#888888"}, # hints
5000 # expire_timeout
]
proxy = SessionBus().get_object(bus["name"], bus["path"])
interface = Interface(proxy, bus["name"])
interface.Notify(*notif_args)
#!/usr/bin/bash
# Get fields from mpc, split by tabs.
IFS=$'\t' read album artist title \
<<< "$(mpc --format="%album%\t%artist%\t%title%")"
notify-send --urgency=low --expire-time=5000 --app-name=ncmpcpp \
--icon=audio-x-generic "$album" "$artist\n$title"
@yrps
Copy link
Author

yrps commented Jul 31, 2016

@simplicity-load
Copy link

Just for completeness, to use the shell version you'd preferably put it on your ncmpcpp configuration folder so most likely ~/.config/ncmpcpp/ and append this line to your ncmpcpp config file (~/.config/ncmpcpp/config):
execute_on_song_change="~/.config/ncmpcpp/notify.sh"
But also don't forget to set its executable flag like such: chmod +x notify.sh.
Also for gnome I prefer the urgency level to be normal so it pops up for the specified time (expired-time, which gnome 42 seems to ignore).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment