summaryrefslogtreecommitdiff
path: root/mac/.config/sketchybar.mon/plugins/music.sh
blob: ce2370166d3be61e81d5baa03f7274472cbbf1b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/sh

# Load global styles, colors and icons
source "$CONFIG_DIR/globalstyles.sh"

music_item_defaults=(
  align=center
  width=240
  label.max_chars=33
)

music_cover=(
  background.image=media.artwork
  background.image.scale=5
  background.image.corner_radius=4
  background.image.padding_left=$PADDINGS
  background.image.padding_right=$PADDINGS
  background.label.align=center
  y_offset=-$PADDINGS
  align=center
)

music_artist=(
  "${music_item_defaults[@]}"
)

music_title=(
  "${music_item_defaults[@]}"
  label.font.style="Bold"
)

music_album=(
  "${music_item_defaults[@]}"
)

render_bar_item() {
  sketchybar --set $NAME label="$CURRENT_ARTIST: $CURRENT_SONG"
}

render_popup() {
  sketchybar --set $NAME.cover "${music_cover[@]}"   \
             --set $NAME.artist "${music_artist[@]}" \
             --set $NAME.title "${music_title[@]}"   \
             --set $NAME.album "${music_album[@]}"
}

update() {
  CURRENT_ARTIST="$(echo "$INFO" | jq -r '.artist')"
  CURRENT_SONG="$(echo "$INFO" | jq -r '.title')"
  CURRENT_ALBUM="$(echo "$INFO" | jq -r '.album')"
  PLAYER_STATE="$(echo "$INFO" | jq -r '.state')"

  if [ "$PLAYER_STATE" = "playing" ]; then
    sketchybar --set $NAME drawing=on                      \
                           icon=􀊆                          \
               --set $NAME.artist label="$CURRENT_ARTIST"  \
               --set $NAME.title label="$CURRENT_SONG"     \
               --set $NAME.album label="$CURRENT_ALBUM"
    render_bar_item
    render_popup

  else
    sketchybar --set $NAME icon=􀊄
    popup off
    sketchybar --set $NAME drawing=off
  fi
  
}

popup() {
  sketchybar --set "$NAME" popup.drawing="$1"
}

playpause() {
  osascript -e 'tell application "Music" to playpause'
}
  

case "$SENDER" in
"routine" | "forced" | "media_change")
  update
  ;;
"mouse.entered")
  popup on
  ;;
"mouse.exited" | "mouse.exited.global")
  popup off
  ;;
"mouse.clicked")
  playpause
  ;;
esac