KnowBrainer Speech Recognition
Decrease font size
Increase font size
Topic Title: Function to differentiate windows
Topic Summary:
Created On: 05/09/2020 08:15 AM
Status: Post and Reply
Linear : Threading : Single : Branch
 Function to differentiate windows   - Alan Cantor - 05/09/2020 08:15 AM  
 Function to differentiate windows   - R. Wilke - 05/09/2020 08:57 AM  
 Function to differentiate windows   - Alan Cantor - 05/09/2020 10:05 AM  
 Function to differentiate windows   - Alan Cantor - 05/09/2020 10:23 AM  
 Function to differentiate windows[   - R. Wilke - 05/09/2020 11:01 AM  
 Function to differentiate windows   - Rapp - 05/09/2020 10:39 AM  
 Function to differentiate windows   - speechpro - 05/11/2020 11:03 AM  
 Function to differentiate windows   - Edgar - 05/11/2020 01:23 PM  
 Function to differentiate windows   - Ag - 05/10/2020 08:15 PM  
 Function to differentiate windows   - Edgar - 05/11/2020 10:12 AM  
 Function to differentiate windows   - monkey8 - 05/11/2020 02:22 PM  
 Function to differentiate windows   - Ag - 05/11/2020 04:58 PM  
 Function to differentiate windows   - Alan Cantor - 05/11/2020 07:24 PM  
Keyword
 05/09/2020 08:15 AM
User is offline View Users Profile Print this message


Alan Cantor
Top-Tier Member

Posts: 3940
Joined: 12/08/2007

I have revived an archived script for differentiating windows within an application. (I think Lindsay originally posted the code on this forum.)

 

In this example the script is application specific for Microsoft Outlook. It decides whether the window is the main Outlook user interface; an HTML message; or something else.


' Declare three Windows functions
Declare Function GetForegroundWindow& Lib "user32" ()
Declare Function GetWindowTextLengthA& Lib "user32" (ByVal hwnd&)
Declare Sub GetWindowTextA Lib "user32" (ByVal hwnd&, ByVal lpsz$, ByVal cbMax&)


' Use functions to obtain a string with the title of the current window.
' The string is returned as the value of the ActiveWindowTitle function.


Function ActiveWindowTitle$()
    ActiveWindow = GetForegroundWindow()
    TitleLen = GetWindowTextLengthA(ActiveWindow)
    Title$ = Space$(TitleLen)
    GetWindowTextA ActiveWindow,Title$,TitleLen+1
    ActiveWindowTitle$ = Title$
End Function
'
Sub Main ()
    Select Case ActiveWindowTitle$
        Case "- Outlook"
            MsgBox "This is the main Outlook user interface"
        Case "- Message (HTML"
            MsgBox "This is an email message"
        Case Else
            MsgBox "This is something else!"
  End Select
End Sub


 

 

 

The script is not recognizing the first two cases.

 

I'm using Windows 10 and Outlook 2019. I wonder whether something has changed since I last needed this script in Windows 7 and Outlook 2010.



 05/09/2020 08:57 AM
User is offline View Users Profile Print this message

Author Icon
R. Wilke
Top-Tier Member

Posts: 7401
Joined: 03/04/2007

I am not sure about Outlook 2019, and I would assume that the title of the window is actually longer, but you can always do:

MsgBox ActiveWindowTitle$

in the appropriate places of the script to see what the variable actually holds, currently.



-------------------------



No need to buy if all you want to do is try ...

DragonCapture KB Download (Latest)
DragonCapture Homepage



 05/09/2020 10:05 AM
User is offline View Users Profile Print this message


Alan Cantor
Top-Tier Member

Posts: 3940
Joined: 12/08/2007

My memory is that this method worked with partial matches and exact matches, but I'll confirm.
 05/09/2020 10:23 AM
User is offline View Users Profile Print this message


Alan Cantor
Top-Tier Member

Posts: 3940
Joined: 12/08/2007

That's it! The script expects exact matches. Thank you!

There does not appear to be a built-in way to test whether a string contains another string, but partial matches can be detected this rather inelegant way:

Sub Main ()
If InStr (ActiveWindowTitle$, "- Outlook") > 0 Then
MsgBox "This is the main Outlook user interface"
Exit Sub
End If
If InStr (ActiveWindowTitle$, "- Message") > 0 Then
MsgBox "This is an email message"
Exit Sub
End If
MsgBox "This is something else!"
End Sub


If someone knows a "pretty" way to do this, please share!
 05/09/2020 11:01 AM
User is offline View Users Profile Print this message

Author Icon
R. Wilke
Top-Tier Member

Posts: 7401
Joined: 03/04/2007

If someone knows a "pretty" way to do this, please share!

 

This is why we can't have pretty things!

 

A simple rewrite of the Select Case switch would look like that, in its generic form:

 

If ...

Elseif ...

Else ...

End If

 



-------------------------



No need to buy if all you want to do is try ...

DragonCapture KB Download (Latest)
DragonCapture Homepage

 05/09/2020 10:39 AM
User is offline View Users Profile Print this message


Rapp
Senior Member

Posts: 103
Joined: 12/12/2007

I believe that the Case function works only with a complete match with the title. You might consider using and "If ... Then" statement using Instr() to find a partial match.
 05/11/2020 11:03 AM
User is offline View Users Profile Print this message


speechpro
Power Member

Posts: 73
Joined: 11/09/2006

If you prefer the structure of Select Case to multipart If, you can use it in combination with other functions to create classes based on the variable you're processing.

Suppose you have a variable like LNAME. It might be upper, lower, mixed case, or even questionable data (not alpha). Say you want to handle A-H as group#1, I-P as group#2, Q-Z as group#3 and anything else as group#4 (possible error/investigate).

- use various operators (like, >, <, "") to assign the input to GROUPNUM {"GROUP1", "GROUP2", "GROUP3", "GROUP4"}

Then use Select Case on the group#

- select case GROUPNUM

..."GROUP1"

..."GROUP2"

etc.

 

 05/11/2020 01:23 PM
User is offline View Users Profile Print this message

Author Icon
Edgar
Top-Tier Member

Posts: 987
Joined: 04/03/2009

Don't forget you can also "stack" cases:

      Select Case UCASE (firstLetter)

         Case "A", "B", "C", "D", "E"

            folder = "G:\"

         Case "F", "G", "H", "I", "J"

            folder = "I:\"

         Case "K", "L", "M", "N", "O", "P"

            folder = "K:\"

         Case "Q", "R","S", "T", "U", "V", "W", "X", "Y", "Z"

            folder = "M:\"

         Case Else

            Exit All

      End Select



-------------------------

-Edgar
DPI 15.3, 64-bit Windows 10 Pro, OpenOffice & Office 365, Norton Security, Shure X2U XLR to USB mic adapter with Audio Technica DB135 vocal mic, Asus X299-Deluxe Prime, Intel Core i9-7940X (14 core, 4.3 GHz overclocked to 4.9 GHz), G.SKILL TridentZ Series 64GB (4 x 16GB) DDR4 3333 (PC4 26600) F4-3333C16Q-64GTZ, NVIDIA GIGABYTE GeForce GTX 1060 GV-N1060G1 GAMING-6GD REV 2.0 6GB graphics card with 3 1920x1080 monitors

 05/10/2020 08:15 PM
User is offline View Users Profile Print this message

Author Icon
Ag
Top-Tier Member

Posts: 495
Joined: 07/08/2019

I feel like an idiot asking this, but where do you place this function so that it can be called by more than one KnowBrainer command?

-------------------------

DPG15.6 (also DPI 15.3) + KB, Sennheiser MB Pro 1 UC ML, BTD 800 dongle, Windows 10 Pro, MS Surface Book 3, Intel Core i7-1065G7 CPU @ 1.3/1.5GHz (4 cores, 8 logical, GPU=NVIDIA Quadro RTX 3000 with Max-Q Design.

 05/11/2020 10:12 AM
User is offline View Users Profile Print this message

Author Icon
Edgar
Top-Tier Member

Posts: 987
Joined: 04/03/2009

Originally posted by: Ag I feel like an idiot asking this, but where do you place this function so that it can be called by more than one KnowBrainer command?

You asked this question in another thread, I answered but I suspect that you did not see the answer. It is always a good idea to start a new topic when you want to ask an off-topic question. The short answer is, create a text file somewhere convenient then use this convention:

'#Uses "C:\Program Files (x86)\KnowBrainer\KnowBrainer Professional 2017\WindowsAPI.txt"

Look here for a longer answer: 

http://www.knowbrainer.com/forums/forum/messageview.cfm?catid=11&threadid=34338&enterthread=y



-------------------------

-Edgar
DPI 15.3, 64-bit Windows 10 Pro, OpenOffice & Office 365, Norton Security, Shure X2U XLR to USB mic adapter with Audio Technica DB135 vocal mic, Asus X299-Deluxe Prime, Intel Core i9-7940X (14 core, 4.3 GHz overclocked to 4.9 GHz), G.SKILL TridentZ Series 64GB (4 x 16GB) DDR4 3333 (PC4 26600) F4-3333C16Q-64GTZ, NVIDIA GIGABYTE GeForce GTX 1060 GV-N1060G1 GAMING-6GD REV 2.0 6GB graphics card with 3 1920x1080 monitors

 05/11/2020 02:22 PM
User is offline View Users Profile Print this message

Author Icon
monkey8
Top-Tier Member

Posts: 3643
Joined: 01/14/2008

Alan the script should just be checking the end of the window title which you have already worked out and a completed version as shown below with comments, not so sure I did original though:

 

'Declare three Windows functions
Declare Function GetForegroundWindow& Lib "user32" ()
Declare Function GetWindowTextLengthA& Lib "user32" (ByVal hwnd& )
Declare Sub GetWindowTextA Lib "user32" (ByVal hwnd&, ByVal lpsz$, ByVal cbMax& )

 

' Use functions to obtain a string with the title of the current window. 
' The string is returned as the value of the ActiveWindowTitle function.

 

Function ActiveWindowTitle$()
    ' Get Window Handle
    ActiveWindow = GetForegroundWindow()
    ' Get Length of Window Title
    TitleLen = GetWindowTextLengthA(ActiveWindow)
    ' Create text buffer which is the same size as the active window title
    
' in this case it is filled with spaces
    Title$ = Space$(TitleLen)
    GetWindowTextA ActiveWindow,Title$,TitleLen+1
    ActiveWindowTitle$ = Title$
End Function

 

Sub Main ()
WindowTitle = ActiveWindowTitle$
If (InStr(WindowTitle, "- Outlook") > 0) Then
  MsgBox "This is the main Outlook user interface"
ElseIf (InStr (WindowTitle, "- Message (HTML)") > 0) Then
  MsgBox "This is an email message"
Else
  MsgBox "This is something else!"
End If
End Sub

 

Ag you can put the text file with the declared functions and subs anywhere you like as long as you specify in the script.

 

Lindsay

 

EDIT: you should really use the Mid$ function or InStr$ with index to work out the exact end of the string.  Or maybe even use Right$



-------------------------



 05/11/2020 04:58 PM
User is offline View Users Profile Print this message

Author Icon
Ag
Top-Tier Member

Posts: 495
Joined: 07/08/2019

Thanks, Edgar.

Textual include. No shared state (which I also asked elsewhere, and you answered).



-------------------------

DPG15.6 (also DPI 15.3) + KB, Sennheiser MB Pro 1 UC ML, BTD 800 dongle, Windows 10 Pro, MS Surface Book 3, Intel Core i7-1065G7 CPU @ 1.3/1.5GHz (4 cores, 8 logical, GPU=NVIDIA Quadro RTX 3000 with Max-Q Design.

 05/11/2020 07:24 PM
User is offline View Users Profile Print this message


Alan Cantor
Top-Tier Member

Posts: 3940
Joined: 12/08/2007


you should really use the Mid$ function or InStr$ with index to work out the exact end of the string. Or maybe even use Right$


Or a combination. In recent years Microsoft has blurred lines between Outlook and Outlook.com. In the past, the title bars were quite distinct; but now there is enough overlap that I'm needing to rewrite old scripts that differentiate Outlook windows from Outlook.com windows.

For example, the string "outlook.com" used to appear on the title bar of (you guessed it) outlook.com; now it's " - Outlook - ".

Regular Outlook used to contain " - Microsoft Outlook" but now it's " - Outlook".

Although it's not hard to determine which is which by analyzing title bar text, the similarities make it a bit trickier.
Statistics
31892 users are registered to the KnowBrainer Speech Recognition forum.
There are currently 0 users logged in.
The most users ever online was 12124 on 09/09/2020 at 04:59 AM.
There are currently 582 guests browsing this forum, which makes a total of 582 users using this forum.

FuseTalk Standard Edition v4.0 - © 1999-2021 FuseTalk™ Inc. All rights reserved.