1/*
2_____________________________________________________________________________
3
4                       File Association
5_____________________________________________________________________________
6
7 Based on code taken from http://nsis.sourceforge.net/File_Association
8
9 Usage in script:
10 1. !include "FileAssociation.nsh"
11 2. [Section|Function]
12      ${FileAssociationFunction} "Param1" "Param2" "..." $var
13    [SectionEnd|FunctionEnd]
14
15 FileAssociationFunction=[RegisterExtension|UnRegisterExtension]
16
17_____________________________________________________________________________
18
19 ${RegisterExtension} "[executable]" "[extension]" "[description]"
20
21"[executable]"     ; executable which opens the file format
22                   ;
23"[extension]"      ; extension, which represents the file format to open
24                   ;
25"[description]"    ; description for the extension. This will be display in Windows Explorer.
26                   ;
27
28
29 ${UnRegisterExtension} "[extension]" "[description]"
30
31"[extension]"      ; extension, which represents the file format to open
32                   ;
33"[description]"    ; description for the extension. This will be display in Windows Explorer.
34                   ;
35
36_____________________________________________________________________________
37
38                         Macros
39_____________________________________________________________________________
40
41 Change log window verbosity (default: 3=no script)
42
43 Example:
44 !include "FileAssociation.nsh"
45 !insertmacro RegisterExtension
46 ${FileAssociation_VERBOSE} 4   # all verbosity
47 !insertmacro UnRegisterExtension
48 ${FileAssociation_VERBOSE} 3   # no script
49*/
50
51
52!ifndef FileAssociation_INCLUDED
53!define FileAssociation_INCLUDED
54
55!include Util.nsh
56
57!verbose push
58!verbose 3
59!ifndef _FileAssociation_VERBOSE
60  !define _FileAssociation_VERBOSE 3
61!endif
62!verbose ${_FileAssociation_VERBOSE}
63!define FileAssociation_VERBOSE `!insertmacro FileAssociation_VERBOSE`
64!verbose pop
65
66!macro FileAssociation_VERBOSE _VERBOSE
67  !verbose push
68  !verbose 3
69  !undef _FileAssociation_VERBOSE
70  !define _FileAssociation_VERBOSE ${_VERBOSE}
71  !verbose pop
72!macroend
73
74
75
76!macro RegisterExtensionCall _EXECUTABLE _EXTENSION _DESCRIPTION
77  !verbose push
78  !verbose ${_FileAssociation_VERBOSE}
79  Push `${_DESCRIPTION}`
80  Push `${_EXTENSION}`
81  Push `${_EXECUTABLE}`
82  ${CallArtificialFunction} RegisterExtension_
83  !verbose pop
84!macroend
85
86!macro UnRegisterExtensionCall _EXTENSION _DESCRIPTION
87  !verbose push
88  !verbose ${_FileAssociation_VERBOSE}
89  Push `${_EXTENSION}`
90  Push `${_DESCRIPTION}`
91  ${CallArtificialFunction} UnRegisterExtension_
92  !verbose pop
93!macroend
94
95
96
97!define RegisterExtension `!insertmacro RegisterExtensionCall`
98!define un.RegisterExtension `!insertmacro RegisterExtensionCall`
99
100!macro RegisterExtension
101!macroend
102
103!macro un.RegisterExtension
104!macroend
105
106!macro RegisterExtension_
107  !verbose push
108  !verbose ${_FileAssociation_VERBOSE}
109
110  Exch $R2 ;exe
111  Exch
112  Exch $R1 ;ext
113  Exch
114  Exch 2
115  Exch $R0 ;desc
116  Exch 2
117  Push $0
118  Push $1
119
120  ReadRegStr $1 HKCR $R1 ""  ; read current file association
121  StrCmp "$1" "" NoBackup  ; is it empty
122  StrCmp "$1" "$R0" NoBackup  ; is it our own
123    WriteRegStr HKCR $R1 "backup_val" "$1"  ; backup current value
124NoBackup:
125  WriteRegStr HKCR $R1 "" "$R0"  ; set our file association
126
127  ReadRegStr $0 HKCR $R0 ""
128  StrCmp $0 "" 0 Skip
129    WriteRegStr HKCR "$R0" "" "$R0"
130    WriteRegStr HKCR "$R0\shell" "" "open"
131    WriteRegStr HKCR "$R0\DefaultIcon" "" "$R2,0"
132Skip:
133  WriteRegStr HKCR "$R0\shell\open\command" "" '"$R2" "%1"'
134  WriteRegStr HKCR "$R0\shell\edit" "" "Edit $R0"
135  WriteRegStr HKCR "$R0\shell\edit\command" "" '"$R2" "%1"'
136
137  Pop $1
138  Pop $0
139  Pop $R2
140  Pop $R1
141  Pop $R0
142
143  !verbose pop
144!macroend
145
146
147
148!define UnRegisterExtension `!insertmacro UnRegisterExtensionCall`
149!define un.UnRegisterExtension `!insertmacro UnRegisterExtensionCall`
150
151!macro UnRegisterExtension
152!macroend
153
154!macro un.UnRegisterExtension
155!macroend
156
157!macro UnRegisterExtension_
158  !verbose push
159  !verbose ${_FileAssociation_VERBOSE}
160
161  Exch $R1 ;desc
162  Exch
163  Exch $R0 ;ext
164  Exch
165  Push $0
166  Push $1
167
168  ReadRegStr $1 HKCR $R0 ""
169  StrCmp $1 $R1 0 NoOwn ; only do this if we own it
170  ReadRegStr $1 HKCR $R0 "backup_val"
171  StrCmp $1 "" 0 Restore ; if backup="" then delete the whole key
172  DeleteRegKey HKCR $R0
173  Goto NoOwn
174
175Restore:
176  WriteRegStr HKCR $R0 "" $1
177  DeleteRegValue HKCR $R0 "backup_val"
178  DeleteRegKey HKCR $R1 ;Delete key with association name settings
179
180NoOwn:
181
182  Pop $1
183  Pop $0
184  Pop $R1
185  Pop $R0
186
187  !verbose pop
188!macroend
189
190!endif # !FileAssociation_INCLUDED
191
192