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 WilliamasKumeliukas
      2021.07.15 News: Project is NOT dead! I am still very devoted into accomplishing this project, I am spending my every bit of energy that is left end of the day (when there is any) on this project and it really does challenge my knowledge in AutoIT language but I won't give up  
       
      Hi,
      I started this project alone in May 2020 as project in my spare time at work, I'm working for a IT company that started opening their services to residential customers few months ago and now my position in the company kind of drifted in the doom and gloom world of repetitive tasks like: Reinstallation + Configuration of Windows 10.
      The procedure is very repetitive and I started feeling like being a robot which is the main reason I started this project.
       
      ==============================FAQ==================================
      1. Q: Do you want this project to be accomplished with the usage of AutoIT ONLY or 3rd party tools / Scripts (BATCH / POWERSHELL / VB) ? A: No, if I cannot find a way using AutoIT to accomplish a task I will move to my Plan B which consist of automating an 3rd party tool to accomplish the affected task until a solution is found. 2. Q: What do I get from helping/collaborating in this project? A: I will personally take the responsability to mention you in the credits of this project. 3. Q: If I have more questions, can I ask? A: Certainly! feel free to ask any questions related to this project! 4. Q: What is the main goal of this project? A: Automating Windows 10 configuration without user interaction needed (as much as possible) ______________________________________________________________________________________________________________________________
      Current progression of the project (more will be added in future)
      « Blue = Info || Yellow = Unfinished/Untested || Purple = Could be better || Green = Done ||Red = Not Yet Started »
      ***Very early Stage ***
      Connect Network Attached Storage(NAS) (Work but missing configuration in GUI - AutoIT only)
      Download & Install up to 600+ softwares (Tested & Working - using 3rd party tool + Powershell)
       Auto prediction of Apps name of text typed inside input (Tested & Working - AutoIT Only)
      Change OEM Informations (Tested & Working -  AutoIT)
      Disable ScreenSaver (Work but require testing AutoIT only)
      Change Computer Name (Work but require testing AutoIT only) 
      Show Computer Information and Smart status on GUI (Tested & Working - AutoIT Only)
      Change .pdf / .pdfxml from Edge to Adobe Reader DC (Tested & Working - using 3rd party tool)
      Change Edge to Google Chrome as Default Browser (Tested & Working - using 3rd party tool)
      Windows Updater (Seems to work but require further testing - AutoIT only)
      Install Office 365 / 2013 + Activation (To Do)
      Add L2TP VPN Configuration in Windows Built-in VPN (To Do)
      Save / Load tasks configuration profile in (.ini file) to avoid repeating same configuration twice (In progress - AutoIT Only)
      (EXPERIMENTAL) Install Apps from Microsoft Store with UIAutomation UDF made by: @junkew(Work if you know what your doing)
         P.S: Installing Apps from Microsoft Store will require usage of  UIA spy tool made by @LarsJ which you can download & learn how to use it on UIA Spy Tool thread.
      ***  If this project interest you, Reply here This will greatly help me to see if you'd like this project to become real  ***
       
      Download: W10-Configurator.zip  (2021-07-19) 
      Project Website: W10-Configurator on GitHub 
      ______________________________________________________________________________________________________________________________
      Best Regards,
      ~WilliamasKumeliukas
    • 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 abberration
      Software Installer
      version 1.2 - Jan 2, 2021
      Hello, everyone!
      Today, I present to you a new version of Software Installer. This script is designed to be used on CDs/DVDs/USB drives where you put software in a folder (called Software) and you can select multiple software and it will automate installing your selected software one after another with little to no interaction. To do this, you simply need to configure each software with a silent switch (most software have one or more). You can put your software in categories to organize them on the main GUI.
      Screenshot:

      The major new things concerning this project:
      Added a Detect button, where the software tries to detect what kind of installer is used, saving you from having to find out on your own. It's not perfect, but it detects many common installer types. Added a Installers.db file for installers that were not created by the most common methods (Inno Setup, NSIS, etc). I will continue to add to this file. If you have any that you find that you would like to contribute, please PM. I would very much appreciate your help. This version searches for all subfolders now and shows .exe and .msi files that you may not want to see on the main screen. Therefore, I have added the option to "Hide This Item On Main GUI". Added the ability to install up to 2 software before and up to 2 software or registry entries after installing your selected software. This will allow you to install things like service packs, DotNet, etc. (prerequisites). And it will allow you to apply your licensing information through registry entries or patches/updates. If you want the full package (source code, compiled .exe (both 32/64 bit), changelog, installers.db, icon, etc.), you can download it here:
      https://www.filefactory.com/file/2o2enxmm73qo/Software_Installer_v.1.2.zip
      I have a lifetime account at FileFactory, so the link should never die and you do NOT need an account or pay them money to download. Simply choose the free/slow download option. The file is small and will still download fairly quickly. If you have trouble downloading it, PM me. I have the previous versions of this script, but they are inferior to this version, so if you want the older versions, PM me and I will provide them.
      If you just want the bare minimum, the script, installers.db and changelog are attached on this forum below.
      Enjoy!
      Software Installer.au3 Installers.db Changelog.pdf
    • 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...