@miroxlav solution did not work for me. I changed the script as follows.
- You still have to create two power saving configs
- The AutoHotKey script is typically executed at startup.
- The caught event is a bit different (WM_DISPLAYCHANGE)
- You have to identify your main monitor instance nam from powershell get-WmiObject or device manager or...
- power config UUIDs are hard coded in script too.
/*
Please note that it is not sufficient to count the number of monitors because the
main monitors goes off line when you close the lid.
Which resets the count to... 1
So instead, we just make our decision on the presence of a different monitor than the known
main one (hardcoded id, SN is a poor criterion).
*/
/*
Subscribe to windows event
0x7E = WM_DISPLAYCHANGE
*/
OnMessage(0x7E, "MsgMonitor")
MsgMonitor(wParam, lParam, msg) {
/* Sleep 2 sec because there is a delay before display is known to WMI */
Sleep 2000
/* default */
strComputer := "."
/* This is the one for my PC... */
myMonitor := "DISPLAY\LGD056E\4&13419694&0&UID265988_0"
objWMIService := ComObjGet("winmgmts:{impersonationLevel=impersonate}!\\" . strComputer . "\root\wmi")
colItems := objWMIService.ExecQuery("Select * FROM WMIMonitorID")._NewEnum
hasExternal := false
While colItems[objItem]
if objItem.instanceName != myMonitor {
hasExternal := True
}
if ( hasExternal ) {
/* this is the power config that does not set laptop to sleep on lid closing */event
Run, powercfg /s a48ebd52-0590-400d-b032-ac7f4302c0e1
} Else {
/* this instead is the power config that does set laptop to sleep on lid closing event */
Run, powercfg /s 377a8558-bff4-4f51-ab43-626b1aa5a65f
}
}