简体   繁体   中英

Spotify: Does anyone know any command prompt commands for the Windows 10 Spotify desktop app?

I'm writing a python script to control Spotify through command prompt, but the only command I know is:

spotify --uri=[xxxx:xxxxxxxxxxxxxxxxxxxx]

Which opens Spotify to a track, album, or artist. I've tried lots of other random commands trying to find some that work, but with no luck.

I don't know whether you can do it through cli but you could simulate keypresses and find the process and window title.

import win32api, win32con, win32gui, win32process, psutil, time

class Helper():
  play_pause = 0xB3
  next_track = 0xB0
  previous_track = 0xB1
  
  def __init__(self):
      self.hwnd = 0

  def winEnumHandler(self, hwnd, ctx ):
      process_name = psutil.Process(win32process.GetWindowThreadProcessId(hwnd)[-1]).name()
      window_name = win32gui.GetWindowText(hwnd)
      if win32gui.IsWindowVisible(hwnd) and process_name == "Spotify.exe" and window_name != "":
          self.hwnd = hwnd

  def sendKey(self, key):
      win32api.keybd_event(key, 0, 0, 0)
      win32api.keybd_event(key, 0, win32con.KEYEVENTF_KEYUP, 0)

  def getInfo(self):
      win32gui.EnumWindows(self.winEnumHandler, None)
      return win32gui.GetWindowText(self.hwnd)

hp = Helper()
print(hp.getInfo())

hp.sendKey(Helper.next_track)
time.sleep(1)
print(hp.getInfo())

hp.sendKey(Helper.play_pause)
time.sleep(1)
print(hp.getInfo())

在此处输入图像描述

You can determine the playing state by the title (contains - or Spotify ) and you should check whether the process is still alive / hwnd.= 0.

I found the keycodes here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM