views:

613

answers:

2

I am trying to create a simple open source utility for windows using Python that can perform user-defined actions on the selected text of the currently active window. The utility should be activated using a pre-defined keyboard shortcut.

Usage is partially outlined in the following example:

  1. The user selects some text using the mouse or the keyboard (in any application window)
  2. The user presses a pre-defined keyboard shortcut
  3. The selected text is retrieved by our utility or copied to clipboard (both approaches should be fine)
  4. The keyboard shortcut-dependent action is performed on the selected text

What puzzles me is step 3. How the selected text is retrieved from the active window. This should work with all applications.

I use the pywin32 module.

Thanks in advance for your answers and tips.

Update #1:

Turns out that there are two approaches to accomplish the task:

  1. Find the active window, then send a message/keystroke (Ctrl-C) to it in order to copy the selected text to the clipboard. Then the utility can work on the text by accessing it using the clipboard-related functions.
  2. Find the active Window, then retrieve the selected text directly (without copying it to clipboard). This seems more difficult than the 1st approach.

As starting points:

Get the active window ID as Anurag Uniyal has pointed out in his reply.

Or get the window object with the following code:

import win32ui
wnd = win32ui.GetForegroundWindow()
print wnd.GetWindowText()
+1  A: 

It won't be trivial but here is the starting point

import win32gui
hwnd = win32gui.GetForegroundWindow()
print win32gui.GetWindowText(hwnd)

Maybe you will have to use FindWindow,FindWindowEx to get child windows with focus

edit: also while experimenting use spy++ to see how it retrieves information about various windows, see hwnd, window class etc

basically if you can find a example in C/C++/C# it won't be difficult to translate that into pywin32 equivalent, so in a way it is win32 api specific question

Anurag Uniyal
I've been getting the active window object with the following: import win32ui wnd = win32ui.GetForegroundWindow() print wnd.GetWindowText()But I have difficulties in getting to the selected text from there. Thanks for the tips. I will look into spy++.
George Notaras
+1  A: 

You're far better off using the Ctrl-C method. Fetching text directly will work for something like an edit control, but is useless for retrieving text that an application has painted directly on its own window.

Bob Moore
Thanks. This assumes that all applications, which support text selection, make use of the clipboard. This is probably true for 99.99% of the applications, but do you happen to know if this is the rule?
George Notaras
Keep in mind there are a few applications that do support clipboard copy of text, but assign a different meaning to Ctrl+C than "clipboard copy." One example that comes to mind is the command shell (cmd.exe).
Jon Schneider