Jump to content

Recommended Posts

;******************************************************************
;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
;------------------------------------------------------------------

;Replace the below with the Spotify URI
;If you right click the playlist> share> copy spotify uri
;you can find the uri
$spotifyURI = "spotify:user:12177300374:playlist:0iaTAvHRSaIkD23ofl2tck"
;******************************************************************

;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

PlaySpotifyPlaylist($spotifyURI)

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
        WinActivate($hwndSpotify)
        ControlSend($hwndSpotify,"","","{ENTER}") ;space will play what was previously playing (even another playlist or song), enter will play the playlist ;)
        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

 

Edited by boomingranny
v1.01
Link to post
Share on other sites
  • 3 months later...

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

 

Link to post
Share on other sites
  • 4 months later...

Thanks for the scripts above - I have been using them extensively from my home automation system.

I just noticed that it wasn't working again.  I played around and came up with the following combination when loading a URI to get it to play:

   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}")
 

Not sure if this is the most elegant way to do this but it works.

Link to post
Share on other sites
  • 2 weeks later...

Thanks Simon!  Noticed mine was not working and made your changes.  I had to add some sleeps between the commands... maybe because the computer is a bit slow.... but it works!

Yeah, the Ctrl Right arrow never worked for me either.  Honestly I am kinda fed up with Spotify's crappy client.  You can find and download older versions of the client that have better automation capability ( and disable upgrading ).  I might do that again to avoid having to update scripts.  I used to run a script on an older spotify client (pre- 1.0) that would select the current song, grab its name (for writing to a file) and delete it.  Was cool for "blowing up" songs.

Link to post
Share on other sites
  • 3 weeks later...

ok, it stopped working again so I started playing with it.  I decided to go in another direction.  I am now doing the following:

   ControlSend($hwndSpotify,"","","^s")
   ControlSend($hwndSpotify,"","","^r")
   MouseClick($MOUSE_CLICK_LEFT, 400, 250, 2)

This is clicking the PLAY button after turning on shuffle and repeat.  The playlist is loaded in the command line.

This works most of the time.  Weirdly it seems to fail when I haven't played Spotify in a while.  But if I repeat the process all over again (loading spotify and issuing these commands) then it works.  I am trying to figure out what happens when Spotify is not loaded for a while.

Also, the X,Y is set for resolution my server runs at 1024x768.  It needs to be adjusted according to resolution.

One thing that I would like to do is test the PLAY/PAUSE button for state.  That would allow me to see if the click has worked.  I am not that familiar with autoit so if someone wants to jump in and help in identifying the button and how to test for the state - PLAY or PAUSE - I could always use the help.  I have attached the script, not the prettiest thing but I usually leave all of the commands in it that have failed so I don't go over old ground again.

 

As an aside they have improved their API - https://developer.spotify.com/documentation/web-api/reference-beta/ - I am not that familiar with this approach but I guess I will need to start learning how to do it.

SpotifyLauncher.au3

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By abberration
      Software Installer
      Version: 2.0
      It's been been a long year, but I finally got some time to rework this project. I re-wrote everything from scratch because the old version was getting too complicated with so many options and sub-menus. This new version is much easier to use and I have been testing for a few days and it seems very stable.
      For those who are new to this software, it helps you install software silently/unattended. This new version tries to determine the silent switch automatically. You can also re-organize the order in which the software installs by dragging & dropping them in the listview. It now supports creating profiles and checks for missing software (and automatically unchecks them, so it does not attempt to install non-existent software). One feature I included was because I have seen several people on Youtube talk about disliking bright screens at night. So, now you can choose from a few color theme (half of them are dark). I dabbled a bit more into GDI+ to draw a few things and show my logo with a transparent background (hint: I'm not good at GDI+).
      Under the Help menu, you will find a User Guide, which goes through most of it's features. I included a new icon if you want to use when you compile the script (in the Assets > Misc folder).
      If you have questions, comments or suggestions, all are welcome. Hope you enjoy!
      Here it is in action:

       
      Software_Installer_2.0.zip
    • By fire35
      Hello. I need to write a User Script to add videos to created playlists. On the video watch page on the right, there are recommended videos.
      When cursor on thumbnail upper-right on icon appears plus sign, when cursor on that plus sign appears list of user's playlists and by clicking on playlist name video adding to playlist and appears plus sign located right of playlist name. If video already in some playlist, when cursor on thumbnail plus sign, plus sign already located right of playlist name.
      I think it must be like JS function copy thumbnail, link and video name, after adding that on playlist page. Playlist page initially is some cleared page of site, for example page that doesn't exist - when you put wrong address that's written. User Script saves data to appear it on that page.
      On the playlist page, you can remove videos from it. There is also a page set in the code where you can create or delete a playlist. When creating, along with the name, the address of an empty page is set to which the video will be added.
      On the video viewing page, a plus sign is also needed under it so that this video can be added.
      This is the code for the thumbnail on the right of the page.
      <div id="node-1001410" class="node node-video node-sidebar_teaser clearfix" data-toggle="tooltip" data-placement="top" title="" data-original-title="VIDEO NAME">
          <div>
              <div class="icon-bg">
                                  <div class="right-icon likes-icon">
                          <i class="glyphicon glyphicon-heart"></i> 795                </div>
                  
                  <div class="left-icon likes-icon">
                      <i class="glyphicon glyphicon-eye-open"></i> 37.7k            </div>
                          </div>
              <div class="field field-name-field-video field-type-video field-label-hidden"><div class="field-items"><div class="field-item even"><a href="/videos/gf45ugh5ca"><img src="//Full IMG Address" width="141" height="84" alt="VIDEO NAME" title="VIDEO NAME"></a></div></div></div>
                  </div>
          </div>
      <div>
      On the video viewing page, to add to the playlist, you need to extract the poster address from part of the code
      <div poster = "//Full IMG Address" ...>
      Title from here 
      <h1 class = "title"> VIDEO NAME </h1>
      I think it's worth copying the number of likes and views from here
      <div class="node-views"><i class="glyphicon glyphicon-heart"></i> 380 <i class="glyphicon glyphicon-eye-open"></i> 17,716 </div>
      I rely on you.
    • By rcmaehl
      A UDF to connect and control Five9 on the Local Network via it's provided HTTP API
       
      THIS UDF IS STILL IN TESTING
       
      Current Features:
      Agent Ready
      * Contains most functions needed to control Agent Status  
      PLANNED Features:
      Heavyweight
      * Control most Five9 API functions
      * Full Control over own Status as well as Supervisor functionality
        Helpful Premade Examples Files
      * Examples for almost every function
      * Premade CUI and GUI clients
        Ready for all users, beginner and up
      * Detailed Error Reporting to help pin-point problems
      * Thorough parameter checking to prevent accidental script crashes  
      Support:
      Support for this UDF can be obtained in my Discord Server
    • By rcmaehl
      A UDF to connect and control Cisco Finesse on the Local Network via it's provided HTTP API
       
      Current Features:
      Heavyweight
      * Large feature set, containing most Finesse Desktop API functions  
      PLANNED Features:
      Heavyweight
      * Control over own Status as well as Supervisor functionality
        Helpful Premade Examples Files
      * Examples for almost every function
      * Premade CUI and GUI clients 
        Ready for all users, beginner and up
      * Detailed Error Reporting to help pin-point problems
      * Thorough parameter checking to prevent accidental script crashes  
      Support:
      Support for this UDF can be obtained in my Discord Server
    • By WindIt
      Hi, 
      I have a set of excel docs 1& 2
      1) In Excel 1 i have a fixed column called dimensionId that contains list of dimensionid's with in between empty cells.
      Here, For each dimesionId, i need to check corresponding columns of L,M,N ,O(senior, mid, junior, student) & check If they contain letter Y (Open excel 2) and if empty ignore,check next.
      I need to loop through the entire column of dimensionId one by one, please help me proceed with the logic.
      2) If letter Y exists, open excel 2 and check if same dimesionId exists in column A.
      New to autoit excel automation, Help me with logic to execute this.
      #include <Excel.au3>
      Global $oExcel = _Excel_Open()
      Global $oWorkbook = _Excel_BookOpen($oExcel, "D:\yta\Trial concept dimensions list.xlsx")
      Local $read1 = _Excel_RangeRead ($oWorkbook,Default,"C3")
      MsgBox(0,"",$read1)
      Attached exce 1 doc.
       
       
      exl1.xlsx exl2.xlsx
×
×
  • Create New...