TwoCanoe Posted July 10 Posted July 10 Hello all I am trying to get the window title and the executable filename by using the system tray. Here is my example: expandcollapse popup#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?
argumentum Posted July 10 Posted July 10 You could send alt + tab to get back to it. But I think WinList presents the array with Z-order, so the 2nd one would be it. Try that ? Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
argumentum Posted July 10 Posted July 10 (edited) expandcollapse popup#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 July 10 by argumentum Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
Solution argumentum Posted July 10 Solution Posted July 10 (edited) expandcollapse popup#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 July 11 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.
TwoCanoe Posted July 10 Author Posted July 10 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! argumentum 1
pixelsearch Posted July 10 Posted July 10 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. argumentum 1 "I think you are searching a bug where there is no bug... don't listen to bad advice."
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now