Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add current played track to preselected favourite playlist #7

Open
jrie opened this issue Feb 15, 2025 · 5 comments
Open

Add current played track to preselected favourite playlist #7

jrie opened this issue Feb 15, 2025 · 5 comments

Comments

@jrie
Copy link

jrie commented Feb 15, 2025

Hello, as the title states:
It would be nice to have the option to add the currently played/queued track to a favorite playlist.

How it might work:

  1. Create a playlist
  2. Use a command, like playlist setfavorite *nameOfPlaylist* to set nameOfPlaylist as, current favorite playlist.
  3. Use a command: playlist favorite or even only playlist favor to add the currently played/queued track to the above favorite playlist.
@Malvineous
Copy link
Member

There's probably a better way to do this, but I do it in a shell script like this:

#!/bin/sh

function msgok() {
        echo -e "\e[1;32m$1\e[0m"
}
function addToList() {
        LIST="$1"
        CONTENT=`xmms2 info`
        ID=`echo "$CONTENT" | grep "\[\s*server\] id" | sed 's/[^]]\+\][^=]\+=\s\+//'`
        TITLE=`echo "$CONTENT" | grep "\[\s*plugin[^]]\+\] title" | sed 's/[^]]\+\][^=]\+=\s\+//'`
        msgok "xmms2 add -p \"$LIST\" -t \"#$ID\"  # $TITLE"
        # Switch to red text in case xmms2 outputs any errors (like an invalid playlist name)
        echo -en "\e[1;31m"
        xmms2 add -p "$LIST" -t "#$ID"
        R=$?
        echo -en "\e[0m"
        return $R
}
if [ "$1" != "" ]; then
        addToList "$1"
        exit $?
fi

If I save this in ~/plist then I can run ~/plist fav to add the currently playing song to the playlist "fav".

The script above could be shortened a bit, the reason for the extra functions is that I have multiple favourite playlists (for different genres etc.) and the full script displays a list of them, and I can press a key to choose which list the currently playing song should get added to.

Open to any suggestions about a better way of getting the details of the currently playing song too, as my solution above works but it feels like a bit of a hack.

@jrie
Copy link
Author

jrie commented Feb 15, 2025

In the the default client, it is possible to add command alias(es) in the config.

What I did not recall, but your code brought me to some idea for playing around, that it is possible to use (at least, but max?) one variable argument.

As for the alias(es), like

[alias]
...
listplaylist = list -p $@

The aliases can be used like (from inside client):
listplaylist "myPlaylist" would then run list -p "myPlaylist" - so far so good.

or, like in your script prefix with xmms2 listplaylist "myPlaylist". Works well.

If there is, one positional argument possible - I really do not know otherwise, I would end up like some alias like that
addFav = add -p "$@" -t "#$3"

This works.

If I hardcode the playlist.
addFav = add -p "myFavList" -t "#$@"

Works using the "ID" like:
xmms2 addFav 3 which adds trackid 3 to the "myFavList".

But using two variables as alias:
addFav = add -p "$@" -t "#$@"

Did not work for me - or using positional values.

This would be almost exactly do what I would like to have/archive, in addition to your shell script.

Unfortunately I do not know, another way of how to get the track id.

@Malvineous
Copy link
Member

Positional parameters look like they've been implemented, maybe lines 39 and 57 might give you some ideas.

But yes, getting the currently playing ID seems like the tricky bit. It looks like the value is parsed as a collection, but I can't see any collection grammar for returning the current track ID.

@jrie
Copy link
Author

jrie commented Feb 16, 2025

@Malvineous - I found a very decent way to get only the "id":
current -f ${id}

What is basically does:
Get the current command output / currently played track and show the output in format as the defined format pattern, here only the (server) id field.

So, you very much simplify your script code without grep and sed and only use the return value of:
xmms2 current -f '${id}'

You can also use -f '${title}'...

I overworked your script a little bit, and it works like a charm:

#!/bin/sh

function msgok() {
        echo -e "\e[1;32m$1\e[0m"
}
function addToList() {
        LIST="$1"
        ID=`xmms2 current -f '${id}'`
        TITLE=`xmms2 current -f '${title}'`
        msgok "xmms2 add -p \"$LIST\" -t \"#$ID\"  # $TITLE"
        # Switch to red text in case xmms2 outputs any errors (like an invalid playlist name)
        echo -en "\e[1;31m"
        xmms2 add -p "$LIST" -t "#$ID"
        R=$?
        echo -en "\e[0m"
        return $R
}
if [ "$1" != "" ]; then
        addToList "$1"
        exit $?
fi

One more edit:
Having only one xmms2 current -f '${id} | ${title}' in your script would probably the best. The pipe or what else could be used as a split charater here.

Could also be:
xmms2 current -f '${id} ## ${title}'

Or anything like that. To fetch the "current" information one call.

@Malvineous
Copy link
Member

Ohh that's a much better solution, I can't believe I never noticed the current command before! Thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants