Skip to content

Enhancement: spotify-player widget for macOS #336

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

Open
iberniex opened this issue Apr 24, 2025 · 2 comments
Open

Enhancement: spotify-player widget for macOS #336

iberniex opened this issue Apr 24, 2025 · 2 comments
Labels
enhancement New feature or request

Comments

@iberniex
Copy link

iberniex commented Apr 24, 2025

Greetings.
I'm really new to open-source so critique me in anyway.

Feature Proposal

A macOS spotify widget for the plugin.
I have two ways to propose this feature

  • using AppleScript to interact with spotify. There is a sample bash script I did below and works pretty well. Added it as a custom and here is how it looks like
Image
  • I also found spotify-player. A fast, easy to use, and configurable terminal music player. But doesn't have a command for displaying the current playback but could be used to enable custom keybindings for play, pause, next and previous instances.
#!/usr/bin/env bash
# Spotify status for Dracula tmux
# Displays current track with play/pause status (macOS only)

export LC_ALL=en_US.UTF-8

current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$current_dir/utils.sh" || { echo "❌ utils.sh missing"; exit 1; }

if [[ "$OSTYPE" != "darwin"* ]]; then
    echo "⏭️ Spotify (macOS only)"
    exit 0
fi

get_spotify_status() {
    local spotify_data
    spotify_data=$(osascript <<'END'
        tell application "System Events"
            if not (exists process "Spotify") then return "not_running"
        end tell
        tell application "Spotify"
            if player state is stopped then return "stopped"
            set t to "♪ " & artist of current track & " - " & name of current track
            if player state is paused then set t to "❚❚ " & artist of current track & " - " & name of current track
            return t
        end tell
END
    )
    case "$spotify_data" in
        "not_running") echo "Spotify not running" ;;
        "stopped")     echo "Spotify stopped" ;;
        *)             echo "$spotify_data" ;;
    esac
}

main() {
    local cache_file="/tmp/tmux_spotify_cache"
    local rate=$(get_tmux_option "@dracula-refresh-rate" 5)
    
    if [ ! -f "$cache_file" ] || [ $(($(date +%s) - $(stat -f%c "$cache_file"))) -ge "$rate" ]; then
        get_spotify_status > "$cache_file"
    fi
    cat "$cache_file"
}

main "$@"

Risks considered

New to applescript so not sure if it a good idea or not.
I was faced with a cache issue so I decided to create a cache file in tmp folder but I'm not really sure if it's a good idea.

Alternatives considered

I tried the spotify-tui but the package isn't being maintained so it was removed from brew and also using mpc is a hassle for me (maybe it's a skill issue).

@iberniex iberniex added the enhancement New feature or request label Apr 24, 2025
@Theoreticallyhugo
Copy link
Collaborator

Hi iberniex,
thank you very much for your suggestions! I was aware that spotify-tui wasn't maintained anymore, but I didn't yet know it was gone from brew.
A couple of things come to mind:

  • i don't know any apple script and might not have a machine to test it on in the not too distant future, so I probably wouldn't really be able to maintain this snippet. hence, whether or not we can maintain the apple script solution has to be decided by ethan i think.
  • your spotify player idea sounds cool, but should probably be a separate pr and plugin
  • do you think that your apple script could be done in such way that it works for a variety of media players? like setting a variable that id like to see playback by spotify, safari, and tidal, where the apple script would then, e.g. in a loop, check whether any of these run and then display the playback it found?
  • I like your proposals and would gladly test them as two separate PRs
    cheers
    hugo

@iberniex
Copy link
Author

Hey @Theoreticallyhugo ,

I will gladly do the PRs. Based on:

  • The AppleScript plugin
  • The spotify_player plugin
  • i don't know any apple script and might not have a machine to test it on in the not too distant future, so I probably wouldn't really be able to maintain this snippet. hence, whether or not we can maintain the apple script solution has to be decided by ethan i think.

Considering your comment on this matter, I did some digging and found a reliable way to recover the precise playback using the spotify_player package ( I kinda lied there is a way to get the playback with this ).
It actually supports various instances of spotify like browser, desktop and also with the terminal.

I tried it for some time and found out that there are latency issues.

# Before the additions

$ time sp get key playback |  jq -r '.item | "\(.artists | map(.name) | join(", ")) - \(.name)"'

Pitbull, Kesha - Timber
spotify_player get key playback  0.14s user 0.04s system 4% cpu 3.614 total
jq   0.00s user 0.00s system 0% cpu 3.613 total

You have to run the daemon feature provided and add your own spotify client_id from spotify in order for it to work.

# After the additions

$ time sp get key playback |  jq -r '.item | "\(.artists | map(.name) | join(", ")) - \(.name)"'

Aqua - Barbie Girl
spotify_player get key playback  0.01s user 0.01s system 59% cpu 0.020 total
jq 0.00s user 0.01s system 48% cpu 0.018 total

So I found it precise to just use the spotify_player package and it's maintained.

  • like setting a variable that id like to see playback by spotify, safari, and tidal, where the apple script would then, e.g. in a loop, check whether any of these run and then display the playback it found?

Based on this, I can set a flag to check for other media players like Music.App, VLC and spotify, but it might be tricky for the browser and tidal.

  • After some research, with the browser it would require injecting to the DOM and it would be really hard cause some apps on their browser apps they obfuscates their DOM.
  • For Tidal, there is no local playback documentation and there is no applescript support at the moment but I found a guy who did some stuff but its from four years ago so will research on it while I do the PRs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

No branches or pull requests

2 participants