;****************************************************************** ;Spotify Automation: Various Commands for Spotify ; ; 11-17-18 - Disabling the random and repeat controls as the Spotify app seems to remember these settings so this is not needed. ; Added command line: ; shuffle - turn shuffle on ; spotify - only plays playlist - does not not turn on shuffle or repeat ; ; 9-2-18 - Created my first version ; ; Syntax ; Options: ; OFF - Shutsdown spotify ; THIS IS NOT PROGRAMMED: spotify - Starts up Spotify and sends shuffle toggle and repeat toggle ; spotify:user:simondmason:playlist:7xvKCqIvIRtqYinBmUJj6G - loads spotify and plays the specified playlist ; ; Removed from else statement below as I wasn't using this command: ; Elseif($spotifyURI = "shuffle") Then ; LoadSpotifyShuffle($spotifyURI) ; MsgBox(0, "Spotify Au3 Script Debug", "Shuffle") ; ;Old notes: ;v1.01 27/04/2018 ;------------------------------------------------------------------ ;Originally 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 ;------------------------------------------------------------------ #include Local $uriIndex[][2] = [ _ ["OFF", "OFF"], _ ["playlistname1", "spotify:user:user:playlist:UIRCODE"], _ ["playlistname2", "spotify:user:user:playlist:UIRCODE"], _ ["playlistname3", "spotify:user:user:playlist:UIRCODE"] _ ] ;if this script breaks in the future, figure this out using the AutoIt Window Info Tool Const $SpotifyWindowIdentifier = "[Title:Spotify;Class:Chrome_WidgetWin_0]" if($CmdLine[0] == 0) Then ;MsgBox(0, "Spotify Au3 Script Error", "No Parameters") echo ("No parameter specified") ElseIf($CmdLine[0] >= 1) Then ;MsgBox(0, "Spotify URI", "Incoming URI: " & $CmdLine[1]) echo ("Received a parameter") $spotifyURI = $CmdLine[1] if($spotifyURI = "") Then ; MsgBox(0, "Spotify URI", "Incoming URI: " & $spotifyURI) MsgBox(0, "Spotify Au3 Error", "Incoming URI is blank: " & $CmdLine[1]) echo ("Incoming parameter is blank") Else if($spotifyURI = "OFF") Then ; Sending commands to turn off shuffle and repeat echo ("Turning Spotify off") echo ("Getting spotify window handle") $hwndSpotify = WinGetHandle($SpotifyWindowIdentifier) ControlSend($hwndSpotify,"","","^s") ControlSend($hwndSpotify,"","","^r") ControlSend($hwndSpotify,"","","^r") ProcessClose("Spotify.exe") ; MsgBox(0, "Spotify Au3 Script Debug", "Closing Spotify") echo ("Received OFF - closing spotify") Else echo ("Loading Spotify") PlaySpotifyPlaylist($spotifyURI) ; MsgBox(0, "Spotify Au3 Script Debug", "Play") echo ("Received a parameter - sending it to PlaySpotifyPlaylist") Endif EndIf EndIf Func PlaySpotifyPlaylist($spotifyURI,$retry = 0) ;terminate spotify process on subsequent attempts ;Make sure that Spotify is not running... echo ("Closing existing Spotify process") ProcessClose("Spotify.exe") ;load spotify by using the URI - this will launch spotify and load the playlist 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 echo ("Getting spotify window handle") $hwndSpotify = WinGetHandle($SpotifyWindowIdentifier) echo ("Attempting to play playlist") $timer = TimerInit() 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}") ;+ is for shift ;ControlSend($hwndSpotify,"","","+{TAB}") ;ControlSend($hwndSpotify,"","","+{TAB}") ;ControlSend($hwndSpotify,"","","{ENTER}") ;ControlSend($hwndSpotify,"","","{TAB}") ;ControlSend($hwndSpotify,"","","{TAB}") ;ControlSend($hwndSpotify,"","","{ENTER}") ; Sending space to play the currently loaded playlist ; space will play what was previously playing (even another playlist or song) ;ControlSend($hwndSpotify,"","","{SPACE}") ; Sending commands to turn shuffle and repeat on ControlSend($hwndSpotify,"","","^s") ControlSend($hwndSpotify,"","","^r") ; Sending commands to turn load playlist and play it ; +{TAB} - Shift Tab ;ControlSend($hwndSpotify,"","","{ENTER}") ;ControlSend($hwndSpotify,"","","+{TAB}") ;ControlSend($hwndSpotify,"","","+{TAB}") ;ControlSend($hwndSpotify,"","","+{TAB}") ;ControlSend($hwndSpotify,"","","+{TAB}") ;ControlSend($hwndSpotify,"","","{TAB}") ;ControlSend($hwndSpotify,"","","{TAB}") ;ControlSend($hwndSpotify,"","","{TAB}") ;ControlSend($hwndSpotify,"","","{TAB}") ;ControlSend($hwndSpotify,"","","{TAB}") ;ControlSend($hwndSpotify,"","","{ENTER}") ; Click on Play Button - 400 240 Sleep(5000) MouseClick($MOUSE_CLICK_LEFT, 400, 250, 2) ; Not used: ;ControlSend($hwndSpotify,"","","^s") ;ControlSend($hwndSpotify,"","","^r") EndFunc Func echo($text) ConsoleWrite ($text&@CRLF) EndFunc Func ErrorMsg($text) MsgBox(16,StringTrimRight(@ScriptName,4),$text) Exit EndFunc