I'm trying to get the contents of the Find Results window in a program
that's intended to run from the Tools menu. I've got as far as getting
the Find Window object, but I can't figure out how to get the actual
contents. If I get the selection (which is exposed), the program does
exactly what I want it to do. How can I get the contents of the window
regardless of selection?
The actual functionality takes the list of filenames given by Find In
Files and opens them for edit (i.e. "checks them out") in Perforce.
Here's the code in Python; it should be easy enough to follow what's
going on even if you don't know it:
import re
import p4
import win32com.client
FILENAME_REGEX = re.compile(r'^[a-zA-Z0-9 /\\:._]*(?=\(\d*\):)', re.M)
def GetLines():
devStudio = win32com.client.Dispatch('VisualStudio.DTE')
findWindow =
devStudio.Windows.Item(win32com.client.constants.vsWindowKindFindResults1)
selection = findWindow.Selection()
return selection
result of "Find in Files" into a list.
def BuildFileList(buffer):
files = set()
for match in FILENAME_REGEX.finditer(buffer):
start = match.start()
end = match.end()
files.add(buffer[start:end])
return files
if __name__ == '__main__':
p4c = p4.P4()
p4c.connect()
lines = GetLines()
if not lines:
print 'You must select the lines in the find window. Sorry.'
else:
files = BuildFileList(lines)
for file in files:
print 'edit', file
p4c.run_edit(str(file)) # the P4 api requires strings, but we get
unicode from DevStudio
print '\nDone.'
p4c.disconnect()
|