KnowBrainer Speech Recognition
Decrease font size
Increase font size
Topic Title: [Solved] Replacing Selection using Regular Expression
Topic Summary: TL;DR let an external program handle it
Created On: 03/11/2019 01:01 PM
Status: Post and Reply
Linear : Threading : Single : Branch
 [Solved] Replacing Selection using Regular Expression   - locution - 03/11/2019 01:01 PM  
 Replacing Selection using Regular Expression   - locution - 03/11/2019 01:55 PM  
 Replacing Selection using Regular Expression   - locution - 03/20/2019 06:42 PM  
 Replacing Selection using Regular Expression   - R. Wilke - 03/20/2019 08:24 PM  
 Replacing Selection using Regular Expression   - locution - 03/21/2019 12:08 PM  
 Replacing Selection using Regular Expression   - Lunis Orcutt - 03/21/2019 12:24 PM  
 Replacing Selection using Regular Expression   - R. Wilke - 03/21/2019 01:13 PM  
 Replacing Selection using Regular Expression   - locution - 03/21/2019 07:06 PM  
Keyword
 03/11/2019 01:01 PM
User is offline View Users Profile Print this message

Author Icon
locution
Senior Member

Posts: 124
Joined: 02/27/2019

Howdy,

I started reading the Nunce Advanced Scripting page but am having trouble understanding to what extent VBA is supported.

I made a simple script that uses the selected text, and verified that I can add something to it. But when I try a simple Regex.Replace() with it, the script editor growls.

Can someone please point me to what I'm missing? Also am I looking at the wrong scripting manual?

Sub Main
Dim my_clipboard As String
Dim new_text As String
SendKeys "^c"
Wait 0.2
my_clipboard = Clipboard$()
Rem THIS WORKS new_text = "TEST === " + my_clipboard + " === TEST"

Rem THIS FAILS even though it is correct VBA
new_text = Regex.Replace(my_clipboard, "^([^\r\n;]*);([^\r\n;]*);", "\2;\1;")
Clipboard new_text
SendKeys "^v"
End Sub

 



 03/11/2019 01:55 PM
User is offline View Users Profile Print this message

Author Icon
locution
Senior Member

Posts: 124
Joined: 02/27/2019

For the record, I was able to do it using AutoHotkey, but I hope there is a more direct way to do it directly in Dragon VBA.

Here is my current solution for anyone on the same track.

1. Dragon Command

Sub Main
Rem send Ctrl + Alt + K and let AutoHotkey handle it
SendSystemKeys "{Ctrl+Alt+k}"
End Sub

 

2. AutoHotkey key binding

#IfWinActive ahk_exe EditPadPro7.exe
; Swap Fields
^!k::
text_to_alter := GetSelectedText()
swapped_text := RegExReplace(text_to_alter, "^([^\r\n;]*);([^\r\n;]*);", "$2;$1;")
Sleep 100
Send % swapped_text
return
#IfWinActive

GetSelectedText() {
    tmp_clip := ClipboardAll ; save clipboard
    Sleep 20  
    Clipboard := "" ; start off empty to allow ClipWait to detect when text has arrived.
    Sleep 20
    Send, ^c ; simulate Ctrl+C: selection in clipboard
    ClipWait ; wait until clipboard contains data
    selection := Clipboard ; save the content of the clipboard
    Clipboard := tmp_clip ; restore old content of the clipboard
    return selection
}

 03/20/2019 06:42 PM
User is offline View Users Profile Print this message

Author Icon
locution
Senior Member

Posts: 124
Joined: 02/27/2019

Should I assume, from the lack of replies, that there is no way to do this natively?
 03/20/2019 08:24 PM
User is offline View Users Profile Print this message

Author Icon
R. Wilke
Top-Tier Member

Posts: 7881
Joined: 03/04/2007

You are expecting Dragon Advanced Scripting to do things it wasn't designed for. Even in my C++ programs, I need to include regex.h if I want to use regular expressions. Maybe you could link to a specific DLL in Advanced Scripting to achieve this, however.



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



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

DragonCapture KB Download (Latest)
DragonCapture Homepage

 03/21/2019 12:08 PM
User is offline View Users Profile Print this message

Author Icon
locution
Senior Member

Posts: 124
Joined: 02/27/2019

Thank you, that makes a lot of sense.
In VB.NET, I would normally have this line at the top of a program that uses regex:
Imports System.Text.RegularExpressions

In Dragon scripting, the script window will not let me save a script that has this line. Do you know an example of a script that shows how to include a library within Dragon scripting? Or is there a text or manual that you consider to be the gold reference for Dragon scripting? I am having a hard time getting started.

Thank you again for your thoughts.

 03/21/2019 12:24 PM
User is offline View Users Profile Print this message

Author Icon
Lunis Orcutt
Top-Tier Member

Posts: 39967
Joined: 10/01/2006

You might consider Larry Allen's Scripting for Dragon



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

Change "No" to "Know" w/KnowBrainer 2020
Trial Downloads
Dragon/Sales@KnowBrainer.com 
(615) 884-4558 ex 1

 03/21/2019 01:13 PM
User is offline View Users Profile Print this message

Author Icon
R. Wilke
Top-Tier Member

Posts: 7881
Joined: 03/04/2007

Originally posted by: locution Thank you, that makes a lot of sense. In VB.NET, I would normally have this line at the top of a program that uses regex: Imports System.Text.RegularExpressions In Dragon scripting, the script window will not let me save a script that has this line. Do you know an example of a script that shows how to include a library within Dragon scripting? Or is there a text or manual that you consider to be the gold reference for Dragon scripting? I am having a hard time getting started. Thank you again for your thoughts.

 

If Dragon Advanced Scripting is essential to you, you should definitely get Larry Allen's guide. It may not teach you many new things, but it covers all the basics, and then adds some nice and really useful details. 
 

In native Dragon Advanced Scripting, you can also reference many object libraries which are currently installed on the system. To do so, start an Advanced Scripting and put the cursor into the editor, then press the Alt key + Enter, and wait long enough for the list to populate. Not sure if there is a path to get to referencing the RegEx API this way.

 

Finally, if you have the KnowBrainer software, you can do VB.NET scripting right off the bat.



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



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

DragonCapture KB Download (Latest)
DragonCapture Homepage



 03/21/2019 07:06 PM
User is offline View Users Profile Print this message

Author Icon
locution
Senior Member

Posts: 124
Joined: 02/27/2019

Thank you both for your thoughts. After unsuccessfully experimenting with DLLcall, I found a suggestion on this thread that sounds like it will solve the problem.

> why don't you just create a .net executable with command line arguments to call the different functions, that will work in exactly the same way and you can then run it using ShellExecute within an advanced script.

Will explore some more later this week and update.

Statistics
32324 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 352 guests browsing this forum, which makes a total of 352 users using this forum.

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