"Like" Spotify Song script doesn't work

Get help with using AutoHotkey and its commands and hotkeys
reverberation
Posts: 114
Joined: 13 Dec 2015, 20:48

"Like" Spotify Song script doesn't work

04 May 2019, 20:49

I made a script using imagesearch to find the "heart" button and click on it, but for some reason it is not activating. any idea why?

Code: Select all

; Spotify "Star Song"
f8::
spotify = ahk_class SpotifyMainWindow
IfWinExist, %spotify%
{
;Store active window and mouse position.
WinGetActiveTitle, activeWindow
MouseGetPos x, y, winID
SysGet, Monitor, Monitor, %A_Index% ;Get Monitor Resolution 
screenWidth = %MonitorRight%
screenHeight = %MonitorBottom%

;Activate Spotify.
WinActivate %spotify%
WinWaitActive %spotify%
WinMaximize %spotify%

heart = SpotifyHeart.PNG
ImageSearch, X, Y, 0, 0, %screenWidth%, %screenHeight%, *100, %heart%
MouseClick, left, X, Y
MouseMove, X, Y-10

;Restore original window and mouse position.
WinActivate ahk_id %winID%
MouseMove %x%, %y%
}

Return
User avatar
gregster
Posts: 2291
Joined: 30 Sep 2013, 06:48

Re: "Like" Spotify Song script doesn't work

04 May 2019, 23:36

A few things to consider:
  • what does Errorlevel look like after the Imagesearch, was it successful?
  • Imagesearch gives you the coordinates of the upper-left pixel of where the image was found on the screen - perhaps you need to adjust these coords for a few pixels so that you click the middle of the "like" symbol instead of somewhere around its outer border (why do you move the mouse by 10 pixels after the click and not before?)
  • For performance, it would probably be good if you reduced the search area a bit instead of searching the whole screen ("Coordinates are relative to the active window unless CoordMode was used to change that." ). You could probably limit the search to the lower part of the Spotify window, I imagine.
  • At the end, you want to restore the original mouse coords after you saved them at the start in the variables x and y - but then you overwrite them with the result of the Imagesearch when you use the same variable names...

Also, there is a syntax error in your Imagesearch line:
https://autohotkey.com/docs/commands/ImageSearch.htm#Parameters wrote:Options: Zero or more of the following strings may be also be present immediately before the name of the file. Separate each option from the next with a single space or tab. For example: *2 *w100 *h-1 C:\Main Logo.bmp
Remove the comma between *100 and %heart%:

Code: Select all

ImageSearch, X, Y, 0, 0, %screenWidth%, %screenHeight%, *100 %heart%

Then, where does A_index come from in SysGet, Monitor, Monitor, %A_Index% ? There is no loop over the monitors... please compare Example #2 in the SysGet docs
CloakerSmoker
Posts: 12
Joined: 07 Sep 2018, 15:54

Re: "Like" Spotify Song script doesn't work

05 May 2019, 00:20

I tried to a similar thing a while ago, and automating the Spotify client is a bit of a nightmare.
One thing is that you seem to have the WinTitle wrong (At least if you've got the newest version of the client), the window that can be activated/takes input isn't actually the main window, instead it has its title set as either the title of the currently playing song, or just "Spotify" with the class "Chrome_WidgetWin_0".
Here's the regex WinTitle I used for that

Code: Select all

.*-.*|Spotify ahk_class Chrome_WidgetWin_0
which should help you with actually targeting the window you need.

Automating the client got to be so annoying though after I figured that WinTitle out that I dropped the idea and wrote a library for automating Spotify through their web API which you might be able to use instead of ImageSearch and clicks.
You can find it here (https://github.com/CloakerSmoker/Spotify.ahk), it doesn't use any mouse movements and stuff, so it might be less intrusive too.
Here's how you'd "Like" a song using that:

Code: Select all

Spoofy := new Spotify()
F1::Spoofy.Player.SaveCurrentlyPlaying()
reverberation
Posts: 114
Joined: 13 Dec 2015, 20:48

Re: "Like" Spotify Song script doesn't work

12 May 2019, 06:11

Hi,

I tried running your script but there's a missing JSON library.

Tried downloading from here but it doesn't seem to be the right one: https://github.com/cocobelgica/AutoHotkey-JSON

Able to advise please?
User avatar
gregster
Posts: 2291
Joined: 30 Sep 2013, 06:48

Re: "Like" Spotify Song script doesn't work

17 May 2019, 21:02

I haven't used this library, but I assume that to make it work you need at least the lib folder and its contents from Github (https://github.com/CloakerSmoker/Spotify.ahk) - there is also a JSON.ahk library included.

Return to “Ask For Help”

Who is online

Users browsing this forum: scriptors and 211 guests