Wow thanks for this script! For different versions of spotify this will have problems for sure.. only because spotify sux.
The version I am running 1.0.86.337.ga8d5cef9 does not work with just your script. I had to make the following adjustments:
1) Always close spotify at the beginning (to ensure tab selection is reset)
2) Just pressing enter does not play the playlist. Need to shift tab 3x times, press enter, then press tab 2x times, then press enter.
Also the retry was triggering even though the song was playing, so I got rid of that.
I also added the ability to pass in a parameter to play a playlist
;******************************************************************
;Spotify Automation: Start spotify and play a playlist
;v1.01 27/04/2018
;------------------------------------------------------------------
;by Daniel Barnes
;------------------------------------------------------------------
;v1.01 27/04/2018
;restarts spotify if it fails the first time
;works on computers that minimize Spotify to tray
;instead of closing it, and have a different ui element selected
;such as the Devices available option when spotify is started
;------------------------------------------------------------------
;v1.00 25/04/2018
;initial version
;------------------------------------------------------------------
Local $uriIndex[][2] = [ _
["OFF", "OFF"], _
["playlistname1", "spotify:user:user:playlist:UIRCODE"], _
["playlistname2", "spotify:user:user:playlist:UIRCODE"], _
["playlistname3", "spotify:user:user:playlist:UIRCODE"] _
]
;Replace the below with the Spotify URI
;If you right click the playlist> share> copy spotify uri
;you can find the uri
$spotifyURI = ""
;******************************************************************
;if this script breaks in the future, figure this out using the AutoIt Window Info Tool
Const $SpotifyWindowIdentifier = "[Title:Spotify;Class:Chrome_WidgetWin_0]"
;NOTE ABOUT SHUFFLE:
;I couldn't automate turning shuffling on or off
;I can toggle it, but I cannot figure out the current state
;If you want to shuffle, turn on shuffle in Spotify prior
;to running this script
if($CmdLine[0] == 0) Then
MsgBox(0, "Spotify Au3 Script Error", "No Parameters")
ElseIf($CmdLine[0] >= 1) Then
Local $spotifyURI = ""
for $i = 0 To UBound($uriIndex)-1 Step 1
if($CmdLine[1] = $uriIndex[$i][0]) Then
$spotifyURI = $uriIndex[$i][1]
ExitLoop
EndIf
Next
if($spotifyURI = "") Then
MsgBox(0, "Spotify Au3 Error", "Unable to find spotify uri with key: " & $CmdLine[1])
Else
if($spotifyURI = "OFF") Then
ProcessClose("Spotify.exe")
Else
PlaySpotifyPlaylist($spotifyURI)
Endif
EndIf
EndIf
Func PlaySpotifyPlaylist($spotifyURI,$retry = 0)
;terminate spotify process on subsequent attempts
;If $retry Then
echo ("Closing existing Spotify process")
ProcessClose("Spotify.exe")
;endif
echo ("Launching Spotify URI")
ShellExecute($spotifyURI)
;Pause any existing song playing (if there is any)
;this is because Spotify's window title is only 'Spotify' when it is not playing any songs
If Not WinExists($SpotifyWindowIdentifier) Then
echo ("Pausing existing song (To find spotify window)")
$timer = TimerInit()
While Not WinExists($SpotifyWindowIdentifier)
Send("{MEDIA_PLAY_PAUSE}")
Sleep(500)
If TimerDiff($timer) > 5 * 1000 Then ErrorMsg ("Timed out trying pause existing song")
WEnd
endif
;Get Spotify Window Handle
$hwndSpotify = WinGetHandle($SpotifyWindowIdentifier)
echo ("Attempting to play playlist")
$timer = TimerInit()
;While WinGetTitle($hwndSpotify) = "Spotify" ;once it is playing the song, the title changes to the name of the song
Sleep(5000)
WinActivate($hwndSpotify)
;ControlSend($hwndSpotify,"","","{ENTER}") ;space will play what was previously playing (even another playlist or song), enter will play the playlist ;)
ControlSend($hwndSpotify,"","","+{TAB}")
ControlSend($hwndSpotify,"","","+{TAB}")
ControlSend($hwndSpotify,"","","+{TAB}")
ControlSend($hwndSpotify,"","","{ENTER}")
ControlSend($hwndSpotify,"","","{TAB}")
ControlSend($hwndSpotify,"","","{TAB}")
ControlSend($hwndSpotify,"","","{ENTER}")
;Sleep(1000)
;If TimerDiff($timer) > 5 * 1000 Then
; If $retry Then
; ErrorMsg ("Timed out trying to play playlist")
; else
; Return PlaySpotifyPlaylist($spotifyURI,$retry+1)
; endif
;endif
;WEnd
EndFunc
Func echo($text)
ConsoleWrite ($text&@CRLF)
EndFunc
Func ErrorMsg($text)
MsgBox(16,StringTrimRight(@ScriptName,4),$text)
Exit
EndFunc