Registering Windows file types with NSIS
8 October 2006 (programming windows) (9 comments)While working on a Windows installer for Guikachu, I found the need to register .guikachu files, so that they can be opened by double-clicking on them. Read on for instructions (with sample NSIS script snippets) on how to create new file types on Windows.
First of all, Windows stores file type information separately from the extension->filetype mapping, which is a good thing – this way, you can register several file extensions to mean the same file type. File extensions are registered by creating a key in HKEY_CLASSES_ROOT with the extension (including the leading dot), and setting the default field to the file type.
WriteRegStr HKCR ".mtxt" "" "MyTextEditor.Document"
Like noted above, you can register any number of file extensions for a given file type. Now that in our example, .mtxt is registered, it is time to define the MyTextEditor.Document file type.
File type definitions go into the same HKEY_CLASSES_ROOT hierarchy as file extensions. File types contain information for the shell (i.e. Windows Explorer) and definitions of document operations. So let's first create our file type and give it a nice user-visible name, and set up its icon as well:
WriteRegStr HKCR "MyTextEditor.Document" "" \
"MyText Document"
WriteRegStr HKCR "MyTextEditor.Document\DefaultIcon" "" \
"$INSTDIR\mytextedit.exe,1"
The ,1 above means to use the second icon from the mytextedit executable. For this purpose, icons are ordered by their resource ID's, so even if you have two icons with ID's 100 and 105, their index will be 0 and 1 (and not 100 and 105), respectively. Note how the NSIS variable $INSTDIR is used to point to the right location of the executable.
OK now for the actions. Each action has an ID, an optional user-visible name, and an associated command. Actions are created as keys under HKEY_CLASSES_ROOT/FileType/shell. The user-visible name of the action is stored in the default field of the key, and the command sub-key stores the command to run (with %1 substituted with the file name). The default command is open, or whatever you set the default field of shell to. So here's an example defining two actions:
WriteRegStr HKCR "MyTextEditor.Document\shell\open\command" "" \
'"$INSTDIR\mytextedit.exe" "%1"'
WriteRegStr HKCR "MyTextEditor.Document\shell\print\command" "" \
'"$INSTDIR\mytextedit.exe" /p "%1"'
Whenever applicable, try to use the standard actions without specifying a user-visible command name. This way, Windows can display actions in menus in whatever language the user has set up.
As a final note, yes, there are more sophisticated ways to implement actions, by using DDE or COM messages. However, if you're creating a straight Windows port of some Unix software, like I was doing, the above should be enough to register your file types with Windows.
sitesupport (http://www.robosoft.info) 2007-01-15 15:12:27
Nice article!
But extend not all of features of NSIS. I'm looking for description how to set icon for registered file type with NSIS. Could somebody help me?
cactus 2007-01-15 16:17:54
That's exactly what the DefaultIcon key is for. See the second line of the second code snippet.
THRESHE 2008-02-13 07:35:57
Thanks a nice article :)
Mark McLaren (http://www.solaraccounts.co.uk) 2008-03-26 16:50:13
Great article - exactly what I was looking for. Here's a few things to watch out for too:
1. After registering your file type, you should add these lines to update Windows:
!insertmacro RefreshShellIcons
Call RefreshShellIcons
(Otherwise the file icons will not be upadated until the system is rebooted.)
2. Don't forget to unregister file types on uninstall
3. If your installing a Java program: When the user double-clicks on a file to open it, the user.dir the directory of that file, not the directory of the executable.
Victor Conesa (http://www.justinmind.com) 2008-06-19 16:48:02
Good article!
I've noticed, like Mark McLaren that when the user doble-clicks the file to open it the working directory points to the place where the file is instead where the program is. Is there a way to make windows to point to the place where the program is? I would really appreciate any help with this...
erazer 2008-09-05 12:49:09
very helpful post mate!
thx!
rc helicopter (http://www.lalarola.com/fr-loisir-et-d%C3%A9tente-sb0-pi1-c63.htm) 2011-08-23 03:23:41
Avions d'intérieur contrôlés à distance
L'une des plaintes que vous avez l’habitude d'entendre est que vous besoin d'un endroit pour faire fonctionner ces avions. Avec les avions RC d'intérieur, ce n'est plus le cas. Les avions contrôlés à distance ont toujours été des modèles réduits des versions originales en taille réelle. L'échelle a varié, mais il a toujours existé un facteur limitant, quelle doit être la petitesse en réalité. L'avion doit être capable de porter le moteur, le récepteur, et dans le cas des avions électriques, des batteries. Les avions résultant avaient besoin d'un espace assez grand pour décoller et atterrir. Plus d’espace était nécessaire encore pour le vol. Cela séparait les avions des autres modèles télécommandés. Les avions avaient besoin de voler à l'extérieur.
Les hélicoptères RC étaient une réponse à ce problème. L'hélicoptère avait besoin de moins d’espace de décollage et d'atterrissage, et était capable de voler dans un espace beaucoup plus restreint. Il était possible de faire voler les hélicoptères RC dans un grand bâtiment comme un atelier ou un gymnase. Pourtant, les hélicoptères, en dépit du plaisir, n'étaient pas des avions. C’était la possibilité de produire des composants miniatures qui inaugure l'ère de l'avion RC d'intérieur. Le récepteur était un problème majeur, mais l'avènement de microcircuits permit des récepteurs petits et légers. Il est désormais possible de piloter des avions RC à l'intérieur des bâtiments ainsi que les hélicoptères. Les clubs de vol d'intérieur ont commencé à ouvrir et rapidement gagné en popularité. N'importe quel bâtiment avec un espace ouvert était désormais une zone de vol possible.
Les avions électriques sont la nouvelle tendance pour les débutants. Avec la nouveauté de l'électricité pour alimenter, les moteurs sont devenus plus petits avec envergure et la masse et le poids de l'avion. Ces merveilles ailées peuvent naviguer à travers de grandes chambres familiales et les plafonds de haute cathédrale. Certains sont même assez petits pour supporter les petits salons et chambres à coucher. Plus un avion RC est lourd et rapide, le plus de dégâts il est susceptible de causer. Habituellement, le constructeur d'avions RC affiche sur l'emballage de l'avion s’il est destiné ou non à un usage intérieur.
If you want to get some more information about Modélisme RC then Please visit my site.
wedding veil (http://www.beauty511.com) 2011-08-23 08:18:57
the place where the file is instead where the program is. Is there a way to make windows to point to the place where the program is? I would really appreciate any help with this...Denver Broncos jerseys
Mikey (http://www.dyspraxia.info) 2011-08-23 09:41:43
Nice tip about using standard actions - that really makes sense. dyspraxia