Spotify PostMessage Library

Post your working scripts, libraries and tools
User avatar
king-of-hearts
Posts: 30
Joined: 01 Oct 2017, 06:29

Spotify PostMessage Library

05 Nov 2017, 08:13

Hello everybody,

I was just playing around with 'PostMessage' and found something cool. This commands works in Spotify even when it is Minimized

For now it can toggle play, change tracks and other things. I will mess with it later to find other things

Code: Select all

; PostMessage, 0x111, 114, 0, , ahk_class SpotifyMainWindow ; toggle play
; PostMessage, 0x111, 115, 0, , ahk_class SpotifyMainWindow ; next song
; PostMessage, 0x111, 116, 0, , ahk_class SpotifyMainWindow ; last song or replay
; PostMessage, 0x111, 117, 0, , ahk_class SpotifyMainWindow ; forward 7 seconds
; PostMessage, 0x111, 118, 0, , ahk_class SpotifyMainWindow ; backward 7 seconds
; PostMessage, 0x111, 119, 0, , ahk_class SpotifyMainWindow ; toggle shuffle
; PostMessage, 0x111, 120, 0, , ahk_class SpotifyMainWindow ; toggle repeat
; -----------------------------------------
; PostMessage, 0x111, 121, 0, , ahk_class SpotifyMainWindow ; increase volume
; PostMessage, 0x111, 122, 0, , ahk_class SpotifyMainWindow ; decrease volume
; -----------------------------------------
; PostMessage, 0x111, 129, 0, , ahk_class SpotifyMainWindow ; toggle private session
; PostMessage, 0x111, 130, 0, , ahk_class SpotifyMainWindow ; change to offline
; PostMessage, 0x111, 138, 0, , ahk_class SpotifyMainWindow ; restart spotify
; PostMessage, 0x111, 139, 0, , ahk_class SpotifyMainWindow ; toggle friend activity
; PostMessage, 0x111, 140, 0, , ahk_class SpotifyMainWindow ; create playlist
; PostMessage, 0x111, 141, 0, , ahk_class SpotifyMainWindow ; create playlist '2'
; -----------------------------------------
; PostMessage, 0x111, 32006, 0, , ahk_class SpotifyMainWindow ; last song or replay
; PostMessage, 0x111, 32007, 0, , ahk_class SpotifyMainWindow ; toggle play
; PostMessage, 0x111, 32008, 0, , ahk_class SpotifyMainWindow ; next song
; -----------------------------------------
; PostMessage, 0x111, 103, 0, , ahk_class SpotifyMainWindow ; about sptofy
; PostMessage, 0x111, 104, 0, , ahk_class SpotifyMainWindow ; close
; PostMessage, 0x111, 105, 0, , ahk_class SpotifyMainWindow ; settings
; PostMessage, 0x111, 110, 0, , ahk_class SpotifyMainWindow ; third party license
; PostMessage, 0x111, 111, 0, , ahk_class SpotifyMainWindow ; zoom in
; PostMessage, 0x111, 112, 0, , ahk_class SpotifyMainWindow ; zoom out
; -----------------------------------------
; PostMessage, 0x111, 65639, 0, , ahk_class SpotifyMainWindow ; about sptofy
; PostMessage, 0x111, 65640, 0, , ahk_class SpotifyMainWindow ; close
; PostMessage, 0x111, 65641, 0, , ahk_class SpotifyMainWindow ; log off
; PostMessage, 0x111, 65646, 0, , ahk_class SpotifyMainWindow ; third party license

Last edited by king-of-hearts on 05 Nov 2017, 16:51, edited 1 time in total.
https://autohotkey.com/boards/viewtopic.php?f=6&t=38707 - MS Access Manager - SQL Query: Incredible tool for MS Access/SQL Queries on the fly!!
User avatar
GollyJer
Posts: 56
Joined: 19 Sep 2015, 19:33
GitHub: GollyJer

Re: Spotify PostMessage Library

05 Nov 2017, 13:30

Nice! It would be awesome if you are able to track down "Add to My Library".
It would remove move the need for this complex image search and mouse click code.
User avatar
jeeswg
Posts: 5288
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Spotify PostMessage Library

06 Nov 2017, 15:28

I don't use Spotify but I thought I'd mention a few things:

For getting menu item IDs, you can use Resource Hacker to check inside the program for menu and accelerator resources:
e.g. C:\Windows\System32\notepad.exe or C:\Windows\System32\en-US\notepad.exe.mui

There's also this to show menu item IDs when you hover over menu items (note: using the menu item IDs with WM_COMMAND doesn't always work):
Get Info from Context Menu (x64/x32 compatible) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=31971

@king-of-hearts: It would be interesting to know how you discovered the menu item IDs. I know for some programs I just tried every single number until something happened.

@GollyJer: If the methods I mentioned don't help, you could try AccViewer and the Acc functions:
Acc library (MSAA) and AccViewer download links - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=26201
User avatar
davebrny
Posts: 70
Joined: 05 Dec 2016, 06:26

Re: Spotify PostMessage Library

09 Nov 2017, 00:06

nice one. it was only last month i was searching for hotkeys to move the track forward but they only work when spotify is focused. these work even when spotify is minimzed! autohotkey to the rescue again

i dont have much experience using postmessage or sendmessage though. doest one of those commands return which state the command is in after t switches? it would be useful to get some feedback on what state shuffle is in so it could be shown in a tooltip, or another good use case would be having a timer keep private mode on the whole time.
User avatar
jeeswg
Posts: 5288
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Spotify PostMessage Library

09 Nov 2017, 05:06

You could check the return value, which is stored in ErrorLevel, when you use SendMessage. PostMessage sends the message and doesn't wait, SendMessage sends the message and waits for a reply which is stored in the variable ErrorLevel.

Code: Select all

q:: ;Notepad - get undo state, get modified state
WinGet, hWnd, ID, A

;get undo state (is undo available: yes/no)
SendMessage, 0xC6, 0, 0, Edit1, % "ahk_id " hWnd ;EM_CANUNDO := 0xC6 ;get undo state
vUndoState := ErrorLevel ? "yes" : "no"

;get modified state (are there unsaved changes: yes/no)
SendMessage, 0xB8, 0, 0, Edit1, % "ahk_id " hWnd ;EM_GETMODIFY := 0xB8 ;get modified state
vModState := ErrorLevel ? "YES" : "no"

MsgBox, % "undo: " vUndoState ", changes: " vModState
return
You could also check to see what AccViewer retrieves.
Acc library (MSAA) and AccViewer download links - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=26201
User avatar
davebrny
Posts: 70
Joined: 05 Dec 2016, 06:26

Re: Spotify PostMessage Library

10 Nov 2017, 15:54

thanks for the info jeeswg
You could check the return value,
no luck there unfortunately, it just returns 0/no whether shuffle is on and off
You could also check to see what AccViewer retrieves
this is picking up a lot of different elements inside spotify anyway, but none of them have any control names, only text on a few of them

it detects the text "Save to Your Library" or "Remove from Your Library" and the xy position of that element, so at least theres the option to use mouseClick if the window is active

when private session is on it detects a "graphic", which is the lock symbol, but again no control name so im not sure if anything can be done with that


how do you go about getting the same elements that acc viewer shows?
the graphic for example is nested way down in the list: 4,1,4,1,4,1,1,1,1,1,2,1,1,1,4,1,1,1
User avatar
davebrny
Posts: 70
Joined: 05 Dec 2016, 06:26

Re: Spotify PostMessage Library

10 Nov 2017, 16:13

just tried opening spotify in resource hacker. ive never used it before so i dont know what these accelerator thingamabobs are but heres the list of them

Code: Select all

129 ACCELERATORS
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
{
	46434, 46428, CONTROL, SHIFT, VIRTKEY
	46459, 46393, ALT, CONTROL, SHIFT, ASCII
	46459, 46392, ALT, CONTROL, ASCII
	46447, 46394, ALT, CONTROL, SHIFT, ASCII
	46444, 46373, ALT, CONTROL, SHIFT, ASCII
	46445, 46390, ALT, CONTROL, SHIFT, ASCII
	46454, 46385, ALT, CONTROL, SHIFT, ASCII
	46454, 46376, ALT, CONTROL, ASCII
	46435, 46384, ALT, CONTROL, SHIFT, ASCII
	46491, 46387, ALT, SHIFT, ASCII
	46452, 46386, ALT, CONTROL, SHIFT, ASCII
	46452, 46397, ALT, CONTROL, ASCII
	46457, 46396, ALT, CONTROL, SHIFT, ASCII
	46451, 46395, ALT, CONTROL, SHIFT, ASCII
	46437, 46431, ALT, CONTROL, SHIFT, ASCII
	46350, 46426, ALT, CONTROL, SHIFT, ASCII
	46344, 46405, ALT, CONTROL, SHIFT, ASCII
	46469, 46404, ALT, CONTROL, SHIFT, ASCII
	46482, 46406, ALT, CONTROL, SHIFT, ASCII
	46480, 46401, ALT, CONTROL, SHIFT, ASCII
	46482, 46400, ALT, ASCII
	46480, 46403, ALT, ASCII
	46438, 46402, ALT, CONTROL, SHIFT, ASCII
	46439, 46413, ALT, CONTROL, SHIFT, ASCII
	46483, 46412, ALT, CONTROL, SHIFT, ASCII
	46493, 46415, ALT, CONTROL, SHIFT, ASCII
	46439, 46414, CONTROL, SHIFT, ASCII
	46450, 46409, CONTROL, SHIFT, ASCII
	46460, 46408, CONTROL, SHIFT, ASCII
	46454, 46377, CONTROL, SHIFT, ASCII
	46405, 46430, ALT, SHIFT, ASCII
}
User avatar
jeeswg
Posts: 5288
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Spotify PostMessage Library

10 Nov 2017, 16:53

Menu resources if present, might be better to look at.
In this accelerators example from Notepad, it's quite easy to follow what's going on.
E.g. VK_S, 3, CONTROL, VIRTKEY
This means that ctrl+s invokes menu item 3, which is Save.

The 'slipup' accelerator resource, which includes all of those listed in the 'main' accelerator resource.

Code: Select all

SLIPUPACC ACCELERATORS
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
{
	VK_N, 1, CONTROL, VIRTKEY
	VK_O, 2, CONTROL, VIRTKEY
	VK_S, 3, CONTROL, VIRTKEY
	VK_P, 6, CONTROL, VIRTKEY
	VK_INSERT, 769, CONTROL, VIRTKEY
	VK_F1, 64, VIRTKEY
	VK_F3, 22, VIRTKEY
	VK_F5, 26, VIRTKEY
	VK_G, 24, CONTROL, VIRTKEY
	VK_F, 21, NOINVERT, CONTROL, VIRTKEY
	VK_H, 23, NOINVERT, CONTROL, VIRTKEY
	VK_BACK, 16, ALT, VIRTKEY
	VK_Z, 16, CONTROL, VIRTKEY
	VK_X, 768, CONTROL, VIRTKEY
	VK_C, 769, CONTROL, VIRTKEY
	VK_V, 770, CONTROL, VIRTKEY
	VK_A, 25, CONTROL, VIRTKEY
	VK_ESCAPE, 7, VIRTKEY
	VK_C, 7, CONTROL, VIRTKEY
	VK_D, 7, CONTROL, VIRTKEY
	VK_Z, 7, CONTROL, VIRTKEY
}
The menu resource from Notepad:

Code: Select all

1 MENU
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
{
POPUP "&File"
{
	MENUITEM "&New\tCtrl+N",  1
	MENUITEM "&Open...\tCtrl+O",  2
	MENUITEM "&Save\tCtrl+S",  3
	MENUITEM "Save &As...",  4
	MENUITEM SEPARATOR
	MENUITEM "Page Set&up...",  5
	MENUITEM "&Print...\tCtrl+P",  6
	MENUITEM SEPARATOR
	MENUITEM "E&xit",  7
}
POPUP "&Edit"
{
	MENUITEM "&Undo\tCtrl+Z",  16
	MENUITEM SEPARATOR
	MENUITEM "Cu&t\tCtrl+X",  768,  GRAYED
	MENUITEM "&Copy\tCtrl+C",  769,  GRAYED
	MENUITEM "&Paste\tCtrl+V",  770,  GRAYED
	MENUITEM "De&lete\tDel",  771,  GRAYED
	MENUITEM SEPARATOR
	MENUITEM "&Find...\tCtrl+F",  21
	MENUITEM "Find &Next\tF3",  22
	MENUITEM "&Replace...\tCtrl+H",  23
	MENUITEM "&Go To...\tCtrl+G",  24
	MENUITEM SEPARATOR
	MENUITEM "Select &All\tCtrl+A",  25
	MENUITEM "Time/&Date\tF5",  26
}
POPUP "F&ormat"
{
	MENUITEM "&Word Wrap",  32
	MENUITEM "&Font...",  33
}
POPUP "&View"
{
	MENUITEM "&Status Bar",  27
}
POPUP "&Help"
{
	MENUITEM "View &Help",  64
	MENUITEM SEPARATOR
	MENUITEM "&About Notepad",  65
}
}
User avatar
jeeswg
Posts: 5288
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Spotify PostMessage Library

10 Nov 2017, 17:02

Some Acc examples:

Code: Select all

q:: ;Notepad - invoke Close button
WinGet, hWnd, ID, ahk_class Notepad
oAcc := Acc_Get("Object", "2", 0, "ahk_id " hWnd)
oAcc.accDoDefaultAction(5)
oAcc := ""
return

;compare with AccViewer: 4.1.5 is the path, 1 and 5 are the child IDs
w:: ;Notepad - invoke scroll down
WinGet, hWnd, ID, ahk_class Notepad
oAcc := Acc_Get("Object", "4.1.5", 0, "ahk_id " hWnd)
;oAcc.accDoDefaultAction(1) ;up
oAcc.accDoDefaultAction(5) ;down
oAcc := ""
return
User avatar
davebrny
Posts: 70
Joined: 05 Dec 2016, 06:26

Re: Spotify PostMessage Library

11 Nov 2017, 08:57

thanks again jeeswg, youre a great help! i never even knew any of this was possible with acc viewer
Nice! It would be awesome if you are able to track down "Add to My Library".
It would remove move the need for this complex image search and mouse click code.
heres a rough example of the same thing. its still complex enough but at least you dont need to use image search

Code: Select all

winGet, hWnd, ID, ahk_exe Spotify.exe
oAcc := Acc_Get("Object", "4.1.4.1.4.1.1.1.2.2.1.3.1.1.2", 0, "ahk_id " hWnd)
in_library := oAcc.accName(0)
acc_Location(oAcc, 0, button_position)
oAcc := ""
if inStr(in_library, "save to")  ; if not already in your library
    {
    winActivate, ahk_exe Spotify.exe
    button_position := regExReplace(button_position, "[\^a-z]")  ; take out xywh letters
    split := strSplit(button_position, a_space)
    coordMode, mouse, screen
    mouseClick, left, (split[1] + (split[3]/2)), (split[2] + (split[3]/2))
    }
return
User avatar
jeeswg
Posts: 5288
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Spotify PostMessage Library

11 Nov 2017, 09:13

- I'm pleased you've had some success.
- Btw usually with Acc, you don't have to use the MouseClick command, you can use something like oAcc.accDoDefaultAction(0), once you've got a handle to the right element, that will invoke a click. The number is either 0 or the child ID, sometimes oAcc.accDoDefaultAction() or oAcc.accDoDefaultAction will also work.
- And usually you can do this without having the window active, the one exception I've seen so far to this was Google Chrome, the default action would only occur if the window was active.
User avatar
davebrny
Posts: 70
Joined: 05 Dec 2016, 06:26

Re: Spotify PostMessage Library

11 Nov 2017, 11:50

And usually you can do this without having the window active
dayum! it works even with another window in front of it

Code: Select all

winGet, hWnd, ID, ahk_exe Spotify.exe
oAcc := Acc_Get("Object", "4.1.4.1.4.1.1.1.2.2.1.3.1.1.2", 0, "ahk_id " hWnd)
oAcc.accDoDefaultAction(0)
library_text := oAcc.accName(0)
if (library_text)
    {
    toolTip, % strReplace(library_text, " ", "d ", , 1)  ; change save to saved
    sleep 1500
    toolTip,
    }
oAcc := ""
return 
User avatar
GollyJer
Posts: 56
Joined: 19 Sep 2015, 19:33
GitHub: GollyJer

Re: Spotify PostMessage Library

14 Nov 2017, 21:21

Hey jeeswg. Thank you for all the insight and help!! :bravo:

I've almost got this working perfectly.

Code: Select all

AddSongToMyLibrary() {
  spotify := WinExist("ahk_class SpotifyMainWindow")
  If (spotify) {
    songTitle := Acc_Get("Object", "4,1,4,1,4,1,1,1,2,2,1,3,1,1,1,2,1,1", , %spotify%).accName(0)
    songArtist := Acc_Get("Object", "4,1,4,1,4,1,1,1,2,2,1,3,2,1,1,2,1,1", , %spotify%).accName(0)
    saveToLibraryButton := Acc_Get("Object", "4.1.4.1.4.1.1.1.2.2.1.3.1.1.2", , %spotify%)
    saveToLibraryButtonText := saveToLibraryButton.accName(0)

    ; Click the button.
    If InStr(saveToLibraryButtonText, "Save") {
      saveToLibraryButton.accDoDefaultAction(0)
      TrayTip, %songTitle% by %songArtist%, Saved to Library
    } Else {
      TrayTip, %songTitle% by %songArtist%, Already in Library
    }
  }
}
Is there a reason why my calls for the songTitle and songArtist are returning empty?
Image

The path is via AccViewer and the correct text shows up in the Name box in the Accessibility Info section of the UI.
Image

Maybe another way instead of accName(0)?
Although, the saveToLibraryButton.accName(0) call works perfectly fine. :headwall:
User avatar
jeeswg
Posts: 5288
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Spotify PostMessage Library

15 Nov 2017, 06:19

This might work: replace %spotify% with "ahk_id " spotify. WinExist returns an hWnd.
User avatar
GollyJer
Posts: 56
Joined: 19 Sep 2015, 19:33
GitHub: GollyJer

Re: Spotify PostMessage Library

15 Nov 2017, 10:44

No luck. Same result. :(
Can it have something to do with the first argument Object? I tried text with no luck.

PS. How do you make the inline preformatted text on this forum? [pre] and

Code: Select all

 don't seem to work.
User avatar
jeeswg
Posts: 5288
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Spotify PostMessage Library

15 Nov 2017, 10:49

You use (no space). [c]Like this. Try clicking on the quote button for this post, to see it.

Btw I tend to use 0, not a blank in Acc_Get.
User avatar
GollyJer
Posts: 56
Joined: 19 Sep 2015, 19:33
GitHub: GollyJer

Re: Spotify PostMessage Library

15 Nov 2017, 11:34

Cool. Thanks for the [c] tip.

Yeah, I was trying to figure things out by looking at the source and realized ChildID has a default.
[b]Acc_Get[/b](Cmd, ChildPath="", [b]ChildID=0[/b], WinTitle="", WinText="", ExcludeTitle="", ExcludeText="")

As for getting the artist/album names... the only other thing I can think of to try are different Cmd parameters. But I don't know what to try besides Location, which didn't work.
For anyone playing along... here's the Cmd section of the ACC source.

Code: Select all

StringReplace, Cmd, Cmd, %A_Space%, , All
		properties.HasKey(Cmd)? Cmd:=properties[Cmd]:
		try {
			if (Cmd = "Location")
				AccObj.accLocation(ComObj(0x4003,&x:=0), ComObj(0x4003,&y:=0), ComObj(0x4003,&w:=0), ComObj(0x4003,&h:=0), ChildId)
			  , ret_val := "x" NumGet(x,0,"int") " y" NumGet(y,0,"int") " w" NumGet(w,0,"int") " h" NumGet(h,0,"int")
			else if (Cmd = "Object")
				ret_val := AccObj
			else if Cmd in Role,State
				ret_val := Acc_%Cmd%(AccObj, ChildID+0)
			else if Cmd in ChildCount,Selection,Focus
				ret_val := AccObj["acc" Cmd]
			else
				ret_val := AccObj["acc" Cmd](ChildID+0)
		} catch {
			ErrorLevel := """" Cmd """ Cmd Not Implemented"
			if Acc_Error()
				throw Exception("Cmd Not Implemented", -1, Cmd)
			return
}
It's so weird the button Name is returned but the text fields are not.
At this point I'm just shooting in the dark. :crazy:
User avatar
jeeswg
Posts: 5288
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Spotify PostMessage Library

15 Nov 2017, 11:43

I would have thought that something like this would work:

Code: Select all

q::
hWnd := WinExist("ahk_class SpotifyMainWindow")
songTitle := Acc_Get("Object", "4,1,4,1,4,1,1,1,2,2,1,3,1,1,1,2,1,1", 0, "ahk_id " hWnd).accName(0)
songArtist := Acc_Get("Object", "4,1,4,1,4,1,1,1,2,2,1,3,2,1,1,2,1,1", 0, "ahk_id " hWnd).accName(0)
MsgBox, % songTitle "`r`n" songArtist
return
Otherwise try retrieving accName/accValue for items further up the hierarchy, and see if you get the expected results for those.
User avatar
GollyJer
Posts: 56
Joined: 19 Sep 2015, 19:33
GitHub: GollyJer

Re: Spotify PostMessage Library

15 Nov 2017, 12:44

Thanks again jeeswg.

I tested every item in the hierarchy and none of them return accName/accValue. I guess Accessibility isn't that reliable.
I would love to get this to work perfectly but whatever. I won't look a gift horse in the mouth and be happy that toggling Save to Your Library does work. :D

Here is my generic solution.

Code: Select all

AddSongToMyLibrary() {
  spotifyId := WinExist("ahk_exe spotify.exe")
  If (spotifyId) {
    saveToLibraryButton := Acc_Get("Object", "4.1.4.1.4.1.1.1.2.2.1.3.1.1.2", 0, %spotifyId%)
    saveToLibraryButtonText := saveToLibraryButton.accName(0)

    If InStr(saveToLibraryButtonText, "Save") {
      saveToLibraryButton.accDoDefaultAction(0)
      TrayTip, Spotify, Song Saved to Library
    } Else {
      TrayTip, Spotify, Song Already in Library
    }

    ; Hide TrayTip after 2.5 seconds.
    Sleep, 2500
    TrayTip

  }
}
Last challenge... get it to work when Spotify is minimized to the system tray.
User avatar
GollyJer
Posts: 56
Joined: 19 Sep 2015, 19:33
GitHub: GollyJer

Re: Spotify PostMessage Library

27 Apr 2018, 17:14

Hi jeeswig. With Spotify's recent update they've somehow managed to hide the top level window from Acc_Viewer.
Is there a way to get the view Show Acc Structure button to show all parents in addition to children?

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Google [Bot], iseahound and 46 guests