That was requested a lot on the forum. I didn't find any script that was noob friendly to achieve this. I made one, but I don't have a programmer background. Just want to make sure that it's not too buggy before releasing it. I'm asking for help if any users are interested and want to take a look at it and test their devices. Any help for fixes or tips would be greatly appreciated !
UPDATE:
The script was officially released on this page, go there if you want to download the latest version:
viewtopic.php?f=6&t=108930
Keep in mind, this script is not finished yet. Bookmark this page if you want to keep up to date.
DeviceIDFinder
Find your device unique ID.
How to use:
- Run the programs and follow the instructions.
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
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 ) )
}
;=============================================================================================
oConnectedDeviceIDs := {}
Loop, Parse, ListConnectedDeviceIDs, "`n"
oConnectedDeviceIDs.Push({"DeviceID":A_Loopfield})
;=============================================================================================
MsgBox, 64, Find deviceID, Unplug your device and press OK
;=============================================================================================
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 ) )
}
;=============================================================================================
Loop, Parse, strListConnectedDeviceIDs, "`n"
{
For Index, Element in oConnectedDeviceIDs
{
If InStr(strListConnectedDeviceIDs, Element.DeviceID)
oConnectedDeviceIDs.RemoveAt(Index)
}
}
;=============================================================================================
For Index, Element in oConnectedDeviceIDs
DeviceIDFound .= Element.DeviceID "`n"
;=============================================================================================
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
Run scripts or programs when a specific device is connected/disconnected.
Works with devices connected to PC via USB, Bluetooth, HDMI, Headphone Jack etc... I'm not sure about what is working and what is not working, needs more testing.
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 or programs that you want to run/close when the devices are connected/disconnected at the bottom of the script.
Code: Select all
/*
Script: DeviceIDPnP.ahk
Author: XMCQCX
Date: 2022-09-27
Version: 1.1.1
Change log:
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 - Run or close scripts/programs if the devices are connected/disconnected when the script start.
Known issue:
- All scripts/programs not launching/closing when connecting multiple devices in "oMyDevices" at the same time or in quick succession.
Not an issue when the script is launch.
*/
#NoEnv
#SingleInstance, Force
SendMode Input
SetWorkingDir, %A_ScriptDir%
oMyDevices := {}
oMyDevices.Push({"DeviceName":"USB Kingston DataTraveler 3.0","DeviceID":"USB\VID_0951&PID_1666\E0D55EA573DCF450E97C104C", "Status":""})
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", "Status":""})
oMyDevices.Push({"DeviceName":"HDMI Samsung TV","DeviceID":"SWD\MMDEVAPI\{0.0.0.00000000}.{ED3C7A62-B05B-44C6-ACD8-BCAA1E894265}", "Status":""})
;=============================================================================================
; List all devices connected
For Device in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_PnPEntity")
ListDevicesConnected .= Device.Name A_Tab Device.DeviceID A_Tab Device.PNPClass A_Tab Device.Status "`n"
;=============================================================================================
; Establish the status of the devices in oMyDevices
For Index, Element in oMyDevices
{
If InStr(ListDevicesConnected, Element.DeviceID)
{
EscapeStrElementDeviceID := RegExReplace(Element.DeviceID, "[\Q\.*?+[{|()^$\E]", "\$0")
RegExMatch(ListDevicesConnected, "`nm)^(.*?)" A_TAB EscapeStrElementDeviceID A_TAB "(.*?)" A_TAB "(.*)$", ElementsDevice)
If (ElementsDevice3 = "OK")
oMyDevices[Index].Status := "Connected"
If (ElementDevice3 = "Unknown")
oMyDevices[Index].Status := "Disconnected"
}
Else
oMyDevices[Index].Status := "Disconnected"
}
;=============================================================================================
; Run or close scripts/programs if the devices are connected/disconnected when the script start.
Loop % oMyDevices.Count()
{
ThisDeviceStatusAtStartup := oMyDevices[A_Index].DeviceName A_Space oMyDevices[A_Index].Status
DeviceActions(ThisDeviceStatusAtStartup, ShowToolTip := "No")
}
;=============================================================================================
OnMessage(0x219, "WM_DEVICECHANGE")
WM_DEVICECHANGE(wParam, lParam, msg, hwnd)
{
SetTimer, CheckDevicesStatus , -1000
}
Return
;=============================================================================================
CheckDevicesStatus:
; Prevent the script to continue if it already previously ran during the last 1500ms.
ElapsedTime := A_TickCount - StartTime
if ElapsedTime between 1 and 1500
{
StartTime := A_TickCount
return
}
;=============================================================================================
; List all devices connected
ListDevicesConnected := ""
For Device in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_PnPEntity")
ListDevicesConnected .= Device.Name A_Tab Device.DeviceID A_Tab Device.PNPClass A_Tab Device.Status "`n"
;=============================================================================================
; Find which device status has changed in oMyDevices
ThisDeviceStatusHasChanged := ""
For Index, Element in oMyDevices
{
If InStr(ListDevicesConnected, Element.DeviceID)
{
EscapeStrElementDeviceID := RegExReplace(Element.DeviceID, "[\Q\.*?+[{|()^$\E]", "\$0")
RegExMatch(ListDevicesConnected, "`nm)^(.*?)" A_TAB EscapeStrElementDeviceID A_TAB "(.*?)" A_TAB "(.*)$", ElementDevice)
If (ElementDevice3 = "OK")
If (oMyDevices[Index].Status = "Disconnected"), oMyDevices[Index].Status := "Connected"
ThisDeviceStatusHasChanged := Element.DeviceName " Connected"
If (ElementDevice3 = "Unknown")
If (oMyDevices[Index].Status = "Connected"), oMyDevices[Index].Status := "Disconnected"
ThisDeviceStatusHasChanged := Element.DeviceName " Disconnected"
}
If !InStr(ListDevicesConnected, Element.DeviceID)
If (oMyDevices[Index].Status = "Connected"), oMyDevices[Index].Status := "Disconnected"
ThisDeviceStatusHasChanged := Element.DeviceName " Disconnected"
}
;=============================================================================================
; If a device in oMyDevices status has changed go to DeviceActions()
If ThisDeviceStatusHasChanged
DeviceActions(ThisDeviceStatusHasChanged, ShowToolTip := "Yes")
StartTime := A_TickCount
return
;=============================================================================================
DeviceActions(ThisDeviceStatusHasChanged, ShowToolTip := "") {
If (ShowToolTip = "Yes") {
CoordMode, ToolTip, Screen
Tooltip, % ThisDeviceStatusHasChanged, 0, 0
SetTimer, RemoveToolTipDeviceStatus, -5000
}
;=============================================================================================
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"
;=============================================================================================
}
RemoveToolTipDeviceStatus:
ToolTip
return