script to restore/bring to front of a program, or open it if it is not active

My understanding of how Window Activation works is pretty simple. It restores the most recently use window of that type. That default behavior makes coding what you want very simple. In fact, I do something almost identical to what you are after with two simple code blocks.

+#w::
SetTitleMatchMode, 2 ; approximate match
IfWinExist, - Microsoft Word
{
WinActivate, - Micrsoft Word
}
IfWinNotExist, - Microsoft Word
{
Run FullFilePathToMicrosoftWord
}
return

I created a bit of an interesting synergy to work with multiple monitors. Note that on Windows 7, holding shift + windows key and using the arrows to navigate left and right will move the active window between monitors at light speed. So I tried something a bit clever and mapped word to shift + window + w. It restores word if already open or runs a new instance if not. The beauty of the approach is that I never let go of shift + windows key. By doing so I can both active/start word and move it to exactly the position I want using left right arrow keys (up will maximize by the way and down will minimize). Map +#r to restore the active window and you completely control all windows that you normally use effortlessly without the mouse. All you must remember is the first letter of the app you are trying to activate.

In answer to your query about making the script more generic:

1) Consider using

; Allows you to search for an approximate window title match
SetTitleMatchMode, 2  

; For example, you could do the following to activate any existing window with 
; "- Microsoft Word" as part of the title

WinWait, - Microsoft Word, 
IfWinNotActive, - Microsoft Word, , WinActivate, - Microsoft Word, 
WinWaitActive, - Microsoft Word,

; some other code here once you have the window active

Or another possibility:

;Active the window by finding its ahk_class
WinWait, ahk_class OpusApp, 
IfWinNotActive, ahk_class OpusApp, , WinActivate, ahk_class OpusApp, 
WinWaitActive, ahk_class OpusApp,

Note: The code above was just an adaptation of the autoscriptwriter's generic output. It is somewhat redundant but effective. To find something like a window's ahk_class consider using AutoIt3 window spy.

If you need to discover the title of a window you can get active title as shown in the ahk example help file

WinGetActiveTitle, Title
MsgBox, The active window is "%Title%".

Another trick you can use is to set the active title using WinSetTitle

Edit: Given that you have multiple instances of excel/word/powerpoint floating around, also consider solving that problem with officetabs free edition. It will allow you to keep all open excel files in a single instance for example instead of strewn about the taskbar.


I see you already know of the magnificent Autohotkey language ;). I'd use:

  • ifWinActive ; check if window active
  • WinGet, output, MinMax ; check if window minimized
  • ifWinExists ; check if window exists (but not active)
  • Run ; run the program if no process associated
  • Z-order / WinList ; those will help you list all of the instances / determine which instance is the first one

If you have more questions, you should definitely ask them on the autohotkey forum.

; The script activates a Word 2007 window
; The script uses 'winTitle' and 'winClass' variables to match the window
; so set those properly first.
; Press Shift + 1 + w to see how the script works.
;
winTitle := "Document1 - Microsoft Word" ; use Autoit Window Spy to get this
winClass := "OpusApp"
RunWait, winword
return

+1::
    KeyWait, w, D T1 ; KeyboardHook not needed
    if(errorlevel) {
        msgbox, % """Shift + 1 + w"" failed"
        return
    }
    Send, {Backspace} ; erase 'w'
    IfWinNotActive, %winTitle%
    {
        WinActivate, %winTitle%
        WinWaitActive, %winTitle%, , 3
        if(errorlevel)
            MsgBox, % "Failed to activate the window."
    } 
    else ; Last Found Window set
    {
        MsgBox, % "Window already activated."

    }
return


F11::Reload

It may not be as complicated as some, but here is what I use to launch most of my programs. The toggle allows you to minimize the window if it is already active.

^NumpadDot::ShowStart("Google Chrome", "chrome.exe")

ShowStart(title, exe, toggle = 0)
{
    If WinActive(title) and toggle
        WinMinimize %title%
    Else
        {
            IfWinExist, %title%
                WinActivate
            else
            {
                Run, %exe%
                WinActivate
            }
        }
}

Tags:

Autohotkey