Saturday, October 27, 2012

Launching a program only if a specific USB-device is connected

With the growing park of MIDI controllers I often forget to turn them on before starting Ableton Live. And I have some additional software like LPC Live by nativeKontrol to interact with the Launchpad for instance.

So I was looking for a way to check if a certain USB-Device is connected and then (and only then) start a corresponding program or reminding me to turn on my keyboard followed by the start of Ableton Live.

Searching the web didn't show up a definite solution. So with a little research and putting the pieces together I came up with this.


You'll need Autohotkey and USBdeview. Put USBdeviw in the same directory as the script (or adapt the script). Before you change the devicenames in the script run USBdeview to get the names of the devices.


So here's the commented script (save as .ahk)

RunWait, usbdeview.exe /stext usbdeviewlog.txt /DisplayDisconnected 0 /sort "~connected",,hide ;Writes connected USB-devices to a file (The 'sort' command is probably obsolete)

FileRead, USBDevices, usbdeviewlog.txt ;Writes the file to a variable

IfInString, USBDevices, Launchpad ;Checks the variable if a certain device is connected
{
   IfWinNotExist, ahk_exe MTPlayer.exe ;Checks if the program is already running
   {
      Run, MTPlayer.exe, C:\Program Files (x86)\MT Player [nativeKONTROL]\, Hide ;Runs the program associated to the device
   }
}

IfNotInString, USBDevices, microkontrol ;Checks if another device is connected
MsgBox, 0, Check for Keyboard, microKontrol not connected!`rAbleton Live starts in 10 seconds..., 10 ;Displays a Messagebox lasting 10 seconds if the device is not connected
 
IfWinNotExist, ahk_exe Ableton Live 8.exe ; Checks if the program is already running
   {
      Run, Ableton Live 8.exe, C:\ProgramData\Ableton\Live 8\Program\ ; Runs the program
   }



No comments:

Post a Comment