#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases. #SingleInstance SendMode Input ; Recommended for new scripts due to its superior speed and reliability. SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory. DetectHiddenWindows, on ;needs to be on to pull outlook from system tray SetTitlematchmode, 2 ;easier to address a window by partial title SysGet,c_monitor,MonitorCount ;save how many monitors you start with in c(current)_monitor SetTimer,detectChange,5000 ;Every 5 seconds check to see if change in monitor# ;msgbox % "my ahk version: " A_AhkVersion ^!0:: ; get the wifi SSID, if there is one connected wifi_ssid := comBowenwebUtilityNetworking_getWifiSSID() ;msgbox, ssid is %wifi_ssid% SysGet,n_monitor,MonitorCount ;n_monitor will equal 3 at work, where I have three monitors if (n_monitor = 3){ HandleChange(n_monitor) Run, MapNetworkDrives.vbs drives-work.txt } ; wifi_ssid is 'baxter' on my home network else if (wifi_ssid = "baxter"){ ;MsgBox, Mapping home network drives. Run, MapNetworkDrives.vbs drives-home.txt } return detectChange: { SysGet,n_monitor,MonitorCount ;poll the system and save the number of monitors in the n(new)_monitor variable if (c_monitor <> n_monitor) ;check to see if current monitor count differs from the new reading of monitor numbers variable { HandleChange(n_monitor) c_monitor = %n_monitor% ;set current monitor numbers to the newly detected number } return } BuildWindowList(Wins) { ;set up the windows we care about adjusting, with their desired location, and if applicable, height and width window_list = ( - Outlook|1925,60 Google Chrome|-1928,-8 Skype for Business|1511,6,402,797 ) Loop,parse,window_list,`n { ind := A_Index Loop,parse,A_LoopField,| { Wins[ind,A_Index] := A_LoopField } } return Wins } HandleChange(n_monitor) { ;MsgBox, c_monitor is %c_monitor% and n_monitor is %n_monitor% if(n_monitor = 3) ;only do if in my 3 screen configuration – any other change does nothing { global c_monitor ; grab a copy of the global monitor count variable Wins := [] ; set up a new array Wins := BuildWindowList(Wins) ; populate the array with windows we care about placing on-screen WinGet, id, list,,, Program Manager Loop, %id% ; loop through all the open windows in the system { this_id := id%A_Index% WinGetTitle,this_title,ahk_id %this_id% if (StrLen(this_title) > 0) ; plenty of system 'windows' don't have a title at all - let's ignore those to speed things up SearchForWindow(this_title,Wins,this_id) } } return } ; search the array of windows we care about for the current system window. ; if found, place that window as needed SearchForWindow(sTitle,aWindows,iWindowId) { ; loop through all the windows we care about for key, value in aWindows { ;MsgBox, Comparing "%needle%" to "%this_title%" needle := Trim(value[1]) ; if we get a 'hit' for our sought-after window on the current window... IfInString, sTitle, %needle% { ; get the details for the desired location (and, maybe, the desired size) of the window second_var := value[2] win_position := [] win_position3 := "" len := 0 StringSplit,win_position,second_var,`, ; find out if win_position3 (the 'width' value) has any length - if not, then we'll be maximizing the window instead Stringlen,len,win_position3 ;MsgBox, Got a hit on "%needle%"! WinRestore,ahk_id %iWindowId% ;restore the window from Maximized of Minimized positions if (len > 0) { WinMove,ahk_id %iWindowId%,,%win_position1%,%win_position2%,%win_position3%,%win_position4% ;move window to desired x/y and h/w } else { WinMove,ahk_id %iWindowId%,,%win_position1%,%win_position2% winMaximize,ahk_id %iWindowId% } Sleep,100 } } }