Search code examples
naturallyspeaking

Copy the title of the active window to the clipboard in Microsoft Windows in Dragon NaturallySpeaking's advanced scripting


Is there any way to copy the title of the active window to the clipboard in Microsoft Windows in Dragon NaturallySpeaking's advanced scripting?

A workaround I use is to define an AutoHotkey script:

^!l::
WinGetActiveTitle, Title
Clipboard = %Title%
return

and call the keyboard shortcut in the voice command:

enter image description here

but I would prefer not to have to jungle it between AutoHotkey and Dragon NaturallySpeaking. Can it be done in "pure" advanced scripting?


Solution

  • Yes, you can copy the title of the active window to the clipboard using Dragon NaturallySpeaking's advanced scripting as follows:

    '
    '   get window title
    '
    Sub Main
        Clipboard ( GetWindowTitle )
    End Sub
    '
    '   Use these Windows Functions for Getting an active Window title
    '
    Declare Function GetForegroundWindow Lib "user32" () As Long
    '
    Declare Function GetWindowText Lib "user32" _
        Alias "GetWindowTextA" ( ByVal hwnd As Long , _
            ByVal lpString As String , ByVal cch As Long ) As Long
    '
    '   GetWindowTitle
    '   (Gets an active Window title)
    '
    Function GetWindowTitle() As String
        Dim x As Integer
        Dim TitleText As String * 300
        Dim hw As Long
        hw = GetForegroundWindow()
        x = GetWindowText ( hw , TitleText , Len ( TitleText ) )
        GetWindowTitle = Trim ( Left ( TitleText , x ) )
    End Function
    '
    

    Now, I keep all the functions in a global '#Uses file (with other declarations, functions and global constants, etc.), so I just need the Main Sub part, but you can put all of the referenced functions and declarations in the one script where you need it, too.

    Hth