-
Notifications
You must be signed in to change notification settings - Fork 1
/
spotifyHotkeys.ahk
256 lines (233 loc) · 11.3 KB
/
spotifyHotkeys.ahk
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#Include %A_ScriptDir%
#Include Spotify.ahk
SetTimer,TOOLTIP,5000
SetTimer,TOOLTIP,Off
; /* ============== */
; /* Spotify */
; /* ============== */
; Download https://github.com/CloakerSmoker/Spotify.ahk
; Add shortcut to shell:Startup
; Add lib folder to shell:Startup
; In Windows settings, set .mp3 association to Spotify Windows App (Spotify.exe) by right clicking an mp3 file, selecting Open With -> Choose another app, Look for another app on this PC, find Spotify.exe. If you don't do this, Windows might open Windows Media Player or another media app whenever you press a media key.
; If you're using the Windows app, run this in PowerShell (as admin) to get exact location of Spotify.exe: "$((ls "C:\Program Files\WindowsApps\" | Where-Object { $_ -like "*spotify*"})[0].FullName)\Spotify.exe"
; If Spotify updated, the folder changed and Windows Media Player or another app may open when you press your media keys. Redo the above two steps.
; If you get an error message attempting to set Spotify to open mp3s, open Task Manager with CTRL + Shift + Esc, click File -> Open New Task, check "Create this task with administrative privileges", type Explorer, and press enter. Try it from there. Then close Explorer and see if the media keys work.
; https://community.spotify.com/t5/Desktop-Windows/Keyboard-media-launch-button/td-p/1736288
; Create new Spotify object
spotifyObject := new Spotify
; Activate Spotify whenever modifier is configured
*Launch_Media::
Run Spotify
return
; Shift + Media_Play_Pause adds the song to a playlist via menu to display all playlist options
+Media_Play_Pause::
playlistMenu()
Return
; Media_Play_Pause toggles playback and shows Now Playing information in a tool tip
*Media_Play_Pause::
CurrentPlayback := spotifyObject.Player.GetCurrentPlaybackInfo()
Send, {Media_Play_Pause}
showSongInfo(CurrentPlayback)
Return
; CTRL + Media_Play_Pause just gives Now Playing information in a tool tip
^Media_Play_Pause::
CurrentPlayback := spotifyObject.Player.GetCurrentPlaybackInfo()
showSongInfo(CurrentPlayback)
Return
; Shift + CTRL + Media_Play_Pause gives Now Playing information in a tool tip and pastes to clipboard
^+Media_Play_Pause::
CurrentPlayback := spotifyObject.Player.GetCurrentPlaybackInfo()
clipboard:= showSongInfo(CurrentPlayback) . "`n" . "Link: " . CurrentPlayback.item.external_urls.spotify
Return
; Alt + Shift + CTRL + Media_Play_Pause gives Now Playing information in a tool tip and pastes artist(s) to clipboard
^!+Media_Play_Pause::
CurrentPlayback := spotifyObject.Player.GetCurrentPlaybackInfo()
clipboard := Trim(returnArtist(CurrentPlayback))
Return
; Media_Next shows song info when going to next track
*Media_Next::
Send,{Media_Next}
Sleep, 1000
CurrentPlayback := spotifyObject.Player.GetCurrentPlaybackInfo()
showSongInfo(CurrentPlayback)
return
; CTRL + Next seeks forward ~10 seconds
^Media_Next::
CurrentPlayer := spotifyObject.Player
CurrentPlayback := CurrentPlayer.GetCurrentPlaybackInfo()
CurrentPlayer.SeekTime(CurrentPlayback.progress_ms + 10000)
ToolTip,% "Forward ~10 seconds"
SetTimer,TOOLTIP,On
Return
; Shift + Next seeks forward ~30 seconds
+Media_Next::
CurrentPlayer := spotifyObject.Player
CurrentPlayback := CurrentPlayer.GetCurrentPlaybackInfo()
CurrentPlayer.SeekTime(CurrentPlayback.progress_ms + 10000 * 3)
ToolTip,% "Forward ~30 seconds"
SetTimer,TOOLTIP,On
Return
; Show song info when going to previous track
*Media_Prev::
Send,{Media_Prev}
Sleep, 1000
CurrentPlayback := spotifyObject.Player.GetCurrentPlaybackInfo()
showSongInfo(CurrentPlayback)
return
; CTRL + Prev seeks backwards ~10 seconds
^Media_Prev::
CurrentPlayer := spotifyObject.Player
CurrentPlayback := CurrentPlayer.GetCurrentPlaybackInfo()
CurrentPlayer.SeekTime(CurrentPlayback.progress_ms - 10000)
ToolTip,% "Backwards ~10 seconds"
SetTimer,TOOLTIP,On
Return
; Shift + Prev seeks backwards ~30 seconds
+Media_Prev::
CurrentPlayer := spotifyObject.Player
CurrentPlayback := CurrentPlayer.GetCurrentPlaybackInfo()
CurrentPlayer.SeekTime(CurrentPlayback.progress_ms - 10000 * 3)
ToolTip,% "Backwards ~30 seconds"
SetTimer,TOOLTIP,On
Return
; CTRL + Media button adds to New School and Social Media Likes playlists
^Launch_Media::
addSongToPlaylist("id", "New School")
Sleep, 50
addSongToPlaylist("id", "Social Media Likes")
SetTimer,TOOLTIP,On
Return
; Shift + Media button adds to Social Media Likes playlists
+Launch_Media::
addSongToPlaylist("id", "Social Media Likes")
SetTimer,TOOLTIP,On
Return
; CTRL + Shift + Media button adds to Starred playlist
^+Launch_Media::
addSongToPlaylist("id", "Starred")
Return
; Media button adds to Starred2 and Starred3 playlists
Launch_Media::
; Starred2 is my primary playlist for new songs I like
addSongToPlaylist("id", "Starred2")
; Adding new tracks to Starred3 as well in case tracks added to Starred2 are reset at some point in time since the playlist is over 10k
Sleep, 50
addSongToPlaylist("id", "Starred3")
Return
; Alt + Media button shows the list of current hotkeys persistently (press another hoykey to clear, i.e. CTRL + Media_Play_Pause)
!Launch_Media::
list := "* Shift + Media_Play_Pause adds the song to a playlist via menu to display all playlist options (you'll have to customize this part)"
list := list . "`n" . "* Media_Play_Pause toggles playback and shows Now Playing information in a tool tip"
list := list . "`n" . "* CTRL + Media_Play_Pause just gives Now Playing information in a tool tip"
list := list . "`n" . "* Shift + CTRL + Media_Play_Pause gives Now Playing information in a tool tip and pastes to clipboard"
list := list . "`n" . "* Alt + Shift + CTRL + Media_Play_Pause gives Now Playing information in a tool tip and pastes artists to clipboard"
list := list . "`n" . "* Media_Next shows song info when going to next track"
list := list . "`n" . "* CTRL + Next seeks forward ~10 seconds"
list := list . "`n" . "* Shift + Next seeks forward ~30 seconds"
list := list . "`n" . "* Next & Prev additionally show song info when going to next/previous track"
list := list . "`n" . "* CTRL + Prev seeks backwards ~10 seconds"
list := list . "`n" . "* Shift + Prev seeks backwards ~30 seconds"
list := list . "`n" . "* CTRL + Media button adds currently playing song to specified playlist(s) (you'll have to customize this part)"
list := list . "`n" . "* Shift + Media button adds currently playing song to specified playlist(s) (you'll have to customize this part)"
list := list . "`n" . "* CTRL + Shift + Media button adds currently playing song to specified playlist(s) (you'll have to customize this part)"
list := list . "`n" . "* Media button adds currently playing song to specified playlist(s) (you'll have to customize this part)"
ToolTip, %list%
Return
; Helps display Tooltip
TOOLTIP:
ToolTip,
SetTimer,TOOLTIP,Off
Return
; Add the song in CurrentPlayback to the playlist "name" identified by playlistID
addSongToPlaylist(playlistID, name){
spotifyObject := new Spotify
CurrentPlayback := spotifyObject.Player.GetCurrentPlaybackInfo()
ToolTip,% "Adding the song """ CurrentPlayback.Track.Name """ by " CurrentPlayback.Track.artists[1].name " to your " name " playlist"
SetTimer,TOOLTIP,On
playlistObject := spotifyObject.Playlists.GetPlaylist(playlistID)
playlistObject.AddTrack(CurrentPlayback.Track.ID)
ToolTip,% "Added the song """ CurrentPlayback.Track.Name """ by " CurrentPlayback.Track.artists[1].name " to your " name " playlist"
SetTimer,TOOLTIP,On
}
; Displays information about the current song (title, artists, album, progress)
showSongInfo(CurrentPlayback)
{
artistString := ""
for index, element in CurrentPlayback.Track.artists
{
artistString .= ", " . CurrentPlayback.Track.artists[index].name
}
artistString := substr(artistString,2)
songInfo := "Title: " CurrentPlayback.Track.Name " `nArtist: " artistString " `nAlbum: " CurrentPlayback.Track.album.name " `nProgress: " floor(CurrentPlayback.progress_ms/1000/60)":"round(mod(CurrentPlayback.progress_ms/1000,60)) " - " floor(CurrentPlayback.Track.duration/1000/60)":"round(mod(CurrentPlayback.Track.duration/1000,60))
ToolTip,%songInfo%
SetTimer,TOOLTIP,On
return songInfo
}
; Returns artist(s) of the current song
returnArtist(CurrentPlayback)
{
artistString := ""
for index, element in CurrentPlayback.Track.artists
{
artistString .= ", " . CurrentPlayback.Track.artists[index].name
}
artistString := substr(artistString,2)
songInfo := "Title: " CurrentPlayback.Track.Name " `nArtist: " artistString " `nAlbum: " CurrentPlayback.Track.album.name " `nProgress: " floor(CurrentPlayback.progress_ms/1000/60)":"floor(mod(CurrentPlayback.progress_ms/1000,60)) " - " floor(CurrentPlayback.Track.duration/1000/60)":"floor(mod(CurrentPlayback.Track.duration/1000,60))
ToolTip,%songInfo%
SetTimer,TOOLTIP,On
return artistString
}
; Build Spotify playlist menu
playlistMenu(){
; Default option at top of menu is to display song information
spotifyObject := new Spotify
CurrentPlayback := spotifyObject.Player.GetCurrentPlaybackInfo()
boundParam := Func("showSongInfo").Bind(CurrentPlayback)
; Construct the rest of the menu
Menu, convert, Add
Menu, convert, Delete
Menu, convert, Add, Add the currently playing Spotify song to a playlist..., % boundParam
Menu, convert, Add,
Menu, convert, Add, &Enter your playlist name here, SPOTIFY_MENU_ACTION
Menu, convert, Add, &Enter your playlist2 name here, SPOTIFY_MENU_ACTION
Menu, convert, Default, Add the currently playing Spotify song to a playlist...
Menu, convert, Show
Return
}
; Return playlist ID by playlist name
getPlaylistID(playlistName){
If (playlistName == "&Starred"){
return "id"
} Else If (playlistName == "&Enter your playlist name here"){
return "enter id of your playlist here"
} Else If (playlistName == "&Enter your playlist2 name here"){
return "enter id of your playlist2 here"
}
}
; Pass selection to Spotify API to be added to playlist
SPOTIFY_MENU_ACTION(){
playlistID := getPlaylistID(A_ThisMenuItem)
addSongToPlaylist(playlistID, A_ThisMenuItem)
Return
}
;
; Caveats
;
; The script will add duplicate songs to a playlist without checking and the Spotify API won't stop me. Tried to implement logic [If NewSchool.tracks [not] contains CurrentPlayback.Track.ID] but it looks like this only checks the first 100 tracks returned from the playlist. However I noticed this puts me over the 10k song limit? Starred2 has over 10k songs adding songs using this script while adding them via the Spotify player gives me an error message.
;
; CTRL + Play was supposed to shuffle devices
; This API appears to be bugged because it only returns my current active playback device, not my other available Devices
; https://developer.spotify.com/console/get-users-available-devices/
; https://developer.spotify.com/documentation/web-api/guides/using-connect-web-api/#devices-not-appearing-on-device-list
; https://community.spotify.com/t5/Spotify-for-Developers/v1-me-player-devices-returns-empty-array/m-p/5224904
; https://github.com/spotify/web-api/issues/671
; ^Media_Play_Pause::
; CurrentPlayer := spotifyObject.Player
; Devices := CurrentPlayer.GetDeviceList
; Sleep, 1
; [...]
; Return
;