Sunday, May 5, 2024
 Popular · Latest · Hot · Upcoming
5
rated 0 times [  5] [ 0]  / answers: 1 / hits: 2272  / 3 Years ago, sun, july 18, 2021, 8:48:01

How can I add a Unity "quicklist" for Spotify (installed from their website), so that when I right-click on the icon on my dock, I get: "Next, Previous, Stop, Play" and so on?


More From » unity

 Answers
2

spotify (native linux client)



Since spotify includes an indicator to control some of its functions, this allows us to use dbus to send events.



There is a great script on ubuntuforums that covers this.



First install a prerequisite:



sudo apt-get install libnet-dbus-perl


Now copy and paste the script into a text file called spcmd and save this to your home folder.



Give it execute rights:



chmod +x ~/spcmd


Lets move this to a more useful folder:



mv ~/spcmd /usr/local/bin


Now, lets create a quick list.



First copy the spotify desktop file to your home folder:



mkdir -p ~/.local/share/applications
cp /usr/share/applications/spotify.desktop ~/.local/share/applications


Open the file and copy and paste the quick list to the end of the file. Save it.



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


end result



enter image description here



quicklist



X-Ayatana-Desktop-Shortcuts=PlayPause;Next;Previous;Stop;

[PlayPause Shortcut Group]
Name=PlayPause
Exec=spcmd playpause
TargetEnvironment=Unity

[Next Shortcut Group]
Name=Next
Exec=spcmd next
TargetEnvironment=Unity

[Previous Shortcut Group]
Name=Previous
Exec=spcmd previous
TargetEnvironment=Unity

[Stop Shortcut Group]
Name=Stop
Exec=spcmd stop
TargetEnvironment=Unity


spcmd



#!/usr/bin/perl

use 5.010;
use strict;
use warnings;
use File::Basename;
use Net::DBus;

# Figure out some dbus stuff
unless ( defined $ENV{'DBUS_SESSION_BUS_ADDRESS'} ) {
&set_DBUS_SESSION_BUS_ADDRESS;
#die "Don't know which dbus to attach to.
Make sure environment variable DBUS_SESSION_BUS_ADDRESS is set.";
}
#my $bus = Net::DBus->find;
my $bus = Net::DBus->session;
my $spotify = $bus->get_service("org.mpris.MediaPlayer2.spotify");
my $player = $spotify->get_object("/org/mpris/MediaPlayer2", "org.mpris.MediaPlayer2.Player");
my $application = $spotify->get_object("/org/mpris/MediaPlayer2", "org.mpris.MediaPlayer2");
my $getorset = $spotify->get_object("/org/mpris/MediaPlayer2", "org.freedesktop.DBus.Properties");

# Handle command line argument
if (scalar @ARGV == 0) { &print_help; }
given ($ARGV[0]) {
# when ('play') { $player->Play(); } #Does not work for some reason.
when ('pause') { $player->Pause(); }
when ('playpause') { $player->PlayPause(); }
when ('next') { $player->Next(); }
when ('previous') { $player->Previous(); }
when ('stop') { $player->Stop(); }
when ('playstatus') { print $getorset->Get("org.mpris.MediaPlayer2.Player", "PlaybackStatus") . "
"; }
when (m/spotify:(artist|album|track):[0-9a-zA-z]{22}/) { $player->OpenUri($_); }
when ('metadata-debug') { &print_debug_metadata; }
default { &print_help; }
}


# Print the help text
sub print_help {
print "USAGE: " . basename($0) . " {pause|playpause|next|previous|stop|playstatus|met adata-debug|<spotify URI>}

";
print " " . "pause" . " " . "Causes spotify to pause current playback." . "
";
print " " . "playpause" . " " . "Causes spotify to pause or play current playback." . "
";
print " " . "next" . " " . "Goes to next song." . "
";
print " " . "previous" . " " . "Goes to previous song." . "
";
print " " . "stop" . " " . "Stops playback." . "
";
print " " . "playstatus" . " " . "Prints current playstatus (Playing/Paused)." . "
";
print " " . "<spotify URI>" . " " . "Starts playing supplied URI." . "
";
print " " . "metadata-debug" . " " . "Shows available data on currently playing song." . "
";
print " " . " " . "Fairly unformatted, thus "debug" data." . "
";
print "
";
print "EXAMPLES: " . basename($0) . " playpause" . "
";
print " " . basename($0) . " spotify:track:5XXAq1r5r73ZyBS0XAiGw0" . "
";

exit;
}

# Print some raw metadata
sub print_debug_metadata {
# Dereference the metadata hashref by copying it to a local hash
my %metadata = %{ $getorset->Get("org.mpris.MediaPlayer2.Player", "Metadata") };

# Print all metadata
print "Now Playing:
";
for (keys %metadata) {
print $_ . ": " . $metadata{$_} . "
" unless ($_ eq 'xesam:artist');
}

# Dereference the artist arrayref by copying it to local array
my @artistarray = @{ $metadata{'xesam:artist'} };

# Print all artists.
foreach my $artist (@artistarray) {
print "artist: " . $artist . "
";
}
}

sub set_DBUS_SESSION_BUS_ADDRESS {
my $curruser = `whoami`; chomp $curruser;
my $procname = 'spotify';
my $pid = `pgrep -o -u $curruser $procname`; chomp $pid;
my $environ = '/proc/' . $pid . '/environ';
my $dbussession = `grep -z DBUS_SESSION_BUS_ADDRESS $environ`; $dbussession =~ s/^DBUS_SESSION_BUS_ADDRESS=//;

$ENV{'DBUS_SESSION_BUS_ADDRESS'} = $dbussession;
}

[#37915] Tuesday, July 20, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
damomnning

Total Points: 422
Total Questions: 90
Total Answers: 106

Location: Mali
Member since Thu, Aug 13, 2020
4 Years ago
damomnning questions
;