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] [Expand] [Download] GeSHi © Codebox Plus

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: 4994
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: 4994
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] [Expand] [Download] GeSHi © Codebox Plus



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

jeeswg wrote:You could check the return value,

no luck there unfortunately, it just returns 0/no whether shuffle is on and off

jeeswg wrote: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] [Expand] [Download] GeSHi © Codebox Plus

User avatar
jeeswg
Posts: 4994
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] [Expand] [Download] GeSHi © Codebox Plus


The menu resource from Notepad:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

User avatar
jeeswg
Posts: 4994
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Spotify PostMessage Library

10 Nov 2017, 17:02

Some Acc examples:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

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

GollyJer wrote: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] [Expand] [Download] GeSHi © Codebox Plus

User avatar
jeeswg
Posts: 4994
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

jeeswg wrote: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] [Expand] [Download] GeSHi © Codebox Plus

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] [Expand] [Download] (Untitled.ahk)GeSHi © Codebox Plus



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: 4994
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] don't seem to work.
User avatar
jeeswg
Posts: 4994
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Spotify PostMessage Library

15 Nov 2017, 10:49

You use [c ] (no space). 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.
Acc_Get(Cmd, ChildPath="", ChildID=0, 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] [Expand] [Download] (Untitled.ahk)GeSHi © Codebox Plus


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: 4994
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] [Download] GeSHi © Codebox Plus

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] [Expand] [Download] (Untitled.ahk)GeSHi © Codebox Plus



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: ArchieEU and 11 guests