Skip to content

Enhancement: spotify-player widget for macOS #336

Open
@iberniex

Description

@iberniex

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions