Run scripts/programs when a specific device is connected/disconnected to your PC.
How to use:
-Run "DeviceIDFinder.ahk" to identify your device.
-Add your devices IDs and devices names at the top of the script. (DeviceIDPnP.ahk) The device's name doesn't have to exactly match the name found with "DeviceIDFinder.ahk". You can name it whatever you want.
-Add the devices names and the scripts/programs that you want to run/close when the devices are connecting/disconnecting.
DeviceIDFinder
Find your device's unique IDs.
How to use:
-Run the program and follow the instructions.
Get the latest version on GitHub.
https://github.com/XMCQCX/DeviceIDPnP
Code: Select all
/*
Script: DeviceIDFinder.ahk
Author: XMCQCX
Date: 2022-09-24
Version: 1.0.0
*/
#NoEnv
#SingleInstance, Force
SendMode Input
SetWorkingDir, %A_ScriptDir%
MsgBox, 64, Find deviceID, Plug your device and press OK
;=============================================================================================
; List all devices connected
For Device in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_PnPEntity")
ListConnectedDeviceIDs .= device.name ":" A_Tab Device.DeviceID "`n"
;=============================================================================================
; Remove duplicates from ListConnectedDeviceIDs
Loop, Parse, ListConnectedDeviceIDs, "`n"
{
ListConnectedDeviceIDs := (A_Index=1 ? A_LoopField : ListConnectedDeviceIDs . (InStr("`n" ListConnectedDeviceIDs
. "`n", "`n" A_LoopField "`n") ? "" : "`n" A_LoopField ) )
}
;=============================================================================================
; Add all devices connected in oConnectedDeviceIDs
oConnectedDeviceIDs := {}
Loop, Parse, ListConnectedDeviceIDs, "`n"
oConnectedDeviceIDs.Push({"DeviceID":A_Loopfield})
;=============================================================================================
MsgBox, 64, Find deviceID, Unplug your device and press OK
;=============================================================================================
; List all devices connected
For Device in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_PnPEntity")
strListConnectedDeviceIDs .= device.name ":" A_Tab Device.DeviceID "`n"
;=============================================================================================
; Remove duplicates from strListConnectedDeviceIDs
Loop, Parse, strListConnectedDeviceIDs, "`n"
{
strListConnectedDeviceIDs := (A_Index=1 ? A_LoopField : strListConnectedDeviceIDs . (InStr("`n" strListConnectedDeviceIDs
. "`n", "`n" A_LoopField "`n") ? "" : "`n" A_LoopField ) )
}
;=============================================================================================
; Find the device that was connected/disconnected
Loop, Parse, strListConnectedDeviceIDs, "`n"
{
For Index, Element in oConnectedDeviceIDs
{
If InStr(strListConnectedDeviceIDs, Element.DeviceID)
oConnectedDeviceIDs.RemoveAt(Index)
}
}
;=============================================================================================
; List the IDs of the device
For Index, Element in oConnectedDeviceIDs
DeviceIDFound .= Element.DeviceID "`n"
;=============================================================================================
; Format the IDs of the device
strDeviceIDFound := ""
For each, line in StrSplit(DeviceIDFound, "`n")
{
RegExMatch(line, "`nm)^(.*?)" A_TAB "(.*)$", OutputVar)
strDeviceIDFound .= OutputVar1 "`n" OutputVar2 "`n`n"
}
strDeviceIDFound := RTrim(strDeviceIDFound, "`n`n")
If !strDeviceIDFound
strDeviceIDFound := "No device found !"
;=============================================================================================
Gui, New
Gui, Add, Text,, DeviceID:
Gui, Add, Edit, vTextDeviceID ReadOnly, %strDeviceIDFound%
Gui, Add, Button, w175 vCopyToClipboard gCopyToClipboard, Copy to Clipboard
Gui, Add, Button, x+10 w175 gFindAnotherDeviceID, Find ID of another device
Gui, Add, Button, x+10 w175 gExit, Exit
GuiControl, Focus, CopyToClipboard
Gui, Show
return
;=============================================================================================
CopyToClipboard:
Clipboard := strDeviceIDFound
MsgBox, 64, Success, Device ID copied to Clipboard !
return
FindAnotherDeviceID:
Reload
Exit:
GuiClose:
Exitapp
Code: Select all
/*
Script: DeviceIDPnP.ahk
Author: XMCQCX
Date: 2022-09-28
Version: 1.2.0
Link: https://www.autohotkey.com/boards/viewtopic.php?f=6&t=108930
Change log:
2022-09-28 -> 1.2.0 - Fixed an issue when connecting/disconnecting multiple devices at the same time or in quick succession.
2022-09-27 -> 1.1.1 - Fixed an issue with some devices status not properly updating when connecting/disconnecting.
2022-09-24 -> 1.1.0 - Added run or close scripts/programs if the devices are connected/disconnected when the script start.
*/
#NoEnv
#SingleInstance, Force
SendMode Input
SetWorkingDir, %A_ScriptDir%
CoordMode, ToolTip
;=============================================================================================
oMyDevices := {}
oMyDevices.Push({"DeviceName":"USB Kingston DataTraveler 3.0", "DeviceID":"USB\VID_0951&PID_1666\E0D55EA573DCF450E97C104C"})
oMyDevices.Push({"DeviceName":"BLUETOOTH PLAYSTATION(R)3 Controller", "DeviceID":"BTHPS3BUS\{53F88889-1AAF-4353-A047-556B69EC6DA6}&DEV&VID_054C&PID_0268&04766E9094F3\9&320AC31D&0&0"})
oMyDevices.Push({"DeviceName":"HDMI Samsung TV", "DeviceID":"SWD\MMDEVAPI\{0.0.0.00000000}.{ED3C7A62-B05B-44C6-ACD8-BCAA1E894265}"})
;=============================================================================================
DevicesActions(ThisDeviceStatusHasChanged) {
If (ThisDeviceStatusHasChanged = "USB Kingston DataTraveler 3.0 Connected")
If !WinExist("ahk_exe Notepad.exe")
Run, Notepad.exe
If (ThisDeviceStatusHasChanged = "USB Kingston DataTraveler 3.0 Disconnected")
If WinExist("ahk_exe Notepad.exe")
Winclose, % "ahk_exe Notepad.exe"
;=============================================================================================
If (ThisDeviceStatusHasChanged = "BLUETOOTH PLAYSTATION(R)3 Controller Connected")
If !WinExist("ahk_exe wordpad.exe")
Run, wordpad.exe
If (ThisDeviceStatusHasChanged = "BLUETOOTH PLAYSTATION(R)3 Controller Disconnected")
If WinExist("ahk_exe wordpad.exe")
Winclose, % "ahk_exe wordpad.exe"
;=============================================================================================
If (ThisDeviceStatusHasChanged = "HDMI Samsung TV Connected")
If !WinExist("ahk_exe mspaint.exe")
Run, mspaint.exe
If (ThisDeviceStatusHasChanged = "HDMI Samsung TV Disconnected")
If WinExist("ahk_exe mspaint.exe")
Winclose, % "ahk_exe mspaint.exe"
;=============================================================================================
}
; Check devices connected
oDevicesConnected := {}
For Device in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_PnPEntity")
oDevicesConnected.Push({"DeviceName":Device.Name, "DeviceID":Device.DeviceID, "DevicePNPClass":Device.PNPClass, "DeviceStatus":Device.Status})
;=============================================================================================
; Establish the status of the devices in oMyDevices
For Index, MyDevice in oMyDevices
{
DeviceFound := ""
For Index, DeviceConnected in oDevicesConnected
{
If (MyDevice.DeviceID = DeviceConnected.DeviceID)
{
If (DeviceConnected.DeviceStatus = "OK"), DeviceFound := "Yes"
MyDevice.DeviceStatus := "Connected"
If (DeviceConnected.DeviceStatus = "Unknown"), DeviceFound := "Yes"
MyDevice.DeviceStatus := "Disconnected"
}
}
If !DeviceFound
MyDevice.DeviceStatus := "Disconnected"
}
;=============================================================================================
; Run or close scripts/programs if the devices are connected/disconnected when the script start.
Loop % oMyDevices.Count()
{
DeviceStatustStartup := oMyDevices[A_Index].DeviceName A_Space oMyDevices[A_Index].DeviceStatus
DevicesActions(DeviceStatustStartup)
DeviceStatustStartup := StrReplace(DeviceStatustStartup, "Disconnected", "Not connected")
strTooltip .= DeviceStatustStartup "`n"
}
If strTooltip
strTooltip := RTrim(strTooltip, "`n")
Tooltip, % strTooltip, 0, 0
SetTimer, RemoveToolTipDeviceStatus, -6000
;=============================================================================================
OnMessage(0x219, "WM_DEVICECHANGE")
WM_DEVICECHANGE(wParam, lParam, msg, hwnd)
{
SetTimer, CheckDevicesStatus , -1250
}
Return
;=============================================================================================
CheckDevicesStatus:
;=============================================================================================
; Check devices connected
oDevicesConnected.Delete(1, oDevicesConnected.Length())
For Device in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_PnPEntity")
oDevicesConnected.Push({"DeviceName":Device.Name, "DeviceID":Device.DeviceID, "DevicePNPClass":Device.PNPClass, "DeviceStatus":Device.Status})
;=============================================================================================
; Find which devices status has changed in oMyDevices
oMyDevicesStatusHasChanged := []
For Index, MyDevice in oMyDevices
{
DeviceFound := ""
For Index, DeviceConnected in oDevicesConnected
{
If (MyDevice.DeviceID = DeviceConnected.DeviceID)
If (DeviceConnected.DeviceStatus = "OK")
If (MyDevice.DeviceStatus = "Disconnected"), MyDevice.DeviceStatus := "Connected", DeviceFound := "Yes"
oMyDevicesStatusHasChanged.Push(MyDevice.DeviceName " Connected")
If (DeviceConnected.DeviceStatus = "Unknown")
If (MyDevice.DeviceStatus = "Connected"), MyDevice.DeviceStatus := "Disconnected", DeviceFound := "Yes"
oMyDevicesStatusHasChanged.Push(MyDevice.DeviceName " Disconnected")
}
If !DeviceFound
If (MyDevice.DeviceStatus = "Connected"), MyDevice.DeviceStatus := "Disconnected"
oMyDevicesStatusHasChanged.Push(MyDevice.DeviceName " Disconnected")
}
;=============================================================================================
; If devices in oMyDevices status has changed go to DevicesActions()
If (oMyDevicesStatusHasChanged)
{
strTooltip := ""
Loop % oMyDevicesStatusHasChanged.Count()
{
DevicesActions(oMyDevicesStatusHasChanged[1])
strTooltip .= oMyDevicesStatusHasChanged[1] "`n"
oMyDevicesStatusHasChanged.RemoveAt(1)
}
If strTooltip
strTooltip := RTrim(strTooltip, "`n")
Tooltip, % strTooltip, 0, 0
SetTimer, RemoveToolTipDeviceStatus, -6000
}
;=============================================================================================
return
;=============================================================================================
RemoveToolTipDeviceStatus:
ToolTip
return