how to associate files with application

Lasse Vågsæther Karlsen lasse at vkarlsen.no
Fri Oct 28 10:51:40 CEST 2005


Ashok wrote:
> hi,
> i want to know how to make a specific type of file open in an
> application i developed in python when the user clicks on the file.(in
> windows) for eg. a .txt file when clicked opens in notepad, a .doc file
> when clicked opens in MS-word. In the same way i want to make a .xyz
> file open in the application i developed when clicked.
> thanks in advance for any advice.
> 

You need to add several registry keys to do this, here's a short version 
of what you need to do:

Example assumes you want to:

1. associate .ext with C:\Program Files\MyProgram\prog.exe
2. pass on any extra arguments to prog.exe (ie. test.ext 1 2 3 would 
send 1 2 3 as well to prog.exe)
3. associate the icon of prog.exe to any file with a .ext extension

Ok, here's what you need to do:

1. Under HKEY_CLASSES_ROOT, add a key (folder) with the name .ext
2. Open that key, and set the (Default) value to MyProgramExtendedFile 
(this name is something you choose yourself and should be a "identifier" 
that identifies the file type. If your program supports several types of 
files, make up unique identifiers for each.)
3. Under HKEY_CLASSES_ROOT, add another key, this time with the same 
name you made up in 2. above, ie MyProgramExtendedFile
4. Open that key, and set the (Default) value to a textual description 
of the type of file. This is what will show up in explorer in the file 
type column. If you leave this empty, the description will be .EXT File
5. Inside MyProgramExtendedFile, add another key with the name shell 
(lower-case is typical, can probably be Shell or whatever)
6. Inside shell, create another key with the name open
7. Inside open, create another key with the name command
8. Inside command, Set the (Default) value to:
    "C:\Program Files\MyProgram\prog.exe" "%1" %*

    Note that you need the quotes as specified above, exactly like written

9. Go back to MyProgramExtendedFile and create another key with the name 
DefaultIcon
10. Inside DefaultIcon, set (Default) value to:
     "C:\Program Files\MyProgram\prog.exe", 0

     This will pick the first icon in prog.exe resource to show for the 
files. Use 1 for second, etc.

There are also other commands you can add. If you want to be able to 
right-click on the file and select a menu item to process the file in a 
specific way, for instance by passing along specific parameters to 
prog.exe, you can add more keys than "open" on the level open is 
created. The (Default) value inside the key is then the text of the menu 
item.

To find examples, just find a file extension in Windows that behaves the 
way you want your own to behave and look through HKEY_CLASSES_ROOT\.ext 
to find the details you want.

-- 
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:lasse at vkarlsen.no
PGP KeyID: 0x2A42A1C2



More information about the Python-list mailing list