r/i3wm•
brettinternet

Scripting open existing window instead of new window

Question

I'm trying to script a dunst/Spotify interaction to dunstify playerctl metadata and open Spotify tracks by way of spotify --uri=$(playerctl -p spotify metadata mpris:trackid) when it's dismissed/clicked.

How can I make sure the existing Spotify window is opened rather than a brand new window?

If the answer requires very program-specific API knowledge, then it would be helpful if someone could provide a practical approach to handling this problem.

I'm also curious about this in order to enhance how rofi handles app-switching, to intervene with whether it should open a new window or an existing window (outside the context of rofi -modi window which handles intentional window switching).

---

Edit: I guess the appropriate way to handle something like this would be to check if the process is running and focus the window, otherwise open the application.

Posted Jan 10, 2020, 6:37 PM UTC · edited

216
124211212121•

Try using dbus like so:

	dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.OpenUri "string:$TRACK_ID"
2
brettinternetOP•

Thank you for the suggestion. Unfortunately, rather than just pulling up the track, it restarts the track. However that's probably the expected behavior for the URI. I think for now I'll just use i3-msg '[class="Spotify"] focus'.

Some related posts I found:

1
brettinternetOP•

For anyone interested, here's an example of the simple script so far:

#!/bin/bash

MSG_ID="234552"
PLAYER="spotify" # $(playerctl -l)

TRACK_ID=$(playerctl -p "$PLAYER" metadata mpris:trackid)
TRACK_TITLE=$(playerctl -p "$PLAYER" metadata xesam:title)
TRACK_ARTIST=$(playerctl -p "$PLAYER" metadata xesam:artist)
TRACK_ALBUM=$(playerctl -p "$PLAYER" metadata xesam:album)

open_player () {
    # opens new window instead of existing 😞
    /usr/bin/spotify --uri="$1" &>/dev/null &
    disown
}

ACTION=$(dunstify -a "$PLAYER" -r "$MSG_ID" -u low --action="openPlayer,Open Album" \
    "$TRACK_TITLE" "$TRACK_ARTIST - $TRACK_ALBUM"
)

case "$ACTION" in
    "openPlayer")
        open_player $TRACK_ID
        ;;
esac

You could handle when $ACTION = "2" as well (to handle when it's dismissed), or change mouse events in dunstrc.

---

I'm also not sure of an elegant way to handle album artwork since Spotify metadata only provides a remote image. For example:

$ playerctl -p spotify metadata mpris:artUrl
https://open.spotify.com/image/9f6346a3426a8f097b77d5ccf40ad55976b1bbae
1
[deleted]•

[deleted]

-4
brettinternetOP•

bad bot

6
[deleted]•

[deleted]

1
theRealStrimmlarn•

is it triggered by $MSG?

1
[deleted]•

[deleted]

1
theRealStrimmlarn•

ok, thx

1