FileAssociation.nsh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. Skip:
  100. WriteRegStr HKCR "$R0\shell\open\command" "" '"$R2" "%1"'
  101. WriteRegStr HKCR "$R0\shell\edit" "" "Edit $R0"
  102. WriteRegStr HKCR "$R0\shell\edit\command" "" '"$R2" "%1"'
  103. Pop $1
  104. Pop $0
  105. Pop $R2
  106. Pop $R1
  107. Pop $R0
  108. !verbose pop
  109. !macroend
  110. !define UnRegisterExtension `!insertmacro UnRegisterExtensionCall`
  111. !define un.UnRegisterExtension `!insertmacro UnRegisterExtensionCall`
  112. !macro UnRegisterExtension
  113. !macroend
  114. !macro un.UnRegisterExtension
  115. !macroend
  116. !macro UnRegisterExtension_
  117. !verbose push
  118. !verbose ${_FileAssociation_VERBOSE}
  119. Exch $R1 ;desc
  120. Exch
  121. Exch $R0 ;ext
  122. Exch
  123. Push $0
  124. Push $1
  125. ReadRegStr $1 HKCR $R0 ""
  126. StrCmp $1 $R1 0 NoOwn ; only do this if we own it
  127. ReadRegStr $1 HKCR $R0 "backup_val"
  128. StrCmp $1 "" 0 Restore ; if backup="" then delete the whole key
  129. DeleteRegKey HKCR $R0
  130. Goto NoOwn
  131. Restore:
  132. WriteRegStr HKCR $R0 "" $1
  133. DeleteRegValue HKCR $R0 "backup_val"
  134. DeleteRegKey HKCR $R1 ;Delete key with association name settings
  135. NoOwn:
  136. Pop $1
  137. Pop $0
  138. Pop $R1
  139. Pop $R0
  140. !verbose pop
  141. !macroend
  142. !endif # !FileAssociation_INCLUDED