It is currently June 26th, 2019, 4:49 pm

Run a program only if a USB-device is connected

Post reviews, recommendations and questions about other software.
Posts: 6
Joined: August 13th, 2012, 11:19 am

Run a program only if a USB-device is connected

mothergarage » October 25th, 2012, 7:17 am

So I this isn't really Rainmeter related, but as I was customizing my desktop I was running into this problem. 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)

Code: Select all

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