Jump to content

WinGetTitle( "[ACTIVE]" ) always fails - how can that be?


Recommended Posts

The code below is almost straight out of the Help, and I feel like I must be doing something wrong in that the value returned from the WinGetTitle is always '0', meaning the IF branch is taken.

When I run the script (.AU3) or the compiled. EXE, during the Sleep call, I'll switch to some other application (e.g,. Notepad), or leave AutoIT active, but regardless, I never encounter a successful run. Any thoughts on what I might be doing wrong?

I've also tried "[ACTIVE]" in lowercase (ie.,. "[active]" which is what appears in the Help for WinGetTitle). I used upper case as that is what's shown in "Window Titles and Text (Advanced)" topic. Because hese two references differ, I am suspecting case does not matter?

Sleep( 3000 ) ; Allows switching to some other window to see if can get its title.
$sActiveWindowTitle = WinGetTitle( "[ACTIVE]" ) ; Get active windows' title, whether visible or hidden.  If multiple windows match
                                            ; this criteria, use the most recently active window.
If( $sActiveWindowTitle  = 0 ) Then
    MsgBox( 4096, "SYS_MSG_" & @ScriptLineNumber, "Error in attempting to get Title of Active Window." )
Else
    MsgBox( 4096, "SYS_MSG_" & @ScriptLineNumber, 'Title of active Window is "' & $sActiveWindowTitle )
Endif

Hopefully I have coded a simple mistake and it will be easy for someone else to spot. I just do not see how this can fail, for *some* window has to be active all the time, correct?

P.S. Anyway to search on "[ACTIVE]" - the results I got seemed to indicate the brackets were stripped out before the search was run such that I got *every* instance of "active"?

Link to comment
Share on other sites

P.S. Anyway to search on "[ACTIVE]" - the results I got seemed to indicate the brackets were stripped out before the search was run such that I got *every* instance of "active"?

I'm just curious .... how can more than one program be "Active" at a time?

Windows is a time-slicing operating system. As such, you can't have more than one "active" window.

Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Link to comment
Share on other sites

WinGetTitle() doesn't fail at all, it's your comparison that fails. Test it yourself:

$sActiveWindowTitle = WinGetTitle("[ACTIVE]")
ConsoleWrite($sActiveWindowTitle & @CRLF)
Link to comment
Share on other sites

Try

Sleep( 3000 ) ; Allows switching to some other window to see if can get its title.
$sActiveWindowTitle = WinGetTitle( "[ACTIVE]" ) ; Get active windows' title, whether visible or hidden.  If multiple windows match
                                            ; this criteria, use the most recently active window.
If Not $sActiveWindowTitle Then
    MsgBox( 4096, "SYS_MSG_" & @ScriptLineNumber, "Error in attempting to get Title of Active Window." )
Else
    MsgBox( 4096, "SYS_MSG_" & @ScriptLineNumber, 'Title of active Window is "' & $sActiveWindowTitle )
Endif

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Link to comment
Share on other sites

I'm just curious .... how can more than one program be "Active" at a time?

Windows is a time-slicing operating system. As such, you can't have more than one "active" window.

You're right. I got tripped up in how I interpreted the "WingGetTitle" Help Topic's "Remarks" content of:

WinGetTitle("[active]") returns the active window's title. WinGetTitle works on both minimized and hidden windows. If multiple windows match the criteria, the most recently active window is used.

by thinking that the 2nd sentence expanded on the first sentence (in my defense, the sentences do technically appear in the same paragraph and therefore should be dealing with the same topic).

I think the content would be clearer if written as a bulleted list with order of sentences reversed; that is (my added text appears in bold):

  • WinGetTitle works on both minimized and hidden windows. If multiple windows match the criteria, the most recently active window is used.
  • WinGetTitle("[active]") or WinGetTitle( "[ACTIVE]") returns the active window's title.
Edited by Gubba1
Link to comment
Share on other sites

WinGetTitle() doesn't fail at all, it's your comparison that fails. Test it yourself:

Duh - I should have thought about using the Alt+D "Debug to Console" feature.

Still, the WinGetTitle help topic states: "Failure: Returns numeric 0 if no title match" so I am still puzzled as to why the IF statement works like it does. The way I have coded the IF to check for "failure" case is valid, correct? Or in the case of a return value that can be of different types, should IsNumber or IsString be first used?

Link to comment
Share on other sites

When you compare a string against a number the string resolves to 0 so the comparison is True.

Quickie example:

$Text = 0
If $Text = "0" Then ; this works, because string 0 equals 0
    ConsoleWrite("string 0 equals numeric 0 " & @CRLF)
EndIf

$Text = "AutoIt rules"
If $Text = "0" Then ; this fails because string text doesn't equal string 0
    ConsoleWrite("string text equals string 0" & @CRLF)
Elseif $Text = 0 Then ; this works because string text equals numeric 0
    ConsoleWrite('string text equals numeric 0' & @CRLF)
EndIf

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
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
 Share

  • Recently Browsing   0 members

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