Efficient Natlink macro to switch between opened windows
Built-in commands provided by Dragon NaturallySpeaking for switching between opened applications are cumbersome and using the mouse grid commands for clicking at the right spot on the taskbar is really inefficient. For this reason, I've created a macro that allows switching to an opened application by simply saying Go N, where N is the position of the application in the taskbar. This macro is independent of the desktop resolution and the number of opened applications. It finds the position of the application in the taskbar and generates a mouse click at this position. It requires Auto-IT to be installed (not a big deal
.
Here is the natlink macro:
#----------------------------------------
# Filename: _go_task.py
# JFournier 2009
#----------------------------------------
#
# This macro allows the user to switch to a given Window by saying Go <N>,
# where <N> is the window's position in the Windows taskbar, starting from the left.
#
# This macro is independent of the desktop resolution and the number of opened applications.
# It finds the position of the application in the taskbar and generates a mouse click at
# this position. It requires Auto-IT to be installed (www.autoitscript.com)).
#
#----------------------------------------
#
# Usage:
# Go <window's position in the taskbar from the left>
#
# Example:
#
# User wants to switch to the third window in the taskbar
# He says: "Go 3"
#
#----------------------------------------
# March 28,2009: Initial version
#----------------------------------------
# Installation:
#
# 1. Download and Install Auto-IT 3
# (http://www.autoitscript.com/autoit3/downloads.shtm...).
# 2. Save the Auto-IT code in your natlink user directory in a file called "go_task.au3"
# 3. Compile "go_task.au3" by right-clicking on the file and selecting "Compile script"
# (a file called "go_task.exe" will be generated)
# 4. Put this Natlink script in your natlink user directory as "_go_task.py"
# It is important that the name begins with an underscore to allow this macro
# to be called from any program
# 5. Turn Dragon NaturallySpeaking off and then on
# 6. Say "Go 2" and see the magic!
#
# NOTE: Your Natlink user directory is shown the in "Configure Natlink & Vocola" utility
#
# Known issue:
#
# Some applications have sub-windows that are considered as active
# windows by the Auto-IT script but that do not appear in the taskbar.
# This sometimes results in a click at the wrong place.
#
#----------------------------------------
#
import re
import sys, string
import os # access to file information
import os.path # to parse filenames
import natlink
from natlinkutils import *
from os import system
NatLinkFolder = os.path.split(sys.modules['natlinkmain'].__dict__['__file__'])[0]
NatLinkFolder = re.sub(r'\core$', "", NatLinkFolder)
class ThisGrammar(GrammarBase):
gramSpec = """
<go_task> exported = Go (1|2|3|4|5|6|7|8|9|10|11|12|13|14|15);
"""
def initialize(self):
self.load(self.gramSpec)
self.activateAll()
def gotResults_go_task(self,words,fullResults):
prog = os.path.join(NatLinkFolder,r'go_task.exe')
#print prog
os.spawnv(os.P_NOWAIT, prog, ('foo', words[1]))
thisGrammar = ThisGrammar()
thisGrammar.initialize()
def unload():
global thisGrammar
if thisGrammar: thisGrammar.unload()
thisGrammar = NoneHere is the Auto-it script:
;----------------------------------------
; Filename: go_task.au3
; JFournier 2009
;----------------------------------------
;
; This script generate a click at the window's position passed as a parameter.
;
;----------------------------------------
$winbardim = 158 ; should be same for all config
$screenyres = @DesktopHeight
$taskid=$CmdLine[1]
$var = WinList()
$wc = 0
For $i = 1 to $var[0][0]
; Only consider visible windows that have a title
If $var[$i][0] <> "" AND IsVisible($var[$i][1]) AND $var[$i][0] <> "Program Manager" Then
;MsgBox(0, "Details", "Title=" & $var[$i][0] & @LF & "Handle=" & $var[$i][1])
$wc = $wc + 1
EndIf
Next
Func IsVisible($handle)
If BitAnd( WinGetState($handle), 2 ) Then
Return 1
Else
Return 0
EndIf
EndFunc
;MsgBox(0, "Active Windows", $wc & " active windows")
; Get task bar dimensions
AutoItSetOption("WinTitleMatchMode", 4)
$bpos = ControlGetPos('classname=Shell_TrayWnd', '', "ToolbarWindow322")
$posx = 0
;account for an invisible window that appear with Dragon Naturally Speaking
;$wc = $wc-1
;when more than 8 windows bardim is less than 158
If $wc > 8 Then
$winbardim = Int( ( $bpos[2]-(($wc-1)*2) )/$wc )
Else
$winbardim = $winbardim
EndIf
$posx = $bpos[0] + Int(($taskid-1)*$winbardim + ($taskid-1)*2 + $winbardim/2)
$posy = $screenyres - $bpos[3] / 2
$posm = MouseGetPos()
MouseClick("left", $posx, $posy, 1, 0)
MouseMove($posm[0],$posm[1],0)Comments are welcome !
John

Works! Thank you.
Thank you for sharing this. It works, and is much better than having to guess the pixel location on the taskbar.