Advertisement
AntVenom

AutoHotKey "Now Playing" for Spotify Windows 10 v1.05

Nov 22nd, 2018
8,406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; ########## Automatically writes "Now Playing" file when Spotify is running ##########
  2. Loop
  3. {
  4.     ; Change # to adjust how often the loop runs. 1000 = 1 Second
  5.     Sleep 2000
  6.  
  7.     ; Output file directory for "Now Playing"
  8.     outputFile := "D:\OBS\assets\nowplaying.txt"
  9.  
  10.     ; Main Program - Only runs if Spotify.exe exists (even if only in taskbar)
  11.     Process, Exist, Spotify.exe
  12.     if !ErrorLevel = 0
  13.     {
  14.         winId := Get_Spotify_Id()
  15.         WinGetTitle, outputText, ahk_id %winId%
  16.  
  17.         ; If file text changes, overwrite w/ new text, unless text = "Spotify"
  18.         ; When Spotify is paused, it's title changes to "Spotify". It's dumb.
  19.         FileRead, fileText, %outputFile%
  20.         if (fileText != outputText) && (outputText != "Spotify")
  21.         {
  22.             FileDelete, %outputFile%
  23.             FileAppend, %outputText%, %outputFile%
  24.         }
  25.  
  26.         ; Get the ID of the Spotify window (using cache)
  27.         Get_Spotify_Id()
  28.         {
  29.             if (Is_Spotify(cached_spotify_window))
  30.                 return cached_spotify_window
  31.  
  32.             WinGet, windows, List, ahk_exe Spotify.exe
  33.             Loop, %windows%
  34.             {
  35.                 winId := windows%A_Index%
  36.                 if (Is_Spotify(winId))
  37.                 {
  38.                     cached_spotify_window = %winId%
  39.                     return winId
  40.                 }
  41.             }
  42.         }
  43.  
  44.         ; # Check if the given ID is a Spotify window #
  45.         Is_Spotify(winId)
  46.         {
  47.             WinGetClass, class, ahk_id %winId%
  48.             if (class == "Chrome_WidgetWin_0")
  49.             {
  50.                 WinGetTitle, title, ahk_id %winId%
  51.                 return (title != "")
  52.             }
  53.             return false
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement