forked from julian-heng/yabai-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
show_song.sh
executable file
·172 lines (143 loc) · 4.06 KB
/
show_song.sh
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env bash
# shellcheck disable=1090,2194
check_apps()
{
if ! type -p osascript > /dev/null; then
return 1
fi
}
notify()
{
title="${title_parts[*]}"
subtitle="${subtitle_parts[*]}"
message="${message_parts[*]}"
[[ "${title:0:1}" == "|" ]] && \
title="${title##'| '}"
[[ "${title:-1:1}" == "|" ]] && \
title="${title%%' |'}"
[[ "${subtitle:0:1}" == "|" ]] && \
subtitle="${subtitle##'| '}"
[[ "${subtitle:-1:1}" == "|" ]] && \
subtitle="${subtitle%%' |'}"
[[ "${message:0:1}" == "|" ]] && \
message="${message##'| '}"
[[ "${message:-1:1}" == "|" ]] && \
message="${message%%' |'}"
if [[ "${stdout}" ]]; then
[[ "${title}" ]] && \
display+=("${title}")
[[ "${subtitle}" ]] && \
display+=("${subtitle}")
[[ "${message}" ]] && \
display+=("${message}")
printf "%s\\n" "${display[@]}"
else
osa_script="display notification \"${message}\" \
with title \"${title}\" \
subtitle \"${subtitle}\""
/usr/bin/env osascript <<< "${osa_script}"
fi
}
check_app_state()
{
match="false"
if pgrep -x "cmus" > /dev/null; then
app="cmus"
app_state="$(awk '/status/ {print $2}' < <(cmus-remote -Q))"
match="true"
elif [[ "$(osascript -e "application \"Music\" is running")" == "true" ]]; then
app="Music"
app_state="$(osascript -e "tell application \"Music\" to player state as string")"
match="true"
elif [[ "$(osascript -e "application \"Spotify\" is running")" == "true" ]]; then
app="Spotify"
app_state="$(osascript -e "tell application \"Spotify\" to player state as string")"
match="true"
fi
[[ "${match}" != "true" ]] && \
app_state="none"
}
get_song_info()
{
if [[ "${app}" == "cmus" ]]; then
IFS=":" \
read -r track \
artist \
album \
< <(cmus-remote -C "format_print %{title}:%{artist}:%{album}")
else
track_cmd="name of current track as string"
artist_cmd="artist of current track as string"
album_cmd="album of current track as string"
osa_script="tell application \"${app}\"
${track_cmd} & \":\" & \
${artist_cmd} & \":\" & \
${album_cmd}
end tell"
IFS=":" \
read -r track \
artist \
album \
< <(/usr/bin/env osascript <<< "${osa_script}")
fi
}
print_usage()
{
printf "%s\\n" "
Usage: ${0##*/} --option
Options:
[--stdout] Print to stdout
[-r|--raw] Print raw values delimited by commas
[-h|--help] Show this message
"
}
get_args()
{
while (($# > 0)); do
case "$1" in
"--stdout") stdout="true" ;;
"-r"|"--raw") raw="true" ;;
"-h"|"--help") print_usage; exit ;;
esac
shift
done
}
main()
{
! check_apps && exit 1
get_args "$@"
check_app_state
case "${app_state}" in
"none"|"stopped")
title_parts=("Now Playing")
subtitle_parts=()
message_parts=("No Music Playing")
;;
*)
get_song_info
title_parts+=("Now Playing")
if [[ "${artist}" ]]; then
subtitle_parts+=("${artist}")
[[ "${track}" ]] && \
subtitle_parts+=("-" "${track}")
elif [[ "${track}" ]]; then
subtitle_parts+=("${track}")
fi
[[ "${album}" ]] && \
message_parts+=("${album}")
;;
esac
[[ "${raw}" ]] && {
printf -v out "%s," \
"${app}" \
"${app_state}" \
"${artist}" \
"${album}"
printf -v out "%s%s" "${out}" "${track}"
printf "%s\\n" "${out}"
exit 0
}
notify
}
[[ "${BASH_SOURCE[0]}" == "${0}" ]] && \
main "$@"