Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How do I set an action to trigger whenever a new window opens in autohotkey? For instance, I could set any Windows Explorer window to be marked as "Always on Top" as soon as it comes up. Or I could set any new Skype windows to always open on the second monitor, or always be semi-transparent, etc...

I think autohotkey can achieve this, but I don't know how. I need it to perform some arbitrary action on a new window as soon as it comes up.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

AutoHotkey is an off-spin created specifically for hot keys...

You might want to try the original AutoIt instead, it has a lot of automation features, including window events!

More specific, you can find a script here that tracks windows and acts on new windows.

#include <Array.au3>

; Initialize tracking arrays
Global $avWinListPrevious[1][2] = [[0, ""]], $avWinListCurrent

; Monitor unique window handles
While 1
    $avWinListCurrent = WinList("[REGEXPTITLE:.+[ \- ]GIMP]", "GNU Image Manipulation Program")
    For $n = $avWinListCurrent[0][0] To 1 Step -1
   ; Check has title and visible
        If ($avWinListCurrent[$n][0] <> "") And BitAND(WinGetState($avWinListCurrent[$n][1]), 2) Then
       ; Check for already seen
            $fFound = False
            For $i = 1 To $avWinListPrevious[0][0]
                If $avWinListCurrent[$n][1] = $avWinListPrevious[$i][1] Then
                    $fFound = True
                    ExitLoop
                EndIf
            Next

       ; New window found
            If Not $fFound Then
                WinMove("[REGEXPTITLE:.+[ \- ]GIMP]", "GNU Image Manipulation Program", 169, 0, 893, 771 )
            EndIf
        Else
            _ArrayDelete($avWinListCurrent, $n)
        EndIf
    Next
    $avWinListCurrent[0][0] = UBound($avWinListCurrent) - 1
    $avWinListPrevious = $avWinListCurrent
    Sleep(500)
WEnd
share|improve this answer
1  
Not true, you can do this with AHK: autohotkey.com/forum/topic63673.html –  KingRadical Dec 30 '11 at 23:38
    
@KingRadical: Why are you double posting? I did not state that its not possible. –  Tom Wijsman Dec 31 '11 at 1:55
    
Polling every 500 ms? terrible way to do it, and too much code for such a simple thing anyways. A good solution can be found in this AHK thread autohotkey.com/board/topic/85114-detecting-new-child-window –  GetFree Apr 19 '13 at 20:30
    
Didn't wrote this myself, but you're free to post your own answer. –  Tom Wijsman Apr 19 '13 at 20:36

Something like this would work nicely: http://www.autohotkey.com/forum/topic63673.html

and in case the link changes or goes down for any reason:

;========================================================================
; 
; Template:     WinTrigger (former OnOpen/OnClose)
; Description:  Act upon (de)activation/(un)existance of programs/windows
; Online Ref.:  http://www.autohotkey.com/forum/viewtopic.php?t=63673
;
; Last Update:  15/Mar/2010 17:30
;
; Created by:   MasterFocus
;               http://www.autohotkey.net/~MasterFocus/AHK/
;
; Thanks to:    Lexikos, for improving it significantly
;               http://www.autohotkey.com/forum/topic43826.html#267338
;
;========================================================================
;
; This template contains two examples by default. You may remove them.
;
; * HOW TO ADD A PROGRAM to be checked upon (de)activation/(un)existance:
;
; 1. Add a variable named ProgWinTitle# (Configuration Section)
; containing the desired title/ahk_class/ahk_id/ahk_group
;
; 2. Add a variable named WinTrigger# (Configuration Section)
; containing the desired trigger ("Exist" or "Active")
;
; 3. Add labels named LabelTriggerOn# and/or LabelTriggerOff#
; (Custom Labels Section) containing the desired actions
;
; 4. You may also change CheckPeriod value if desired
;
;========================================================================

#Persistent

; ------ ------ CONFIGURATION SECTION ------ ------

; Program Titles
ProgWinTitle1 = ahk_class Notepad
WinTrigger1 = Exist
ProgWinTitle2 = Calculator
WinTrigger2 = Active

; SetTimer Period
CheckPeriod = 200

; ------ END OF CONFIGURATION SECTION ------ ------

SetTimer, LabelCheckTrigger, %CheckPeriod%
Return

; ------ ------ ------

LabelCheckTrigger:
  While ( ProgWinTitle%A_Index% != "" && WinTrigger := WinTrigger%A_Index% )
    if ( !ProgRunning%A_Index% != !Win%WinTrigger%( ProgWinTitle := ProgWinTitle%A_Index% ) )
      GoSubSafe( "LabelTriggerO" ( (ProgRunning%A_Index% := !ProgRunning%A_Index%) ? "n" : "ff" ) A_Index )
Return

; ------ ------ ------

GoSubSafe(mySub)
{
  if IsLabel(mySub)
    GoSub %mySub%
}

; ------ ------ CUSTOM LABEL SECTION ------ ------

LabelTriggerOn1:
LabelTriggerOff1:
LabelTriggerOn2:
  MsgBox % "A_ThisLabel:`t" A_ThisLabel "`nProgWinTitle:`t" ProgWinTitle "`nWinTrigger:`t" WinTrigger
Return

; ------ END OF CUSTOM LABEL SECTION ------ ------

Please note I did not write this code.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.