[python-win32] Finding the selected file in Windows Explorer

Tim Roberts timr at probo.com
Tue Apr 22 19:19:31 CEST 2008


Daniel Gonçalves wrote:
> Hi!
>
> I need to find the selected file(s) in a Windows Explorer window from
> another program (I'd look at the window that last had focus). I found
> something in the following page that should do the trick:
>
> http://blogs.msdn.com/oldnewthing/archive/2004/07/20/188696.aspx
>
> However, it is not Python and, while I'm a competent Python
> programmer, Win32, COM and the like are somewhat outside my
> competences.

WOW, that's fairly amazing.  It takes 9 different COM interfaces to get 
this information.


> Does any one know how to do something similar in Python (maybe in a 
> more Pythonic way?)

Unfortunately, it's not a Pythonic task.  It's a Windows task.  I can 
give you some hints to get you started, but it's going to be a tedious 
task with lots of trial and error.

import pywintypes
import win32com.client
CLSID_ShellWindows = pywintypes.IID( '{9ba05972-f6a8-11cf-a442-00a0c90a8f39}' )
o = win32com.client.Dispatch( CLSID_ShellWindows )
for i in range(o.Count):
    print o.Item(0).Name
    

Now you have an IID_ShellWindows interface.  You can use o.Count to find 
out how many windows there are, and o.Item(i) will return you each 
window in the collection one at a time.  From there, you'll need to 
query the appropriate subinterfaces, and hope that they are all dispatch 
interfaces so they can be used from Python.

I might be tempted to take the C++ code and make a simple extension DLL.

Perhaps Mark or Tim Golden has some better hints.

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the python-win32 mailing list