KnowBrainer Speech Recognition
Decrease font size
Increase font size
Topic Title: Advanced Scripting libraries
Topic Summary: Where are they
Created On: 05/15/2023 01:08 AM
Status: Post and Reply
Linear : Threading : Single : Branch
 Advanced Scripting libraries   - Steve Morris - 05/15/2023 01:08 AM  
 Advanced Scripting libraries   - Matt_Chambers - 05/15/2023 05:36 AM  
 Advanced Scripting libraries   - Steve Morris - 05/15/2023 05:24 PM  
 Advanced Scripting libraries   - Steve Morris - 05/15/2023 11:01 PM  
 Advanced Scripting libraries   - Mav - 05/16/2023 01:50 AM  
 Advanced Scripting libraries   - Steve Morris - 05/16/2023 05:25 PM  
 Advanced Scripting libraries   - Steve Morris - 05/16/2023 07:42 PM  
 Advanced Scripting libraries   - Mav - 05/17/2023 01:42 AM  
 Advanced Scripting libraries   - Matt_Chambers - 05/16/2023 07:41 AM  
 Advanced Scripting libraries   - MDH - 05/17/2023 03:50 PM  
 Advanced Scripting libraries   - monkey8 - 05/17/2023 04:52 AM  
Keyword
 05/15/2023 01:08 AM
User is offline View Users Profile Print this message

Author Icon
Steve Morris
Top-Tier Member

Posts: 229
Joined: 07/22/2007

In the ActiveX Automation Members window of the Advanced Scripting window in Dragon Pro 16, how do you get references: "OLE Automation", "Microsoft Scripting Runtime", "Microsoft Visual Basic for Applications Extensibility 5.3", and "Windows Script Host Object Model." 

I only have DgnMyCommands and DNSTools.

 



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

 


 


Steve 

 05/15/2023 05:36 AM
User is offline View Users Profile Print this message

Author Icon
Matt_Chambers
Top-Tier Member

Posts: 821
Joined: 08/09/2018

Press Alt+Enter, then scroll down, and check the boxes for those references. They are there in Dragon 16.
 05/15/2023 05:24 PM
User is offline View Users Profile Print this message

Author Icon
Steve Morris
Top-Tier Member

Posts: 229
Joined: 07/22/2007

Oops. Cheers

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

 


 


Steve 

 05/15/2023 11:01 PM
User is offline View Users Profile Print this message

Author Icon
Steve Morris
Top-Tier Member

Posts: 229
Joined: 07/22/2007

I am so not a coder. It hurts my little brain. Does anyone have any insights as to what I am doing wrong in this script. It is an attempt to create a list of open windows. I know Dragon already does it. But I am trying to understand how do to this stuff and I am missing something critical. 

'#Language "WWB-COM"

Option Explicit

Sub Main()

' Constants
Const GW_CHILD As Long = 5
Const GW_HWNDNEXT As Long = 2

' Declarations
Dim hWnd As Long
Dim sCaption As String
Dim iNumWindows As Integer
Dim sWindows() As String
Dim i As Integer
Dim dlg As Object
Dim iSelected As Integer
Dim Shell As Object

' Get handle to shell object
Set Shell = CreateObject("WScript.Shell")

' API Declarations
Declare Function GetWindow Lib "user32" Alias "GetWindow" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Declare Function IsWindowVisible Lib "user32" (ByVal hWnd As Long) As Boolean
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpWindowText As String, ByVal nMaxCount As Integer) As Long

' Call API functions
Function MyGetWindow(ByVal hWnd As Long, ByVal wCmd As Long) As Long
MyGetWindow = GetWindow(hWnd, wCmd)
End Function

Function MyGetWindowText(ByVal hWnd As Long, ByVal lpWindowText As String, ByVal nMaxCount As Integer) As Long
MyGetWindowText = GetWindowText(hWnd, lpWindowText, nMaxCount)
End Function

' Count the number of visible windows
hWnd = MyGetWindow(0, GW_CHILD)
Do While hWnd <> 0
If IsWindowVisible(hWnd) Then
iNumWindows = iNumWindows + 1
End If
hWnd = MyGetWindow(hWnd, GW_HWNDNEXT)
Loop

' Store the window captions
ReDim sWindows(0 To iNumWindows - 1) As String
i = 0
hWnd = MyGetWindow(0, GW_CHILD)
Do While hWnd <> 0
If IsWindowVisible(hWnd) Then
sCaption = Space(256)
MyGetWindowText hWnd, sCaption, Len(sCaption)
sWindows(i) = sCaption
i = i + 1
End If
hWnd = MyGetWindow(hWnd, GW_HWNDNEXT)
Loop

' Show the list of open windows to the user
Set dlg = New SelectWindow
For i = 0 To iNumWindows - 1
dlg.WindowsList.AddItem sWindows(i)
Next i
iSelected = dlg.Show()

' Bring the selected window into focus
If iSelected >= 0 Then
Set Shell = CreateObject("WScript.Shell")
Shell.AppActivate sWindows(iSelected)
End If

End Sub


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

 


 


Steve 

 05/16/2023 01:50 AM
User is offline View Users Profile Print this message

Author Icon
Mav
Top-Tier Member

Posts: 723
Joined: 10/02/2008

Hi Steve!

One of the most impoprtant things when asking for help (especially with a programming-related program) is to be as precise as possible.

 

You wrote one line what you want to achive, but don't tell us why the code you posted doesn't do what you want.

Are there any errors?

Is the behavior different from what you expect?

 

Just for fun I fed your code into ChatGPT and asked it to explain the code.

This is the result:


This Visual Basic code is used to create a script that lists all open windows and allows the user to select a window to bring into focus. Here's a breakdown of the code:
1. The code starts with the declaration of some constants, such as `GW_CHILD` and `GW_HWNDNEXT`, which are used later in the code.
2. Various variables are declared, including `hWnd` (window handle), `sCaption` (window caption), `iNumWindows` (number of visible windows), `sWindows()` (array to store window captions), `i` (counter variable), `dlg` (dialog object), and `Shell` (shell object).
3. The code creates an instance of the shell object using the `CreateObject` function.
4. Several API declarations are made using the `Declare` statement. These declarations allow the code to use functions from the user32.dll library, such as `GetWindow`, `IsWindowVisible`, and `GetWindowText`. These functions are used to retrieve information about windows.
5. Two functions, `MyGetWindow` and `MyGetWindowText`, are defined. These functions serve as wrappers for the corresponding API functions, providing a simpler interface for the code to use.
6. The code counts the number of visible windows by iterating through the windows using the `GetWindow` function and checking if each window is visible using the `IsWindowVisible` function. The counter `iNumWindows` is incremented for each visible window.
7. An array `sWindows()` is created with a size of `iNumWindows` to store the captions of the visible windows.
8. The code iterates through the windows again and retrieves the captions of the visible windows using the `GetWindowText` function. The captions are stored in the `sWindows()` array.
9. A dialog object `dlg` is created using the `SelectWindow` class. This dialog will be used to display the list of open windows to the user.
10. The code adds each window caption from the `sWindows()` array to the list of windows in the dialog object using the `AddItem` method.
11. The `Show` method of the dialog object is called, which displays the dialog to the user and returns the index of the selected window.
12. If a window is selected (`iSelected >= 0`), the code uses the shell object and the `AppActivate` method to bring the selected window into focus.
Overall, this code allows the user to select a window from a list of open windows and brings the selected window to the front. It demonstrates the use of API functions and objects in Visual Basic to interact with the Windows operating system. 


Even with the free version you can use it in a situation like this very efficiently.

hth
mav

 05/16/2023 05:25 PM
User is offline View Users Profile Print this message

Author Icon
Steve Morris
Top-Tier Member

Posts: 229
Joined: 07/22/2007

Oh well. 



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

 


 


Steve 



 05/16/2023 07:42 PM
User is offline View Users Profile Print this message

Author Icon
Steve Morris
Top-Tier Member

Posts: 229
Joined: 07/22/2007

Sorry. I was assuming too much. It fails all over the place. The declarations of functions returns "unterminated block statement" variable keep saying they need to be defined. For this reason, I thought it might have been something obviously stupid I was missing. 

I will keep working at it. Thanks. 



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

 


 


Steve 

 05/17/2023 01:42 AM
User is offline View Users Profile Print this message

Author Icon
Mav
Top-Tier Member

Posts: 723
Joined: 10/02/2008

I think the most important skill you have to learn is how to debug effectively.

The script editor in Dragon is not really well-suited for this task, I'm afraid.

 

But if you get the information which variables are not declared that's a first step.

 

You can either declare all remaining variables or remove the "Option Explicit" statement, in which case variables are declared the first time they're being used.

While it's annoying having to declare each variable, it still offers an additional layer of protection against typos, mostly.

 

Cheers

mav

 05/16/2023 07:41 AM
User is offline View Users Profile Print this message

Author Icon
Matt_Chambers
Top-Tier Member

Posts: 821
Joined: 08/09/2018

Yes, ChatGPT is very good at code. Not very good at much else, but that's for another forum.
 05/17/2023 03:50 PM
User is offline View Users Profile Print this message

Author Icon
MDH
Top-Tier Member

Posts: 2336
Joined: 04/02/2008

"Yes, ChatGPT is very good at code. Not very good at much else, but that's for another forum."

 

Well, upon further reflection, maybe that is not the case:

 

ChatGPT Egg Balancing Task Convinced Microsoft That AGI Is Closer (businessinsider.com)

 

MDH



-------------------------
 05/17/2023 04:52 AM
User is offline View Users Profile Print this message

Author Icon
monkey8
Top-Tier Member

Posts: 4186
Joined: 01/14/2008

I am so not a coder. It hurts my little brain. Does anyone have any insights as to what I am doing wrong in this script. It is an attempt to create a list of open windows. I know Dragon already does it. But I am trying to understand how do to this stuff and I am missing something critical. 

 

I would say that the Dragon debugger is more than adequate to resolve the issue and what you're missing is programming experience in VB.  The first thing you need to do is look at the format/organisation of the script.  Looks like VB code possibly taken straight from Google.  It is not in the correct format and syntax for a WinWrap basic script.  What Is the Structure of a WinWrap Basic Script? Taking it stage by stage this is what I would do in terms of organisation of the script(there are other ways):

 

1) Declare your variables, constants and functions and then declare the Sub Main which calls on the variables, constants and declared functions.

 

Constants

Variables

Functions

Sub Main

 

2) So the first thing wrong with the script is that the Sub Main is declared on line 5 in the wrong syntax.  Remove the Sub Main from line 5 (in other words remove line 5) and then run the debugger, you run the debugger by green play button.

 

3)  The debugger will then fail on line 22 Set Shell = CreateObject ("WScript.Shell")

 

Again you need programming experience to understand the issue but I would set the initialisation within Sub Main.  So remove this line, copying it to insert later.  Run the debugger again.  This time the debugger will run through all the constant declarations, all the variable declarations and all the function declarations and then fail on the line hWnd = MyGetWindow(0, GW_CHILD) .  The reason for this is the these lines should be declared within your Sub Main so you need to insert your Sub Main (which we removed earlier) just before the failing line and also add the other line of code we removed earlier (Set Shell = CreateObject ("WScript.Shell").

 

So this then leaves us with the following script, run the debugger on the script below and then carry on your debugging from there(Clue: From the failing line onwards it's not in a WinWrap basic style/form for displaying a dialogue. ):

 

'#Language "WWB-COM"

Option Explicit



' Constants
Const GW_CHILD As Long = 5
Const GW_HWNDNEXT As Long = 2

' Variables
Dim hWnd As Long
Dim sCaption As String
Dim iNumWindows As Integer
Dim sWindows() As String
Dim i As Integer
Dim dlg As Object
Dim iSelected As Integer
Dim Shell As Object


' Windows API Function Declarations
Declare Function GetWindow Lib "user32" Alias "GetWindow" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Declare Function IsWindowVisible Lib "user32" (ByVal hWnd As Long) As Boolean
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpWindowText As String, ByVal nMaxCount As Integer) As Long

' Your Function Declarations
Function MyGetWindow(ByVal hWnd As Long, ByVal wCmd As Long) As Long
MyGetWindow = GetWindow(hWnd, wCmd)
End Function

Function MyGetWindowText(ByVal hWnd As Long, ByVal lpWindowText As String, ByVal nMaxCount As Integer) As Long
MyGetWindowText = GetWindowText(hWnd, lpWindowText, nMaxCount)
End Function
' Main  Routine Sub Main
Sub Main
' Get handle to shell object
Set Shell = CreateObject("WScript.Shell")
' Count the number of visible windows
hWnd = MyGetWindow(0, GW_CHILD)
Do While hWnd <> 0
If IsWindowVisible(hWnd) Then
iNumWindows = iNumWindows + 1
End If
hWnd = MyGetWindow(hWnd, GW_HWNDNEXT)
Loop

' Store the window captions
ReDim sWindows(0 To iNumWindows - 1) As String
i = 0
hWnd = MyGetWindow(0, GW_CHILD)
Do While hWnd <> 0
If IsWindowVisible(hWnd) Then
sCaption = Space(256)
MyGetWindowText hWnd, sCaption, Len(sCaption)
sWindows(i) = sCaption
i = i + 1
End If
hWnd = MyGetWindow(hWnd, GW_HWNDNEXT)
Loop

' Show the list of open windows to the user
Set dlg = New SelectWindow
For i = 0 To iNumWindows - 1
dlg.WindowsList.AddItem sWindows(i)
Next i
iSelected = dlg.Show()

' Bring the selected window into focus
If iSelected >= 0 Then
Set Shell = CreateObject("WScript.Shell")
Shell.AppActivate sWindows(iSelected)
End If
'End of main routine
End Sub


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



KnowBrainer Speech Recognition » Dragon Speech Recognition » Advanced Scripting libraries

Statistics
32617 users are registered to the KnowBrainer Speech Recognition forum.
There are currently 1 users logged in.
The most users ever online was 12124 on 09/09/2020 at 04:59 AM.
There are currently 112 guests browsing this forum, which makes a total of 113 users using this forum.

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