7

I'd like to write Python scripts that drive Visual Studio 2008 and Visual C++ 2008. All the examples I've found so far use win32com.client.Dispatch. This works fine for Excel 2007 and Word 2007 but fails for Visual Studio 2008:

import win32com.client
app1 = win32com.client.Dispatch( 'Excel.Application' ) # ok
app2 = win32com.client.Dispatch( 'Word.Application' )  # ok
app3 = win32com.client.Dispatch( 'MSDev.Application' ) # error

Any ideas? Does Visual Studio 2008 use a different string to identify itself? Is the above method out-dated?

jwfearn
  • 28,781
  • 28
  • 95
  • 122

5 Answers5

3

Depending on what exactly you're trying to do, AutoIt may meet your needs. In fact, I'm sure it will do anything you need it to do.

Taken from my other post about how to use AutoIt with Python:

import win32com.client
oAutoItX = win32com.client.Dispatch( "AutoItX3.Control" )

oAutoItX.Opt("WinTitleMatchMode", 2) #Match text anywhere in a window title

width = oAutoItX.WinGetClientSizeWidth("Firefox")
height = oAutoItX.WinGetClientSizeHeight("Firefox")

print width, height

You can of course use any of the AutoItX functions (note that that link goes to the AutoIt function reference, the com version of AutoIt - AutoItX has a subset of that list...the documentation is included in the download) in this way. I don't know what you're wanting to do, so I can't point you towards the appropriate functions, but this should get you started.

Community
  • 1
  • 1
Dustin Wyatt
  • 4,046
  • 5
  • 31
  • 60
3

I don't know if this will help you with 2008, but with Visual Studio 2005 and win32com I'm able to do this:

>>> import win32com.client
>>> b = win32com.client.Dispatch('VisualStudio.DTE')
>>> b
<COMObject VisualStudio.DTE>
>>> b.name
u'Microsoft Visual Studio'
>>> b.Version
u'8.0'

Unfortunately I don't have 2008 to test with though.

ryan_s
  • 7,826
  • 3
  • 27
  • 28
3

ryan_s has the right answer. You might rethink using win32com.

I prefer the comtypes module to win32com. It fits in better with ctypes and python in general.

Using either approach with vs 2008 will work. Here is an example that prints the names and keyboard shortcuts for all the commands in Visual Studio.

import comtypes.client as client

vs = client.CreateObject('VisualStudio.DTE')

commands = [command for command in vs.Commands if bool(command.Name) or bool(command.Bindings)]
commands.sort(key=lambda cmd : cmd.Name)

f= open('bindings.csv','w')

for command in commands:
    f.write(command.Name+"," +";".join(command.Bindings)+ "\n")

f.close()
minty
  • 22,235
  • 40
  • 89
  • 106
2

You can try .Net's own version, IronPython. It has a VS addon, IronPythonStudio.

Being a .Net language, you can access all the available assemblies, including Visual Studio Tools for Office.

gimel
  • 83,368
  • 10
  • 76
  • 104
0

As of year 2013, better option can be to script Visual Studio via IronPython cause better CLR/COM and other MS stuff integration:


import clr
import System

t = System.Type.GetTypeFromProgID("AutoItX3.Control")
oAutoItX = System.Activator.CreateInstance(t)

oAutoItX.Opt("WinTitleMatchMode", 2)

width = oAutoItX.WinGetClientSizeWidth("IronPythonApplication1 - Microsoft Visual Studio (Administrator)")
height = oAutoItX.WinGetClientSizeHeight("IronPythonApplication1 - Microsoft Visual Studio (Administrator)")

print width, height
Dzmitry Lahoda
  • 939
  • 1
  • 13
  • 34