开发者

Python programming. Accessing Windows right click menu options

开发者 https://www.devze.com 2023-01-31 13:39 出处:网络
I\'m hoping to automate a few tasks at work. One of them being combining and converting power point files to PDFs. I\'m a bit of a newbie (I just finished Magus Heitland\'s Beginning Python), so I\'m

I'm hoping to automate a few tasks at work. One of them being combining and converting power point files to PDFs. I'm a bit of a newbie (I just finished Magus Heitland's Beginning Python), so I'm not entirely sure what I'm specifically asking.

On windows, one can select multiple files, right click, and select combine as adobe PDF. I've figured out the 'grouping' of the files I want to convert (I traverse the dir and nest the files inside of a list based on their names), but I'm 开发者_运维问答unsure how to pursue the next step (the rightclick/combine command).

Googling has led me to things like win32api, pywinauto, and ctypes. But as I read over what they do my newbieness prevents me from knowing which is the tool I need.

Could any one suggest a few good resources or tips?


There is no easy answer to this question. In Windows, context menu entries are created by context menu shell extensions, which is a type of shell extension, which is a COM component registered with Windows Explorer. To invoke an arbitrary context menu entry on a group of files, following operations have to be performed:

  1. Obtain an IShellFolder interface of the shell folder containing the files.
  2. Use IShellFolder::GetUIObjectOf to obtain context menu handler object of that shell folder. Pass it an ITEMIDLIST filled with list of selected files and riid = IContextMenu.
  3. Call IContextMenu::InvokeCommand to execute the desired command on the group of files.

All this can be implemented in Python (via ctypes or win32com), but I think it's better to start with a language that supports COM directly (such as C++) and translate to Python once you get it working... or use a different approach.


EDIT If you have total control of the execution environment, it may be good enough to simulate user input (keyboard and mouse). It is much easier to do in Python. You will need just one system function: SendInput. Quick googling revealed an example of using SendInput via ctypes here.


You want to look up exactly what command line equivalent is run when you click on Combine, let's say it was c:\program files\adobe\combiner .... You want to build this string (with a list of files as the ...) and execute it. Popen in the subprocess module will do that.

import subprocess
cmd = [r"c:\program files\adobe\combiner"]
cmd.extend(file_list)
p = subprocess.Popen(cmd)

Check out that link though, Popen takes lots of arguments, and this is just the simplest form. Don't know if the arguments have to be in a list, but they are in the examples I've seen.


If you are just looking for a simple solution, you can customize a a "Send To..." right click shortcut that will invoke your python script with the command line arguments of the files to convert.

To add a Send To shortcut, it depends on if you are using Windows 7 or a 32 bit prior version.There are instructions HERE to add a custom Send To... shortcut. You could send all the files to a single folder and invoke your python script or invoke it with the selected files as command line arguments.

Send To Toys works in 64 bit. With Send To Toys you could have the your python script and all the shell escaped file names placed on the clipboard when you right click, then paste the constructed command into the shell. I use this method a lot because it only takes seconds to construct.

You can also add a right click shortcut by tweeking the registry to invoke a shell command (your python script) with the selected files as targets. You can either manually tweek the registry or use something like RightClick Extender.

A bit more work, you can write a custom ContextMenu Class form and respond to the form in a .NET language. This would include C# or Ironpython.

If you really want a Pro kind of implementation, you could modify the source code of the new open source version of Image Resizer. The c++ source the context menu handler is here. But these last two suggestions seem like way more work than needed...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号