"Like" Spotify Song script doesn't work

Get help with using AutoHotkey and its commands and hotkeys
reverberation
Posts: 118
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
gregster
Posts: 2658
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: 118
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?
gregster
Posts: 2658
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.
stalegjelsten
Posts: 1
Joined: 08 Jun 2019, 12:20

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

08 Jun 2019, 12:28

I have made my own solution to this (didn't find this thread until after I had a mostly working solution :? ).

Very messy code, and it only works for about 90 % of songs since imagesearch is not always able to find a match. Any feedback is appreciated.

Code: Select all

SpotifyLove(flag)
{
	; Save the current window and mouse position so we can revert back after controlling Spotify.

	WinGetActiveTitle, CurOpen
	MouseGetPos, MouseX, MouseY

	IfWinExist ahk_exe Spotify.exe ; check if spotify is open
		winactivate ahk_exe Spotify.exe
	else
		return
	WinWait ahk_exe Spotify.exe
	
	Click, 65, 65 ; Spotify has a 60x60 pixel window with the menu in the top left corner. I have not find another way to activate the the main window except for this click. Any other solutions to are much appreciated.

	WinGetPos, X, Y, W, H, A ; get the dimensions of the spotify window

	; start searching for image in a rectangle. The first point of the rectangle X1,Y1 is 100 pixels above the bottom left corner. The other point of the rectangle X2,Y2 is 600 pixels right of the left edge and along the bottom of the window.

	X1 := 0
	X2 := 600
	Y1 := H - 100
	Y2 := H

	; imagesearch is really crappy. *70 gives the function some leeway to find the right image in the window. We are searching for the heart. 

	ImageSearch, OutputVarX, OutputVarY, X1, Y1, X2, Y2, *70 C:\path\to\heart.jpg

	; click on heart if we like the song, click on the dislike button and "i dont like this song" otherwise.

	if (flag == 1)
	{
		Click, %OutputVarX%, %OutputVarY%
	} else {
		HateX := OutputVarX+50
		DontLikeSongX := HateX+10
		DontLikeSongY := OutputVarY-10
		Click, %HateX%, %OutputVarY%
		Sleep, 200
		Click, %DontLikeSongX%, %DontLikeSongY%
	}

	; return to previous window with the same mouse position. 
	WinActivate, %CurOpen%
	MouseMove, %MouseX%, %MouseY% 
}

CapsLock & s::
SpotifyLove(1)
Return

CapsLock & d::
SpotifyLove(0)
Return

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], flyingDman, Geriko, Google [Bot], Odius, RsTxSpyMaster, Worly and 148 guests