FelixKratz / SketchyBar

A highly customizable macOS status bar replacement
https://felixkratz.github.io/SketchyBar/
GNU General Public License v3.0
5.45k stars 85 forks source link

Music Item - Content Disappearing #375

Closed jncsotk30 closed 1 year ago

jncsotk30 commented 1 year ago

First off, well done to the developers on SketchyBar! I'm loving it so far and am excited to see what else can be done.

That said, I'm experiencing a quirk with my music / now playing "widget". It's tied to a very basic script which depends on a CLI utility called nowplaying-cli. This is used to retrieve media information like track title, track artist etc. I pass the resulting data from this script into the label field. It will display properly for a brief moment, but then disappears after 1 or 2 seconds.

The update_freq is set to 3, but I don't believe that's where the problem resides. Not quite sure what's going on here.

Could someone please help me identify the problem if possible?

sketchybarrc

### MUSIC ###
sketchybar --add item music left \
           --set music update_freq=3 \
                      icon=󰎆 \
                      label="." \
                      script="$PLUGIN_DIR/music.sh" \
                      click_script="$PLUGIN_DIR/music.sh" \
                      background.drawing=on \
                      background.color=0xff212121 \
                      background.corner_radius=5 \
                      background.padding_left=5 \
                      icon.padding_left=15 \
                      icon.padding_right=15 \
                      label.drawing=on \
                      label.padding_left=0 \
                      label.padding_right=15 \

music.sh

#!/bin/sh
# DESCRIPTION: Script will pull now playing information from the Mac system media library.
# DEPENDS: nowplaying-cli(~/.bin)

# Name of executable in $HOME/.bin (symlinked)
NPCLI="npcli"

# Currently playing music info
TRACK="$(npcli get title)"
ARTIST="$(npcli get artist)"

sketchybar --set music label="$TRACK"

https://github.com/FelixKratz/SketchyBar/assets/125376259/c3e7aa0b-5ac1-481c-82d3-8f011a4de425

jncsotk30 commented 1 year ago

One other note to mention, if I use the $NAME variable in the music.sh script, it returns the following error when running script.

Screenshot 2023-05-20 at 11 12 08 AM Screenshot 2023-05-20 at 11 12 17 AM

However, if you switch to using the manual name of the item, it will work fine. Seems like $NAME isn't passed into the script. How is that info passed?

Screenshot 2023-05-20 at 11 12 30 AM Screenshot 2023-05-20 at 11 12 39 AM

FelixKratz commented 1 year ago

I think the problem is that the routine update executed through sketchybar does not find the npcli executable (since scripts run through sketchybar do not have access to variables set in your zshrc). It shows correctly if you execute it in a shell where npcli is added to PATH.

So the solution will probably be to replace npcli with the absolute path to the binary.

The $NAME parameter is set by sketchybar and thus only available when sketchybar ececutes the scripts internally.

jncsotk30 commented 1 year ago

Spot on @FelixKratz. That was exactly my problem. I updated the music.sh script with the absolute path and all is working well now. Next I just gotta setup song change event and wire that up.

#!/bin/sh
# DESCRIPTION: Script will pull now playing information from the Mac system media library.
# DEPENDS: nowplaying-cli(~/.bin)

# Name of executable in $HOME/.bin (symlinked)
NPCLI="$HOME/.bin/npcli"

# Currently playing music info
TRACK="$($NPCLI get title)"
ARTIST="$($NPCLI get artist)"

sketchybar --set music label="$TRACK"

Screenshot 2023-05-20 at 1 54 22 PM

Thanks very much!

Thank you for clarifying about the $NAME var.

gastonmorixe commented 8 months ago
image image

This is my current script

# Name of executable in $HOME/.bin (symlinked)
NPCLI="$HOME/projects/nowplaying-cli/nowplaying-cli"

# Currently playing music info
TRACK="$($NPCLI get title)"
ARTIST="$($NPCLI get artist)"

# Get the duration and elapsed time from the nowplaying-cli
DURATION="$($NPCLI get Duration)"
ELAPSED_TIME="$($NPCLI get ElapsedTime)"
PLAYBACK_RATE="$($NPCLI get PlaybackRate)"

# Convert the elapsed time to minutes and seconds
ELAPSED_MINUTES=$(awk "BEGIN {printf \"%d\", $ELAPSED_TIME/60}")
ELAPSED_SECONDS=$(awk "BEGIN {printf \"%d\", $ELAPSED_TIME%60}")

# Format the seconds to always be two digits
printf -v ELAPSED_SECONDS "%02d" $ELAPSED_SECONDS

# Calculate the percentage of the song elapsed
PERCENTAGE=$(bc -l <<< "$ELAPSED_TIME*100/$DURATION")
PERCENTAGE=$(printf "%.0f" $PERCENTAGE) # Round to the nearest whole number

# Display the result in the requested format
TIME="${ELAPSED_MINUTES}:${ELAPSED_SECONDS} ${PERCENTAGE}%"

# Set PLAYBACK_STATE based on the playback rate
if [ "$PLAYBACK_RATE" == "0" ]; then
    PLAYBACK_STATE="[PAUSED]"
else
    PLAYBACK_STATE="$TIME"
fi

sketchybar --set music label="$TRACK · $ARTIST $PLAYBACK_STATE"