make this code get spotify cover Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jay lee
Posts: 83
Joined: 14 Oct 2021, 11:17

make this code get spotify cover

21 Feb 2023, 15:05

This code gets the title of the current song but can somebody change it so it gets the artist and the cover?

Code: Select all

app := "spotify.exe"
CreateClass("Windows.Media.Control.GlobalSystemMediaTransportControlsSessionManager", IGlobalSystemMediaTransportControlsSessionManagerStatics := "{2050c4ee-11a0-57de-aed7-c97c70338245}", GlobalSystemMediaTransportControlsSessionManagerStatics)
DllCall(NumGet(NumGet(GlobalSystemMediaTransportControlsSessionManagerStatics+0)+6*A_PtrSize), "ptr", GlobalSystemMediaTransportControlsSessionManagerStatics, "ptr*", GlobalSystemMediaTransportControlsSessionManager)   ; GlobalSystemMediaTransportControlsSessionManager.RequestAsync
WaitForAsync(GlobalSystemMediaTransportControlsSessionManager)
return

f11::
title := ""
DllCall(NumGet(NumGet(GlobalSystemMediaTransportControlsSessionManager+0)+7*A_PtrSize), "ptr", GlobalSystemMediaTransportControlsSessionManager, "ptr*", GlobalSystemMediaTransportControlsSessionList)   ; GlobalSystemMediaTransportControlsSessionManager.GetSessions
DllCall(NumGet(NumGet(GlobalSystemMediaTransportControlsSessionList+0)+7*A_PtrSize), "ptr", GlobalSystemMediaTransportControlsSessionList, "int*", count)   ; count
loop % count
{
   DllCall(NumGet(NumGet(GlobalSystemMediaTransportControlsSessionList+0)+6*A_PtrSize), "ptr", GlobalSystemMediaTransportControlsSessionList, "int", A_Index-1, "ptr*", GlobalSystemMediaTransportControlsSession)
   DllCall(NumGet(NumGet(GlobalSystemMediaTransportControlsSession+0)+6*A_PtrSize), "ptr", GlobalSystemMediaTransportControlsSession, "ptr*", SourceAppUserModelId)   ; GlobalSystemMediaTransportControlsSession.get_SourceAppUserModelId
   buffer := DllCall("Combase.dll\WindowsGetStringRawBuffer", "ptr", SourceAppUserModelId, "uint*", length, "ptr")
   if (StrGet(buffer, "UTF-16") = app)
   {
      DllCall(NumGet(NumGet(GlobalSystemMediaTransportControlsSession+0)+7*A_PtrSize), "ptr", GlobalSystemMediaTransportControlsSession, "ptr*", GlobalSystemMediaTransportControlsSessionMediaProperties)   ; GlobalSystemMediaTransportControlsSession.TryGetMediaPropertiesAsync
      WaitForAsync(GlobalSystemMediaTransportControlsSessionMediaProperties)
      DllCall(NumGet(NumGet(GlobalSystemMediaTransportControlsSessionMediaProperties+0)+6*A_PtrSize), "ptr", GlobalSystemMediaTransportControlsSessionMediaProperties, "ptr*", hTitle)   ; GlobalSystemMediaTransportControlsSessionMediaProperties.get_Title
      buffer := DllCall("Combase.dll\WindowsGetStringRawBuffer", "ptr", hTitle, "uint*", length, "ptr")
      title := StrGet(buffer, "UTF-16")
      ObjReleaseClose(GlobalSystemMediaTransportControlsSessionMediaProperties)
      ObjReleaseClose(GlobalSystemMediaTransportControlsSession)
      break
   }
   ObjReleaseClose(GlobalSystemMediaTransportControlsSession)
}
ObjReleaseClose(GlobalSystemMediaTransportControlsSessionList)
msgbox % title 
return

CreateClass(string, interface := "", ByRef Class := "")
{
   CreateHString(string, hString)
   if (interface = "")
      result := DllCall("Combase.dll\RoActivateInstance", "ptr", hString, "ptr*", Class, "uint")
   else
   {
      VarSetCapacity(GUID, 16)
      DllCall("ole32\CLSIDFromString", "wstr", interface, "ptr", &GUID)
      result := DllCall("Combase.dll\RoGetActivationFactory", "ptr", hString, "ptr", &GUID, "ptr*", Class, "uint")
   }
   if (result != 0)
   {
      if (result = 0x80004002)
         msgbox No such interface supported
      else if (result = 0x80040154)
         msgbox Class not registered
      else
         msgbox error: %result%
      ExitApp
   }
   DeleteHString(hString)
}

CreateHString(string, ByRef hString)
{
    DllCall("Combase.dll\WindowsCreateString", "wstr", string, "uint", StrLen(string), "ptr*", hString)
}

DeleteHString(hString)
{
   DllCall("Combase.dll\WindowsDeleteString", "ptr", hString)
}

WaitForAsync(ByRef Object)
{
   AsyncInfo := ComObjQuery(Object, IAsyncInfo := "{00000036-0000-0000-C000-000000000046}")
   loop
   {
      DllCall(NumGet(NumGet(AsyncInfo+0)+7*A_PtrSize), "ptr", AsyncInfo, "uint*", status)   ; IAsyncInfo.Status
      if (status != 0)
      {
         if (status != 1)
         {
            DllCall(NumGet(NumGet(AsyncInfo+0)+8*A_PtrSize), "ptr", AsyncInfo, "uint*", ErrorCode)   ; IAsyncInfo.ErrorCode
            msgbox AsyncInfo status error: %ErrorCode%
            ExitApp
         }
         ObjRelease(AsyncInfo)
         break
      }
      sleep 10
   }
   DllCall(NumGet(NumGet(Object+0)+8*A_PtrSize), "ptr", Object, "ptr*", ObjectResult)   ; GetResults
   ObjReleaseClose(Object)
   Object := ObjectResult
}

ObjReleaseClose(ByRef Object)
{
   if Object
   {
      if (Close := ComObjQuery(Object, IClosable := "{30D5A829-7FA4-4026-83BB-D75BAE4EA99E}"))
      {
         DllCall(NumGet(NumGet(Close+0)+6*A_PtrSize), "ptr", Close)   ; Close
         ObjRelease(Close)
      }
      ObjRelease(Object)
   }
   Object := ""
}
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: make this code get spotify cover  Topic is solved

13 Mar 2023, 18:12

Something like this:

Code: Select all

app := "spotify.exe"
CreateClass("Windows.Media.Control.GlobalSystemMediaTransportControlsSessionManager", IGlobalSystemMediaTransportControlsSessionManagerStatics := "{2050c4ee-11a0-57de-aed7-c97c70338245}", GlobalSystemMediaTransportControlsSessionManagerStatics)
DllCall(NumGet(NumGet(GlobalSystemMediaTransportControlsSessionManagerStatics+0)+6*A_PtrSize), "ptr", GlobalSystemMediaTransportControlsSessionManagerStatics, "ptr*", GlobalSystemMediaTransportControlsSessionManager)   ; GlobalSystemMediaTransportControlsSessionManager.RequestAsync
WaitForAsync(GlobalSystemMediaTransportControlsSessionManager)
DllCall("LoadLibrary", "str", "gdiplus")
VarSetCapacity(si, A_PtrSize = 8 ? 24 : 16, 0) ; sizeof(GdiplusStartupInput) = 16, 24
NumPut(0x1, si, "uint")
DllCall("gdiplus\GdiplusStartup", "ptr*", pToken:=0, "ptr", &si, "ptr", 0)
return

f11::
title := artist := ""
DllCall(NumGet(NumGet(GlobalSystemMediaTransportControlsSessionManager+0)+7*A_PtrSize), "ptr", GlobalSystemMediaTransportControlsSessionManager, "ptr*", GlobalSystemMediaTransportControlsSessionList)   ; GlobalSystemMediaTransportControlsSessionManager.GetSessions
DllCall(NumGet(NumGet(GlobalSystemMediaTransportControlsSessionList+0)+7*A_PtrSize), "ptr", GlobalSystemMediaTransportControlsSessionList, "int*", count)   ; count
loop % count
{
   DllCall(NumGet(NumGet(GlobalSystemMediaTransportControlsSessionList+0)+6*A_PtrSize), "ptr", GlobalSystemMediaTransportControlsSessionList, "int", A_Index-1, "ptr*", GlobalSystemMediaTransportControlsSession)
   DllCall(NumGet(NumGet(GlobalSystemMediaTransportControlsSession+0)+6*A_PtrSize), "ptr", GlobalSystemMediaTransportControlsSession, "ptr*", SourceAppUserModelId)   ; GlobalSystemMediaTransportControlsSession.get_SourceAppUserModelId
   buffer := DllCall("Combase.dll\WindowsGetStringRawBuffer", "ptr", SourceAppUserModelId, "uint*", length, "ptr")
   if (StrGet(buffer, "UTF-16") = app)
   {
      DllCall(NumGet(NumGet(GlobalSystemMediaTransportControlsSession+0)+7*A_PtrSize), "ptr", GlobalSystemMediaTransportControlsSession, "ptr*", GlobalSystemMediaTransportControlsSessionMediaProperties)   ; GlobalSystemMediaTransportControlsSession.TryGetMediaPropertiesAsync
      WaitForAsync(GlobalSystemMediaTransportControlsSessionMediaProperties)
      DllCall(NumGet(NumGet(GlobalSystemMediaTransportControlsSessionMediaProperties+0)+6*A_PtrSize), "ptr", GlobalSystemMediaTransportControlsSessionMediaProperties, "ptr*", hTitle)   ; GlobalSystemMediaTransportControlsSessionMediaProperties.get_Title
      buffer := DllCall("Combase.dll\WindowsGetStringRawBuffer", "ptr", hTitle, "uint*", length, "ptr")
      title := StrGet(buffer, "UTF-16")
      DllCall(NumGet(NumGet(GlobalSystemMediaTransportControlsSessionMediaProperties+0)+9*A_PtrSize), "ptr", GlobalSystemMediaTransportControlsSessionMediaProperties, "ptr*", hTitle)   ; GlobalSystemMediaTransportControlsSessionMediaProperties.get_Artist
      buffer := DllCall("Combase.dll\WindowsGetStringRawBuffer", "ptr", hTitle, "uint*", length, "ptr")
      artist := StrGet(buffer, "UTF-16")
      DllCall(NumGet(NumGet(GlobalSystemMediaTransportControlsSessionMediaProperties+0)+15*A_PtrSize), "ptr", GlobalSystemMediaTransportControlsSessionMediaProperties, "ptr*", RandomAccessStreamReference)   ; GlobalSystemMediaTransportControlsSessionMediaProperties.get_Thumbnail
      if (DllCall(NumGet(NumGet(RandomAccessStreamReference+0)+6*A_PtrSize), "ptr", RandomAccessStreamReference, "ptr*", RandomAccessStreamWithContentType) = 0)   ; IRandomAccessStreamReference.OpenReadAsync
      {
         WaitForAsync(RandomAccessStreamWithContentType)
         VarSetCapacity(CLSID, 16, 0)
         DllCall("ole32\CLSIDFromString", "wstr", IID_IStream := "{0000000C-0000-0000-C000-000000000046}", "ptr", &CLSID)
         DllCall("ShCore\CreateStreamOverRandomAccessStream", "ptr", RandomAccessStreamWithContentType, "ptr", &CLSID, "ptr*", Stream, "uint")
         DllCall("gdiplus\GdipCreateBitmapFromStream", "ptr", Stream, "ptr*", pBitmap)
         DllCall("gdiplus\GdipCreateHBITMAPFromBitmap", "ptr", pBitmap, "ptr*", hBitmap, "uint", 0xffffffff)
         DllCall("Gdiplus.dll\GdipDisposeImage", "ptr", pBitmap)
         ObjReleaseClose(Stream)
         ObjReleaseClose(RandomAccessStreamWithContentType)
      }
      ObjReleaseClose(RandomAccessStreamReference)
      ObjReleaseClose(GlobalSystemMediaTransportControlsSessionMediaProperties)
      ObjReleaseClose(GlobalSystemMediaTransportControlsSession)
      break
   }
   ObjReleaseClose(GlobalSystemMediaTransportControlsSession)
}
ObjReleaseClose(GlobalSystemMediaTransportControlsSessionList)
msgbox % "title: " title "`nartist: " artist "`nhbitmap: " hbitmap
if hbitmap
{
   DllCall("DeleteObject", "ptr", hBitmap)
   hBitmap := ""
}
return

CreateClass(string, interface := "", ByRef Class := "")
{
   CreateHString(string, hString)
   if (interface = "")
      result := DllCall("Combase.dll\RoActivateInstance", "ptr", hString, "ptr*", Class, "uint")
   else
   {
      VarSetCapacity(GUID, 16)
      DllCall("ole32\CLSIDFromString", "wstr", interface, "ptr", &GUID)
      result := DllCall("Combase.dll\RoGetActivationFactory", "ptr", hString, "ptr", &GUID, "ptr*", Class, "uint")
   }
   if (result != 0)
   {
      if (result = 0x80004002)
         msgbox No such interface supported
      else if (result = 0x80040154)
         msgbox Class not registered
      else
         msgbox error: %result%
      ExitApp
   }
   DeleteHString(hString)
}

CreateHString(string, ByRef hString)
{
    DllCall("Combase.dll\WindowsCreateString", "wstr", string, "uint", StrLen(string), "ptr*", hString)
}

DeleteHString(hString)
{
   DllCall("Combase.dll\WindowsDeleteString", "ptr", hString)
}

WaitForAsync(ByRef Object)
{
   AsyncInfo := ComObjQuery(Object, IAsyncInfo := "{00000036-0000-0000-C000-000000000046}")
   loop
   {
      DllCall(NumGet(NumGet(AsyncInfo+0)+7*A_PtrSize), "ptr", AsyncInfo, "uint*", status)   ; IAsyncInfo.Status
      if (status != 0)
      {
         if (status != 1)
         {
            DllCall(NumGet(NumGet(AsyncInfo+0)+8*A_PtrSize), "ptr", AsyncInfo, "uint*", ErrorCode)   ; IAsyncInfo.ErrorCode
            msgbox AsyncInfo status error: %ErrorCode%
            ExitApp
         }
         ObjRelease(AsyncInfo)
         break
      }
      sleep 10
   }
   DllCall(NumGet(NumGet(Object+0)+8*A_PtrSize), "ptr", Object, "ptr*", ObjectResult)   ; GetResults
   ObjReleaseClose(Object)
   Object := ObjectResult
}

ObjReleaseClose(ByRef Object)
{
   if Object
   {
      if (Close := ComObjQuery(Object, IClosable := "{30D5A829-7FA4-4026-83BB-D75BAE4EA99E}"))
      {
         DllCall(NumGet(NumGet(Close+0)+6*A_PtrSize), "ptr", Close)   ; Close
         ObjRelease(Close)
      }
      ObjRelease(Object)
   }
   Object := ""
}
jay lee
Posts: 83
Joined: 14 Oct 2021, 11:17

Re: make this code get spotify cover

14 Mar 2023, 08:53

Ty so much, I added a bit code to safe the cover to file but this works perfectly!
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: make this code get spotify cover

14 Mar 2023, 09:27

For saving to file You dont need this part:

Code: Select all

         DllCall("gdiplus\GdipCreateHBITMAPFromBitmap", "ptr", pBitmap, "ptr*", hBitmap, "uint", 0xffffffff)
         DllCall("Gdiplus.dll\GdipDisposeImage", "ptr", pBitmap)
And at the end change to this:

Code: Select all

msgbox % "title: " title "`nartist: " artist "`npBitmap: " pBitmap
if pBitmap
{
   DllCall("Gdiplus.dll\GdipDisposeImage", "ptr", pBitmap)
   pBitmap := ""
}
jay lee
Posts: 83
Joined: 14 Oct 2021, 11:17

Re: make this code get spotify cover

14 Mar 2023, 16:08

And could you make a script that sees if spotify is currently paused or not? And maybe a better way then Controlsend to pause and play?
jay lee
Posts: 83
Joined: 14 Oct 2021, 11:17

Re: make this code get spotify cover

14 Mar 2023, 16:29

Thank you for these links. If I found them earlier I wouldve probably been able to solve the whole thing alone :D
jay lee
Posts: 83
Joined: 14 Oct 2021, 11:17

Re: make this code get spotify cover

14 Mar 2023, 17:06

For anyone who wants the code, you can just add these 3 lines to your script:
pause:

Code: Select all

 DllCall(NumGet(NumGet(GlobalSystemMediaTransportControlsSession+0)+20*A_PtrSize), "ptr", GlobalSystemMediaTransportControlsSession, "ptr*", output)
buffer := DllCall("Combase.dll\WindowsGetStringRawBuffer", "ptr", output, "uint*", length, "ptr")
next:

Code: Select all

 DllCall(NumGet(NumGet(GlobalSystemMediaTransportControlsSession+0)+16*A_PtrSize), "ptr", GlobalSystemMediaTransportControlsSession, "ptr*", output)
buffer := DllCall("Combase.dll\WindowsGetStringRawBuffer", "ptr", output, "uint*", length, "ptr")
prev:

Code: Select all

 DllCall(NumGet(NumGet(GlobalSystemMediaTransportControlsSession+0)+17*A_PtrSize), "ptr", GlobalSystemMediaTransportControlsSession, "ptr*", output)
buffer := DllCall("Combase.dll\WindowsGetStringRawBuffer", "ptr", output, "uint*", length, "ptr")
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: make this code get spotify cover

14 Mar 2023, 17:22

Right call this methods like this:

Code: Select all

DllCall(NumGet(NumGet(GlobalSystemMediaTransportControlsSession+0)+20*A_PtrSize), "ptr", GlobalSystemMediaTransportControlsSession, "ptr*", bool)
WaitForAsync(bool)
msgbox % bool
jay lee
Posts: 83
Joined: 14 Oct 2021, 11:17

Re: make this code get spotify cover

14 Mar 2023, 17:25

Thank you so much for your help! Do you know how to see if spotify is currently paused or not?
jay lee
Posts: 83
Joined: 14 Oct 2021, 11:17

Re: make this code get spotify cover

14 Mar 2023, 18:06

can you show me a example function that returns the playbackstatus as a number? I dont really understand this right now sorry :D
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: make this code get spotify cover

14 Mar 2023, 18:17

Code: Select all

DllCall(NumGet(NumGet(GlobalSystemMediaTransportControlsSession+0)+9*A_PtrSize), "ptr", GlobalSystemMediaTransportControlsSession, "ptr*", GlobalSystemMediaTransportControlsSessionPlaybackInfo)
DllCall(NumGet(NumGet(GlobalSystemMediaTransportControlsSessionPlaybackInfo+0)+7*A_PtrSize), "ptr", GlobalSystemMediaTransportControlsSessionPlaybackInfo, "int*", PlaybackStatus)
ObjReleaseClose(GlobalSystemMediaTransportControlsSessionPlaybackInfo)
msgbox % PlaybackStatus
jay lee
Posts: 83
Joined: 14 Oct 2021, 11:17

Re: make this code get spotify cover

14 Mar 2023, 18:19

Thank you so much! :D

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 96 guests