Python script to detect DNS mode
Submitted by jojo on Wed, 12/10/2008 - 04:05.
Hi,
I am using Dragon NaturallySpeaking preferred 9.5 with Natlink/Vocola. I need to be able to programmatically detect (and audibly announce) the mode that Dragon NaturallySpeaking is running in (normal, command, spelling etc.). Of course, if I can detect the mode the announcing should be easy with Vocola. Can anyone refer me to a Vocola or Natlink/python script that I can modify to detect the DNS mode? I assume it is possible to do this using Python but I am not enough of a Python programmer to write the script from scratch.
Regards,
Joe

Hello Joe, Detecting the
Hello Joe,
Detecting the modes is not possible, as far as I know. I've tried that when the modes just came in several versions of NatSpeak ago.
Setting the modes through natlink could be done. I experimented with that in the grammar _general.py in unimacro. As long as you switch on the modes through a natlink command (for example in this grammar) you can keep track of the mode through this grammar as well. (look for "mode" in the file _general.py, and look for the function "setMode" in natlinkutilsbj.py.)
A little experiment just now (with NatSpeak 10) did not work, so I do not know about the current status of all this.
Greetings, Quintijn
With AdvancedScript!
Actually, I found this just now with AdvancedScript:
Try the following line in some test command!
The results are:
Greetings, Quintijn
Power of Python
Hi Jojo,
Happy to see you want to try Python. I applaud that, and of course your efforts shall bear fruit. Please try the following:
from win32com.client import Dispatchapp = Dispatch("Dragon.DgnEngineControl")
print "Recognition mode: %d" % app.RecognitionMode
The recognition mode retrieved in this way will be an integer with the same values as mentioned by Quintijn above.
-- Chris.
Thanks for the advice
I'll see if I can work it into my system and I'll post back on the results.
Thanks again
Joe
Stuck already
I'm afraid I haven't gotten too far.
Remember, I am running Natlink Vocola with DNS 9.5 PREFERRED-so I don't have access to advanced scripting.
First, I tried to see if I could retrieve the DNS mode code from a Vocola command. I tried
1) Test Mode=MsgBoxConfirm("test: " & Eval(EngineControl.RecognitionMode), 64, "test DNS mode");
# the above doesn't work. Natlink complains that "EngineControl" is not defined
2) Test Mode=MsgBoxConfirm("test: " & Eval(Dragon.DgnEngineControl.RecognitionMode), 64, "test DNS mode");
# also doesn't work: "Dragon" is undefined
3) Test Mode=MsgBoxConfirm("test: " & Eval(Dispatch(Dragon.DgnEngineControl).RecognitionMode), 64, "test DNS mode");
# Natlink won't convert the file because of the Dispatch instruction
And I then tried to get the suggested code to run in the Python interpreter with the following results:
Python 2.3.5 (#62, Feb 8 2005, 16:23:02) [MSC v.1200 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from win32com.client import Dispatch
>>> app= Dispatch( "Dragon.DgnEngineControl")
>>> print 'Recognition mode: %d' % app.RecognitionMode
Traceback (most recent call last):
File "", line 1, in ?
File "C:\Python23\lib\site-packages\win32com\client\dynamic.py", line 496, in
__getattr__
raise AttributeError, "%s.%s" % (self._username_, attr)
AttributeError: Dragon.DgnEngineControl.RecognitionMode
>>>
I took a look at the file referred to above but I really couldn't understand it.
Any further suggestions?
Regards,
Joe
jojo wrote: I'm afraid I
I'm afraid I haven't gotten too far.
Remember, I am running Natlink Vocola with DNS 9.5 PREFERRED
>>> from win32com.client import Dispatch
>>> app= Dispatch( "Dragon.DgnEngineControl")
Hello jojo,
That is it then. The solution Chris proposed is essentially the same as the AdvancedScript solution I gave above. Python tries to access "AdvancedScript things" that are not accessible by the preferred version.
Quintijn
Python in DNS Preferred
Python actually does have access to DNS's recognition engine internals, even in the preferred and normal versions. The advanced script things are not exposed to the normal end-user in these cheaper versions, but under the hood as accessed from Python there appears to be no difference.
So, Jojo, I wonder what the problem on your system is. I've only tested the Python code I gave you on DNS v10. It might be that some of the internal components have been renamed since v9. I'm sorry that I don't have that version installed anywhere anymore, so can't help you directly. Anybody else know about such changes?
-- Chris.
Popping the hood
Hi Jojo,
Could you please run the following Python code and let me know what the output is? (By e-mail might be better, as I expect the output to be fairly large. I'll post any relevant information back to the forum here, if people would like that.)
from win32com.client.gencache import EnsureDispatch
app = EnsureDispatch("Dragon.DgnEngineControl", bForDemand=0)
print app
print "Attributes:"
print "\n".join([" - %r" % (a) for a in dir(app)])
properties = app._prop_map_get_.keys()
properties.sort()
print "Properties:"
print "\n".join([" - %r" % (a) for a in properties])
-- Chris.
Python output
Here you go.
http://abm.wso.net/Natlink.html
Good luck
Joe