Jump to content

Recommended Posts

Posted

Hello all

I am trying to get the window title and the executable filename by using the system tray.

Here is my example:

#include <TrayConstants.au3>
#include <WinAPIProc.au3>

Opt("TrayMenuMode", 3)

HotKeySet("{Esc}", "Terminate")

Run("Notepad.exe")  ; Example window

Local $Title = TrayCreateItem("Test")
TrayItemSetState(-1, 512)
TrayCreateItem("")
Local $FindTitle = TrayCreateItem("Get Active Window Title")
TrayCreateItem("")
Local $FindEXE = TrayCreateItem("Get Active Window Executable")
TrayCreateItem("")
Local $Exit = TrayCreateItem("Exit")
TraySetState($TRAY_ICONSTATE_SHOW)

WinActivate("[CLASS:Notepad]")  ; Making sure Notepad is the active window

While 1
    Switch TrayGetMsg()
        Case $FindTitle
            GetWinTitle()   ; Get the title of the active window? (But returns 'AutoIt v3')
        Case $FindEXE
            GetWinProcName()    ; Get the exe name for the active window? (But returns 'AutoIt.exe')
        Case $Exit
            Terminate()
    EndSwitch
WEnd

Func GetWinTitle()
    Local $Title = WinGetTitle("[active]")
    MsgBox(0, "Windows Title", $Title)
EndFunc

Func GetWinProcName()
    Local $PID = WinGetProcess("[active]")
    Local $EXE = _WinAPI_GetProcessName($PID)
    MsgBox(0, "App EXE Filename", $EXE)
EndFunc

Func Terminate()
    Exit
EndFunc

Obviously, when I click on the system tray, the system tray icon becomes the ‘active window.’ Is there any way around this?

 

 

Posted (edited)
#include <TrayConstants.au3>
#include <WinAPIProc.au3>

Opt("TrayMenuMode", 3)

HotKeySet("{Esc}", "Terminate")

ShellExecute("Notepad.exe")  ; Example window

Local $Title = TrayCreateItem("Test")
TrayItemSetState(-1, 512)
TrayCreateItem("")
Local $FindTitle = TrayCreateItem("Get Active Window Title")
TrayCreateItem("")
Local $FindEXE = TrayCreateItem("Get Active Window Executable")
TrayCreateItem("")
Local $Exit = TrayCreateItem("Exit")
TraySetState($TRAY_ICONSTATE_SHOW)

WinActivate("[CLASS:Notepad]")  ; Making sure Notepad is the active window

While 1
    Switch TrayGetMsg()
        Case $FindTitle
            Send("!{TAB}")
            Sleep(200)
            GetWinTitle()   ; Get the title of the active window? (But returns 'AutoIt v3')
        Case $FindEXE
            Send("!{TAB}")
            Sleep(200)
            GetWinProcName()    ; Get the exe name for the active window? (But returns 'AutoIt.exe')
        Case $Exit
            Terminate()
    EndSwitch
WEnd

Func GetWinTitle()
    Local $Title = WinGetTitle("[active]")
    MsgBox(0, "Windows Title", $Title)
EndFunc

Func GetWinProcName()
    Local $PID = WinGetProcess("[active]")
    Local $EXE = _WinAPI_GetProcessName($PID)
    MsgBox(0, "App EXE Filename", $EXE)
EndFunc

Func Terminate()
    Exit
EndFunc

via Alt+TAB

Edited by argumentum

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

  • Solution
Posted (edited)
#include <TrayConstants.au3>
#include <WinAPIProc.au3>
#include <Debug.au3>

Opt("TrayMenuMode", 3)
Global $g_SelfTitle = AutoItWinGetTitle() & " " & TimerInit()
AutoItWinSetTitle($g_SelfTitle)

HotKeySet("{Esc}", "Terminate")

ShellExecute("Notepad.exe")  ; Example window

Local $Title = TrayCreateItem("Test")
TrayItemSetState(-1, 512)
TrayCreateItem("")
Local $FindTitle = TrayCreateItem("Get Active Window Title")
TrayCreateItem("")
Local $FindEXE = TrayCreateItem("Get Active Window Executable")
TrayCreateItem("")
Local $Exit = TrayCreateItem("Exit")
TraySetState($TRAY_ICONSTATE_SHOW)

WinActivate("[CLASS:Notepad]")  ; Making sure Notepad is the active window

While 1
    Switch TrayGetMsg()
        Case $FindTitle
;~          Send("!{TAB}")
;~          Sleep(200)
            GetWinTitle()   ; Get the title of the active window? (But returns 'AutoIt v3')
        Case $FindEXE
;~          Send("!{TAB}")
;~          Sleep(200)
            GetWinProcName()    ; Get the exe name for the active window? (But returns 'AutoIt.exe')
        Case $Exit
            Terminate()
    EndSwitch
WEnd

Func GetWinTitle($iJustTheHWnd = 0)
;~     Local $Title = WinGetTitle("[active]")
    Local $n, $sTitle = "<not found>", $aArray = WinList()
    For $n = 1 To $aArray[0][0] - 1
        If $g_SelfTitle = $aArray[$n][0] Then
            While $aArray[$n + 1][0] = ""
                $n += 1
                If $n > $aArray[0][0] Then ExitLoop
            WEnd
            $sTitle = $aArray[$n + 1][0]
            If $iJustTheHWnd Then Return $aArray[$n + 1][1]
            ExitLoop
        EndIf
    Next
    MsgBox(0, "Windows Title", $sTitle)
;~  _DebugArrayDisplay($aArray)

EndFunc

Func GetWinProcName()
    Local $PID = WinGetProcess(GetWinTitle(1))
    Local $EXE = _WinAPI_GetProcessName($PID)
    MsgBox(0, "App EXE Filename", $EXE)
EndFunc

Func Terminate()
    Exit
EndFunc

via WinList()

Edited by argumentum
added If $n > $aArray[0][0] Then ExitLoop

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Posted

I was trying out the sending Alt Tab, and getting nowhere, fast.

GetWinTitle() always returned an empty string, and GetWinProcName() always returned ‘explorer.exe’.

Your second example works just great! Though way over my head, far too advanced for my newbie skills. But hey it works, so you have saved me hours if not days.

Thanks yet again argumentum!

Posted

For the record, these 2 lines would be a good start in OP's script, placed in the main loop (tested)

Case $TRAY_EVENT_MOUSEOVER
    If WinGetTitle("[active]") <> "AutoIt v3" Then $Title = WinGetTitle("[active]")

It is strange that the help file doesn't mention the event $TRAY_EVENT_MOUSEOVER in the topic TrayGetMsg when it is mentioned in the topic TraySetOnEvent

That's a big difference with the events listed the help file, topic GUIGetMsg and GUISetOnEvent : the same events are listed in these 2 topics.

"I think you are searching a bug where there is no bug... don't listen to bad advice."

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...