Search code examples
automationheadless

Detect when Win10 is headless and run BAT file


I want to launch "D:\Programmi\usbmmidd(virtual-screen)\$-SCREEN-ADD.bat" when there are no physical or virtual screens.

I've tried with AutoHotKey, since I already can trigger run $-SCREEN-ADD.BAT with a hotkey.

This is what I have. (I don't need the automation to use AHK, it's just what I'm most familiar with.)

OnMessage(0x219, "MsgMonitor")
MsgMonitor(wParam, lParam, msg)
{
SysGet, MonitorCount, MonitorCount
if (MonitorCount<1) {
   run "D:\Programmi\usbmmidd(virtual-screen)\$-SCREEN-ADD.bat"
   }
}

Solution

  • WORKING SOLUTION:

    ; ►►►►►►►►►►►►►► 𝐊𝐄𝐄𝐏 𝐎𝐍 𝐓𝐎𝐏 ◄◄◄◄◄◄◄◄◄◄◄◄◄◄◄
    OnMessage(0x219, "MsgMonitor")
    MsgMonitor(wParam, lParam, msg)
    {
       SysGet, MonCount, MonitorCount      ;► num of screens
       SysGet, W1, 78           ;► width  of all the screens
       SysGet, H1, 79           ;► height of all the screens
       if (MonCount=1 and W1=640 and H1=480) {
          run "D:\Programmi\usbmmidd(virtual-screen)\$-SCREEN-ADD.bat"
       }
    }
    ; ►►►►►►►►►►►►►► 𝐊𝐄𝐄𝐏 𝐎𝐍 𝐓𝐎𝐏 ◄◄◄◄◄◄◄◄◄◄◄◄◄◄◄
    
    ; PUT HERE YOUR OTHER SCRIPTS IF NEEDED
    

    DESCRIPTION: the script runs with any change in hardware. It adds a virtual screen (by running $-SCREEN-ADD.bat) whenever it detects only 1 screen with a resolution of 640x480, which is what Windows thinks it has when it has no physical or virtual monitors connected.

    NOTE: this script has to be in the top of the file, which is the auto-execute portion. More info on the script in this thread on the AutoHotKey forum.