# [python-win32] how to open a file with associated application?

Tim Roberts timr at probo.com
Thu Mar 2 23:57:47 CET 2006

On Thu, 02 Mar 2006 16:13:18 -0500,  Michael Li <mli at deform.com> wrote:

>In Windows, there is a default way to open a file,
>like double click test.txt file, then Notepad will
>open the test.txt, double click test.doc, then
>MS-Word will open the test.doc.
>
>If I want to open a specific file, test.KEY,
>with my application, how do I do it ?
>The suffix ".KEY" is fixed.
>More specific, I have a file test.KEY in
>C:\DEFORM3D\PROBLEM\test.KEY
>my application is in C:\DEFORM3D\V6_0\DEF_GUI3.EXE,
>
>when double-click C:\DEFORM3D\PROBLEM\test.KEY,
>I want to open it like :
>C:\DEFORM3D\V6_0\DEF_GUI3.EXE -m pre -d C:\DEFORM3D\PROBLEM -i test -f
>test.KEY
>
>Can anyone point me a direction ?
>
>I believe that I have to write a python script to register
>something first. Is there any workable scripts I can borrow ?
>

You can do it from a command line like this:
assoc .key=MyDefGuiApplication
ftype MyDefGuiApplication=c:\deform3d\v6_0\def_gui3.exe -m pre -d
c:\deform3d\problem -i test -f %1

If you really need to do this from Python, you can also create the
registry keys by hand.

In this key:
HKEY_CLASSES_ROOT\.key
set the default value (the one with no name) to "MyDefGuiApplication".
Then, create this key:
HKEY_CLASSES_ROOT\MyDefGuiApplication\Shell\Open\Command
and set the default value to the command I showed above, of type
REG_EXPAND_SZ.

You can use whatever name you want for "MyDefGuiApplication".  It's just
the link between the extension and the command line.

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