AutoHotKey script for controlling multiple music applications easily

With the following AutoHotkey script you can control multiple media players easily. In windows, by default all the keyboard shortcuts (special media buttons play, previous, next and stop) will go to all applications. Obviously, this will cause problems if I have both Spotify and foobar2000 both running at the same time.

The script solves the situation by examining window titles and the active window. The script uses following logic when deciding the window that will receive the keyboard button press:

  1. Window that is active (determined by examined window title, for example)
  2. Media player that is currently playing a song (determined by examining window title, for example)
  3. The default player

The script should be easy enough to expand to more media players; the only hard thing is to figure how to determine if the player is actually playing something. In foobar2000, the window title is customizable to make the detection easy (DUI: Go to Preferences->Display->Default User Interface, CUI: Preferences->Display->Columns UI->->Main tab). You can append the following format string so that it will display [Playing] if a song is playing

$if($and(%isplaying%,$not(%ispaused%)),'[Playing]',)

UPDATE: The following script will use last activated window as the default player (see step 3).

#InstallKeybdHook

SetTitleMatchMode  2
commandSpotify := 0
commandFoobar := 1

detectMusicPlayers()
{
  ; all variables are global unless prefixed with local
  global
  DetectHiddenWindows, On
  WinGetTitle, spotifyTitle, ahk_class SpotifyMainWindow
  WinGetTitle, foobarTitle, foobar2000
  WinGetTitle, currentTitle, A
  DetectHiddenWindows, Off
  spotifyTitleLength := StrLen(spotifyTitle)
  spotifyActive := InStr(currentTitle, "Spotify")
  foobarActive := InStr(currentTitle, "foobar2000")
  spotifyPlaying := spotifyTitleLength > 7
  foobarPlaying := InStr(foobar2000, "[Playing]")

  if spotifyActive or spotifyPlaying{
    commandSpotify := 1
    commandFoobar := 0 
    ;MsgBox spotify active
  } else if foobarActive or foobarPlaying {
    commandSpotify := 0
    commandFoobar := 1  
    ;MsgBox foobar active
  } else {
    ;MsgBox spotify not playing
    ;Use "defaults" as specified by CheckActivations
  }
  
}

; Check for window activation
SetTimer, CheckActivations, 300


CheckActivations:
  if WinActive("Spotify") {
    ;MsgBox Spotify activated
    commandSpotify = 1
    commandFoobar = 0
  } else if WinActive("foobar2000") {
    ;MsgBox foobar activated
    commandSpotify = 0
    commandFoobar = 1
  }
  
return


; 122 = play/pause handling
SC122::
detectMusicPlayers()

if commandSpotify {
  ControlSend, , {Space}, ahk_class SpotifyMainWindow 
} else if commandFoobar {
  Run "C:\program files (x86)\foobar2000\foobar2000.exe" /playpause 
}
return

; 110	 = PREV  handling
SC110	::
detectMusicPlayers()

if commandSpotify {
  ControlSend, , ^{Left}, ahk_class SpotifyMainWindow 
} else if commandFoobar {
  Run "C:\program files (x86)\foobar2000\foobar2000.exe" /prev 
}
return

; 119	 = NEXT handling
SC119	::
detectMusicPlayers()

if commandSpotify {
  ControlSend, , ^{Right}, ahk_class SpotifyMainWindow 
} else if commandFoobar {
  Run "C:\program files (x86)\foobar2000\foobar2000.exe" /next 
}
return

; 124	 = STOP handling
SC124	::
detectMusicPlayers()

if commandSpotify {
  ; there is no stop in spotify, use "play/pause" instead
  if spotifyPlaying {
    ControlSend, , {Space}, ahk_class SpotifyMainWindow 
  }
} else if commandFoobar {
  Run "C:\program files (x86)\foobar2000\foobar2000.exe" /stop 
}
return

Vastaa

Täytä tietosi alle tai klikkaa kuvaketta kirjautuaksesi sisään:

WordPress.com-logo

Olet kommentoimassa WordPress.com -tilin nimissä. Log Out / Muuta )

Twitter-kuva

Olet kommentoimassa Twitter -tilin nimissä. Log Out / Muuta )

Facebook-kuva

Olet kommentoimassa Facebook -tilin nimissä. Log Out / Muuta )

Google+ photo

Olet kommentoimassa Google+ -tilin nimissä. Log Out / Muuta )

Muodostetaan yhteyttä palveluun %s