FileAssociation.nsh 4.5 KB

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