Reveller Posted September 30, 2010 Share Posted September 30, 2010 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 Link to comment Share on other sites More sharing options...
KaFu Posted September 30, 2010 Share Posted September 30, 2010 Take a look at "Window Titles and Text (Advanced)" in the help-file. WinGetHandle("[ACTIVE]", "") OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2022-Nov-26) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Feb-16) HMW - Hide my Windows (2018-Sep-16) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2023-Jun-03) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
Varian Posted September 30, 2010 Share Posted September 30, 2010 (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 September 30, 2010 by Varian Link to comment Share on other sites More sharing options...
Ascend4nt Posted September 30, 2010 Share Posted September 30, 2010 (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 September 30, 2010 by Ascend4nt My contributions: Performance Counters in Windows - Measure CPU, Disk, Network etc Performance | Network Interface Info, Statistics, and Traffic | CPU Multi-Processor Usage w/o Performance Counters | Disk and Device Read/Write Statistics | Atom Table Functions | Process, Thread, & DLL Functions UDFs | Process CPU Usage Trackers | PE File Overlay Extraction | A3X Script Extract | File + Process Imports/Exports Information | Windows Desktop Dimmer Shade | Spotlight + Focus GUI - Highlight and Dim for Eyestrain Relief | CrossHairs (FullScreen) | Rubber-Band Boxes using GUI's (_GUIBox) | GUI Fun! | IE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) | Magnifier (Vista+) Functions UDF | _DLLStructDisplay (Debug!) | _EnumChildWindows (controls etc) | _FileFindEx | _ClipGetHTML | _ClipPutHTML + ClipPutHyperlink | _FileGetShortcutEx | _FilePropertiesDialog | I/O Port Functions | File(s) Drag & Drop | _RunWithReducedPrivileges | _ShellExecuteWithReducedPrivileges | _WinAPI_GetSystemInfo | dotNETGetVersions | Drive(s) Power Status | _WinGetDesktopHandle | _StringParseParameters | Screensaver, Sleep, Desktop Lock Disable | Full-Screen Crash Recovery Wrappers/Modifications of others' contributions: _DOSWildcardsToPCRegEx (original code: RobSaunder's) | WinGetAltTabWinList (original: Authenticity) UDF's added support/programming to: _ExplorerWinGetSelectedItems | MIDIEx UDF (original code: eynstyne) (All personal code/wrappers centrally located at Ascend4nt's AutoIT Code) Link to comment Share on other sites More sharing options...
Varian Posted October 1, 2010 Share Posted October 1, 2010 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 Link to comment Share on other sites More sharing options...
Reveller Posted October 1, 2010 Author Share Posted October 1, 2010 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: expandcollapse popup#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... Link to comment Share on other sites More sharing options...
Ascend4nt Posted October 1, 2010 Share Posted October 1, 2010 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? My contributions: Performance Counters in Windows - Measure CPU, Disk, Network etc Performance | Network Interface Info, Statistics, and Traffic | CPU Multi-Processor Usage w/o Performance Counters | Disk and Device Read/Write Statistics | Atom Table Functions | Process, Thread, & DLL Functions UDFs | Process CPU Usage Trackers | PE File Overlay Extraction | A3X Script Extract | File + Process Imports/Exports Information | Windows Desktop Dimmer Shade | Spotlight + Focus GUI - Highlight and Dim for Eyestrain Relief | CrossHairs (FullScreen) | Rubber-Band Boxes using GUI's (_GUIBox) | GUI Fun! | IE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) | Magnifier (Vista+) Functions UDF | _DLLStructDisplay (Debug!) | _EnumChildWindows (controls etc) | _FileFindEx | _ClipGetHTML | _ClipPutHTML + ClipPutHyperlink | _FileGetShortcutEx | _FilePropertiesDialog | I/O Port Functions | File(s) Drag & Drop | _RunWithReducedPrivileges | _ShellExecuteWithReducedPrivileges | _WinAPI_GetSystemInfo | dotNETGetVersions | Drive(s) Power Status | _WinGetDesktopHandle | _StringParseParameters | Screensaver, Sleep, Desktop Lock Disable | Full-Screen Crash Recovery Wrappers/Modifications of others' contributions: _DOSWildcardsToPCRegEx (original code: RobSaunder's) | WinGetAltTabWinList (original: Authenticity) UDF's added support/programming to: _ExplorerWinGetSelectedItems | MIDIEx UDF (original code: eynstyne) (All personal code/wrappers centrally located at Ascend4nt's AutoIT Code) Link to comment Share on other sites More sharing options...
Reveller Posted October 1, 2010 Author Share Posted October 1, 2010 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 outlookthe intranet contains a lot of self-help files for users to lookup a solutionusers don't look at the files; they call the helpdesk insteadthis costs a lot of money and timeso 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 problemif 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 clickthe 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? Link to comment Share on other sites More sharing options...
KaFu Posted October 1, 2010 Share Posted October 1, 2010 Something like this might work: expandcollapse popup#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 OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2022-Nov-26) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Feb-16) HMW - Hide my Windows (2018-Sep-16) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2023-Jun-03) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
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