Beginner question and script for dual monitors

Get help with using AutoHotkey and its commands and hotkeys
KG22

Beginner question and script for dual monitors

20 Jan 2015, 12:36

Hey everyone, here is what I have going on at work:

There are 2 TV's connected to one computer that display certain visualizations. Every night, those TV's get turned off completely with a switch which results in both browser windows being on the same screen rather than two separate screens when they are turned back on. I have to manually go in and drag one of them over to the other screen.

How could you have Chrome go to one screen and Firefox go to the other screen and have that repeat every minute? I say a minute because the TV's are turned back on at different times so that would make sure that the script would run within a minute.

I think it would also need to check to see if they are already open because both browsers stay open; one just gets put on top of the other one when the monitors are turned back on.

Also, does AHK have to be tied to a key combination to trigger it or can it run on it's own like a batch file that you could put in your start up folder?

Thanks in advance!
User avatar
boiler
Posts: 6131
Joined: 21 Dec 2014, 02:44

Re: Beginner question and script for dual monitors

20 Jan 2015, 22:44

This will do it. It is set up to fill each monitor with the browser windows, but that can be easily modified for whatever size windows you want. To exit the script, right-click on the icon in the tray and select Exit. A hotkey could be added to have it exit. Just add Esc::ExitApp or whatever.

To have it automatically start when Windows starts, compile the script and put the resulting .exe file in your StartUp folder.

Code: Select all

#Persistent
SysGet, Mon1, MonitorWorkArea, 1
SysGet, Mon2, MonitorWorkArea, 2
SetTimer, MoveBrowsers, 60000
return

MoveBrowsers:
	WinMove, ahk_exe chrome.exe,,Mon1Left, Mon1Top, Mon1Right - Mon1Left, Mon1Bottom - Mon1Top
	WinMove, ahk_exe firefox.exe,,Mon2Left, Mon2Top, Mon2Right - Mon2Left, Mon2Bottom - Mon2Top
return
lexikos
Posts: 7085
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Beginner question and script for dual monitors

21 Jan 2015, 02:33

I believe the following will work better than a timer; it should respond immediately to any change in display settings.

Code: Select all

OnMessage(0x7E, "OnDisplayChange")

OnDisplayChange() {
    ; Replace the content of this function with whatever.
    SysGet mc, MonitorCount
    Loop % mc {
        SysGet m, Monitor, %A_Index%
        s = %s%`nx %mLeft%:%mRight%  y %mTop%:%mBottom%
    }
    MsgBox Displays changed to:%s%
}
You don't need to compile the script if AutoHotkey is installed. Any kind of file you put in the Startup folder will run on startup, including scripts or documents.

If SysGet is used when the script starts, it will only get the right details if both screens are active at that time. Using it after the display-change message is received should work reliably. You will probably want to move the windows only if the monitor count > 1.
Also, does AHK have to be tied to a key combination to trigger it or can it run on it's own like a batch file that you could put in your start up folder?
AutoHotkey has to be running to respond to key combinations (hotkeys), but it doesn't have to respond to key combinations. If the script doesn't contain hotkeys or OnMessage (or one of a few other things), it will exit as soon as the "auto-execute" section is complete (like a batch file), unless you use the #Persistent directive as shown by boiler.

Return to “Ask For Help”

Who is online

Users browsing this forum: danners430, electrone77, Google [Bot], jameskhalil, mikeyww, pgarza and 56 guests