-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppleMusic-Spotify-NowPlaying.10s.sh
executable file
·219 lines (180 loc) · 5.29 KB
/
AppleMusic-Spotify-NowPlaying.10s.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/local/bin/zsh
#exec 2> /dev/null
# <bitbar.title>Now playing</bitbar.title>
# <bitbar.version>v1.1</bitbar.version>
# <bitbar.author>Adam Kenyon</bitbar.author>
# <bitbar.author.github>adampk90</bitbar.author.github>
# <bitbar.desc>Shows and controls the music that is now playing. Currently supports Spotify, Music, and Vox.</bitbar.desc>
# <bitbar.image>https://pbs.twimg.com/media/CbKmTS7VAAA84VS.png:small</bitbar.image>
# <bitbar.dependencies></bitbar.dependencies>
# <bitbar.abouturl></bitbar.abouturl>
apps=(Spotify Music)
script=$0
BitBarDarkMode=$(osascript -e "tell application \"System Events\" to tell appearance preferences
get properties
set currentValue to dark mode
return currentValue
end tell")
if [ $BitBarDarkMode ]; then
COLOR0="#666666"
COLOR1="#ffffff"
COLOR2="#666666"
COLOR3="#333333"
else
COLOR0="#333333"
COLOR1="#000000"
COLOR2="#666666"
COLOR3="#999999"
fi
function geek_visible {
# If you use GeekTool, you can modify these parameters to have the album image appear on your desktop
#
#/usr/bin/osascript -e "try
# tell application \"GeekTool Helper\"
# set image url of image geeklet named \"NowPlaying\" to \"file://$2\"
# set visible of image geeklet named \"NowPlaying\" to \"$1\"
# end tell
#on error
# set message to \"Define a image Geeklet called 'NowPlaying' for BitBar to display an image on the desktop\"
# display dialog message
#end try
#"
}
function check_which_running {
for i in "${apps[@]}"; do
# is the app running?
app_state=$(osascript -e "application \"$i\" is running")
if [[ "$app_state" = "true" ]]; then
#app_playing=$(osascript -e "tell application \"$i\" to player state as string")
echo $i
return
fi
done
geek_visible "false" "fakefile"
echo " | color=$COLOR0 size=14"
exit
}
function control_player {
# open a specified app
if [ "$1" = "open" ]; then
osascript -e "tell application \"$2\" to activate"
exit
fi
# play/pause
if [ "$1" = "play" ] || [ "$1" = "pause" ]; then
osascript -e "tell application \"$2\" to $1"
exit
fi
}
function write_image {
if [ -f "$imgFile" ]; then
base64img=$(base64 < "$imgFile")
else
if [ $app = "Music" ]; then
/usr/bin/osascript <<-EOF
set tmpName to POSIX file "$1"
try
tell application "Music"
tell artwork 1 of current track
set srcBytes to raw data
end tell
end tell
tell application "System Events"
try
set outFile to open for access tmpName with write permission
set eof outFile to 0
write srcBytes to outFile
close access outFile
end try
end tell
on error errText
""
end try
EOF
fi
if [ $app = "Spotify" ]; then
curlAddress=$(/usr/bin/osascript <<-EOF
tell application "Spotify"
return artwork url of current track
end tell
EOF
);
curl -s -o $imgFile $curlAddress
fi
/usr/local/bin/convert $imgFile -set units PixelsPerInch -density 72 -resize 300x300^\> $imgFile
base64img=$(base64 < "$imgFile")
fi
}
function get_track_info {
# determine the track and artist
track_query="name of current track"
artist_query="artist of current track"
album_query="album of current track"
# Vox uses a different syntax for track and artist names
if [ "$app" = "Vox" ]; then
track_query="track"
artist_query="artist"
album_query="album"
fi
track=$(osascript -e "tell application \"$app\" to $track_query")
artist=$(osascript -e "tell application \"$app\" to $artist_query")
album=$(osascript -e "tell application \"$app\" to $album_query")
if [ "$app" = "Music" ]; then
imgFormat=$(osascript <<-EOF
try
tell application "Music"
tell artwork 1 of current track
if format is JPEG picture then
return ".jpg"
else
return ".png"
end if
end tell
end tell
on error
return ""
end try
EOF
)
fi
imgFile=$(osascript -e "POSIX path of (path to temporary items from user domain)")
imgFile+=$(echo $artist $album $imgFormat | sed "s/[^a-zA-Z0-9\.]//g")
}
function output_menu_info {
echo "---"
echo "$artist | color=$COLOR2"
echo "$album | size=14 color=$COLOR3 length=30"
#output the track and artist
echo "---"
if [ "$track" != "no track selected" ] && [ "$base64img" != "" ]; then
echo "| image=$base64img bash='$0' param1=open terminal=false"
fi
}
##########################################################################################
app=$(check_which_running)
if [[ "$app" == "Music" ]] || [[ "$app" == "Spotify" ]]; then
playerState=$(osascript -e "tell application \"$app\" to player state as string")
fi
if (( ${1} )) && (( ${2} )); then
control_player $1 $2
fi
if [[ "$playerState" == "playing" ]] || [[ "$playerState" == "paused" ]]; then
get_track_info
artist=${artist:gs/Various\ Artists/}
menubarArtist=${artist:gs/Various/}
if [ "$menubarArtist" != "" ] && [ "$menubarArtist" != "\n" ]; then
fi
if [ $playerState = "playing" ]; then
echo "$menubarArtist - $track | color=$COLOR3 size=14 length=50"
else
echo " | color=$COLOR0 size=14"
fi
write_image $imgFile
output_menu_info
geek_visible "true" $imgFile
else
echo " | color=$COLOR0 size=14"
echo "---"
echo "> Play | color=$COLOR0 size=14 bash='$0' param1=play param2=$app refresh=true terminal=false"
geek_visible "false"
fi