Sign in to follow this  
Followers 0
Reveller

Trying to find the active window properties

9 posts in this topic

My Lord! I'm trying to find the active window so that I can get its properties (e.g. title). I thought the scipt below would be a good idea, but I think it returns EVERY CONCEIVABLE PROCESS my PC is running right now. Every time I click "OK", another MsgBox appears (Taskbar, Start Menu, uTorrent, MSCTFIME UI, Default IME, 0, even empty MsgBoxes) - about a 100 of them so far.

What is going on? I thought WinList would return only windows (I have about 4 open right now; Chome, Word, Windows Explorer, AutoIt editor). I am typing this in Chrome (duh!). How could I have this function now returning "Chrome" as my active window?

Any help would be greatly appreciated ;)

Func _ActiveWin()
    $windows = WinList();
    For $i = 1 to 
        $windows[0][0]
        MsgBox(64, "Window", $windows[$i][0])
    Next
EndFunc

Share this post


Link to post
Share on other sites



Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

As KaFu said:

Func _ActiveWin()
    Opt('WinTitleMatchMode', 4)
    $Windows = WinList('[ACTIVE]')
    For $i = 1 To $Windows[0][0]
        MsgBox(64, "Window", $Windows[$i][0])
    Next
EndFunc   ;==>_ActiveWin

Edited by Varian

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Varian, WinList('[ACTIVE]') should only return one window, so its really pointless to use the WinList function.

*edit: Reveller, if you would also want WinList to return something other than the active window, while narrowing it down to just what appears in the Alt-Tab list, you can check the WinGetAltTabWinList UDF in my signature.

Edited by Ascend4nt

Share this post


Link to post
Share on other sites

Varian, WinList('[ACTIVE]') should only return one window, so its really pointless to use the WinList function.

I know that, but I just used the OP's own code

Share this post


Link to post
Share on other sites

Thanks for all you replies, however, I think I might be doing something wrong.

> What I want is a systray app, that when left-clicked, shows me the active window on my desktop in a MsgBox:

#cs ----------------------------------------------------------------------------
 AutoIt Version: 3.3.6.1
 Author:         Reveller
#ce ----------------------------------------------------------------------------

#include <Constants.au3>

Opt("TrayMenuMode",1) ; Hide default tray menu items
TraySetIcon("Shell32.dll",-87)
TraySetClick(8) ; Only show the menu when right clicking
$aboutitem = TrayCreateItem("About")
$infoitem  = TrayCreateItem("Info")
$exititem  = TrayCreateItem("Exit")

While 1
 Switch TrayGetMsg() ; switch seems nicer in this case
 Case 0
     ContinueLoop
    Case $TRAY_EVENT_PRIMARYDOWN ; reaction to left clicking
     Msgbox(64,"",_ActiveWin())
 Case $aboutitem
     TrayItemSetState($aboutitem,$TRAY_UNCHECKED) ; stop the automated (un)checking when clicked
     Msgbox(64, "About", "Dennis' Cool Q&A app!")
 Case $infoitem
     TrayItemSetState($infoitem,$TRAY_UNCHECKED) ; stop the automated (un)checking when clicked
     MsgBox(64, "POPUP", "HELLO WORLD!")
 Case $exititem
     ExitLoop
 EndSwitch
WEnd
Exit

Func _ActiveWin()
    Opt('WinTitleMatchMode', 4)
    $Windows = WinList('[ACTIVE]')
    For $i = 1 To $Windows[0][0]
        MsgBox(64, "Window", $Windows[$i][0])
    Next
EndFunc   ;==>_ActiveWin

I am now typing this post in Chrome. If I leftclick on the systray icon, I would expect a MsgBox to appear saying something like "Chrome: Replying To Trying ..." but instead the MsbBox is empty. When I click "OK", another MsgBox appears with value "0".

Is there something I'm missing here? Does left-clicking on the systray icon take the [active] status away from Chrome, returning an empty MsgBox? Would it be best to look for the TOP window instead? What I want is for the windows title to popup of the window the user has last been active in...

Share this post


Link to post
Share on other sites

Clicking in the System Tray is changing the active window to the system tray window, so you're not gonna have any luck with that. Why would you need to click that anyway? Seems to defeat the purpose. Why not just map some hotkey to show that info when pressed instead?

Share this post


Link to post
Share on other sites

Isn't there a workaround to the active window loosing focus when clicking the icon?

The purpose of the application is this:

  • a user within our organisation is experiencing a problem with, say outlook
  • the intranet contains a lot of self-help files for users to lookup a solution
  • users don't look at the files; they call the helpdesk instead
  • this costs a lot of money and time
  • so we want to experiment with a systray tool that, by clicking, would redirect you to a specific section of the intranet (in this case: Outlook problems), so users have to click less to arrive at a possible answer to their problem
  • if it works, we would like to find somebody to expand the application, e.g. sending not only the name of the active window to the server, but also the specific error code (if there is one)
  • users need a simple tool, being a systray "panic button" the can click
  • the app will readout the current windows' name, opens a browser and redirects to the appropriate intranet address (e.g. http://intranet/knowledgebase/software/outlook?error=00X532FFS)
  • if users have to remember a hotkey, that's another barrier to start using the "panic button"
So is there any way to send the active window title to the app?

Share this post


Link to post
Share on other sites

Something like this might work:

#NoTrayIcon

#include <Constants.au3>
#include <WindowsConstants.au3>

Global $hWnd_Active, $hWnd_Active_Buffer

Opt("TrayMenuMode", 1) ; Default tray menu items (Script Paused/Exit) will not be shown.
Opt("TrayOnEventMode", 1)

TraySetClick(8)

$c_Tray_Exit = TrayCreateItem("Exit")
TrayItemSetOnEvent($c_Tray_Exit, "_Exit")

TraySetOnEvent($TRAY_EVENT_PRIMARYDOWN, "_Show_Intranet_Info")

TraySetIcon("Shell32.dll", 28)
TraySetState()
TraySetToolTip("Knowledge Base Tray" & @LF & "Shows Intranet Knowledge Base info on currently active program")

While 1
    Sleep(50)
    $hWnd_Active = WinGetHandle("[ACTIVE]", "")
    If WinGetProcess($hWnd_Active) <> @AutoItPID Then $hWnd_Active_Buffer = $hWnd_Active
WEnd

Func _Exit()
    Exit
EndFunc   ;==>_Exit

Func _Show_Intranet_Info()
    $Process_PID = WinGetProcess($hWnd_Active_Buffer)
    TrayTip("", "", 0)
    Switch _ProcessGetFilenameByPID($Process_PID)
        Case "SciTE.exe"
            If MsgBox(1 + 32 + 262144, "Knowledge Base Tray", "Open Knowledge Base article for: " & @CRLF & @CRLF & "SciTE.exe") = 1 Then
                ShellExecute("http://intranet/knowledgebase/SciTE")
            EndIf
        Case "Explorer.exe"
            ; Do noting (double click on tray)
            TrayTip("Knowledge Base Tray", "Activate the program you need help for...", 2)
        Case Else
            MsgBox(48 + 262144, "Knowledge Base Tray", "No Knowledge Base article found for " & @CRLF & @CRLF & _ProcessGetFilenameByPID($Process_PID))
    EndSwitch
EndFunc   ;==>_Show_Intranet_Info


; ===================================================================================================================
; Func _ProcessGetFilenameByPID($vProcessID)
;
; Returns the process name from Process ID #/name.
;
; $vProcessID = process name or Process ID # of process to find the child processes of
;
; Returns:
;   Success: Filename/Imagename of process
;   Failure: "" with @error set:
;       @error = 1 = process name/ID passed is either invalid or does not exist
;       @error = 2 = ProcessList() failure
;       @error = 4 = process not found in ProcessList (process may have ended, or PID is invalid)
;
; Author: Ascend4nt
; ===================================================================================================================

Func _ProcessGetFilenameByPID($vProcessID)
    If Not IsNumber($vProcessID) Then
        $vProcessID = ProcessExists($vProcessID)
        If $vProcessID = 0 Then Return SetError(1, 0, "")
    EndIf

    Local $aProcList = ProcessList()
    If @error Then Return SetError(2, @error, "")
    For $i = 1 To $aProcList[0][0]
        If $vProcessID = $aProcList[$i][1] Then Return $aProcList[$i][0]
    Next
    Return SetError(4, 0, "")
EndFunc   ;==>_ProcessGetFilenameByPID

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0