fileassoc.nsh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. ; fileassoc.nsh
  2. ; File association helper macros
  3. ; Written by Saivert
  4. ;
  5. ; Improved by Nikku<https://github.com/nikku>.
  6. ;
  7. ; Features automatic backup system and UPDATEFILEASSOC macro for
  8. ; shell change notification.
  9. ;
  10. ; |> How to use <|
  11. ; To associate a file with an application so you can double-click it in explorer, use
  12. ; the APP_ASSOCIATE macro like this:
  13. ;
  14. ; Example:
  15. ; !insertmacro APP_ASSOCIATE "txt" "myapp.textfile" "Description of txt files" \
  16. ; "$INSTDIR\myapp.exe,0" "Open with myapp" "$INSTDIR\myapp.exe $\"%1$\""
  17. ;
  18. ; Never insert the APP_ASSOCIATE macro multiple times, it is only ment
  19. ; to associate an application with a single file and using the
  20. ; the "open" verb as default. To add more verbs (actions) to a file
  21. ; use the APP_ASSOCIATE_ADDVERB macro.
  22. ;
  23. ; Example:
  24. ; !insertmacro APP_ASSOCIATE_ADDVERB "myapp.textfile" "edit" "Edit with myapp" \
  25. ; "$INSTDIR\myapp.exe /edit $\"%1$\""
  26. ;
  27. ; To have access to more options when registering the file association use the
  28. ; APP_ASSOCIATE_EX macro. Here you can specify the verb and what verb is to be the
  29. ; standard action (default verb).
  30. ;
  31. ; Note, that this script takes into account user versus global installs.
  32. ; To properly work you must initialize the SHELL_CONTEXT variable via SetShellVarContext.
  33. ;
  34. ; And finally: To remove the association from the registry use the APP_UNASSOCIATE
  35. ; macro. Here is another example just to wrap it up:
  36. ; !insertmacro APP_UNASSOCIATE "txt" "myapp.textfile"
  37. ;
  38. ; |> Note <|
  39. ; When defining your file class string always use the short form of your application title
  40. ; then a period (dot) and the type of file. This keeps the file class sort of unique.
  41. ; Examples:
  42. ; Winamp.Playlist
  43. ; NSIS.Script
  44. ; Photoshop.JPEGFile
  45. ;
  46. ; |> Tech info <|
  47. ; The registry key layout for a global file association is:
  48. ;
  49. ; HKEY_LOCAL_MACHINE\Software\Classes
  50. ; <".ext"> = <applicationID>
  51. ; <applicationID> = <"description">
  52. ; shell
  53. ; <verb> = <"menu-item text">
  54. ; command = <"command string">
  55. ;
  56. ;
  57. ; The registry key layout for a per-user file association is:
  58. ;
  59. ; HKEY_CURRENT_USER\Software\Classes
  60. ; <".ext"> = <applicationID>
  61. ; <applicationID> = <"description">
  62. ; shell
  63. ; <verb> = <"menu-item text">
  64. ; command = <"command string">
  65. ;
  66. !macro APP_ASSOCIATE EXT FILECLASS DESCRIPTION ICON COMMANDTEXT COMMAND
  67. ; Backup the previously associated file class
  68. ReadRegStr $R0 SHELL_CONTEXT "Software\Classes\.${EXT}" ""
  69. WriteRegStr SHELL_CONTEXT "Software\Classes\.${EXT}" "${FILECLASS}_backup" "$R0"
  70. WriteRegStr SHELL_CONTEXT "Software\Classes\.${EXT}" "" "${FILECLASS}"
  71. WriteRegStr SHELL_CONTEXT "Software\Classes\${FILECLASS}" "" `${DESCRIPTION}`
  72. WriteRegStr SHELL_CONTEXT "Software\Classes\${FILECLASS}\DefaultIcon" "" `${ICON}`
  73. WriteRegStr SHELL_CONTEXT "Software\Classes\${FILECLASS}\shell" "" "open"
  74. WriteRegStr SHELL_CONTEXT "Software\Classes\${FILECLASS}\shell\open" "" `${COMMANDTEXT}`
  75. WriteRegStr SHELL_CONTEXT "Software\Classes\${FILECLASS}\shell\open\command" "" `${COMMAND}`
  76. !macroend
  77. !macro APP_ASSOCIATE_EX EXT FILECLASS DESCRIPTION ICON VERB DEFAULTVERB SHELLNEW COMMANDTEXT COMMAND
  78. ; Backup the previously associated file class
  79. ReadRegStr $R0 SHELL_CONTEXT "Software\Classes\.${EXT}" ""
  80. WriteRegStr SHELL_CONTEXT "Software\Classes\.${EXT}" "${FILECLASS}_backup" "$R0"
  81. WriteRegStr SHELL_CONTEXT "Software\Classes\.${EXT}" "" "${FILECLASS}"
  82. StrCmp "${SHELLNEW}" "0" +2
  83. WriteRegStr SHELL_CONTEXT "Software\Classes\.${EXT}\ShellNew" "NullFile" ""
  84. WriteRegStr SHELL_CONTEXT "Software\Classes\${FILECLASS}" "" `${DESCRIPTION}`
  85. WriteRegStr SHELL_CONTEXT "Software\Classes\${FILECLASS}\DefaultIcon" "" `${ICON}`
  86. WriteRegStr SHELL_CONTEXT "Software\Classes\${FILECLASS}\shell" "" `${DEFAULTVERB}`
  87. WriteRegStr SHELL_CONTEXT "Software\Classes\${FILECLASS}\shell\${VERB}" "" `${COMMANDTEXT}`
  88. WriteRegStr SHELL_CONTEXT "Software\Classes\${FILECLASS}\shell\${VERB}\command" "" `${COMMAND}`
  89. !macroend
  90. !macro APP_ASSOCIATE_ADDVERB FILECLASS VERB COMMANDTEXT COMMAND
  91. WriteRegStr SHELL_CONTEXT "Software\Classes\${FILECLASS}\shell\${VERB}" "" `${COMMANDTEXT}`
  92. WriteRegStr SHELL_CONTEXT "Software\Classes\${FILECLASS}\shell\${VERB}\command" "" `${COMMAND}`
  93. !macroend
  94. !macro APP_ASSOCIATE_REMOVEVERB FILECLASS VERB
  95. DeleteRegKey SHELL_CONTEXT `Software\Classes\${FILECLASS}\shell\${VERB}`
  96. !macroend
  97. !macro APP_UNASSOCIATE EXT FILECLASS
  98. ; Backup the previously associated file class
  99. ReadRegStr $R0 SHELL_CONTEXT "Software\Classes\.${EXT}" `${FILECLASS}_backup`
  100. WriteRegStr SHELL_CONTEXT "Software\Classes\.${EXT}" "" "$R0"
  101. DeleteRegKey SHELL_CONTEXT `Software\Classes\${FILECLASS}`
  102. !macroend
  103. !macro APP_ASSOCIATE_GETFILECLASS OUTPUT EXT
  104. ReadRegStr ${OUTPUT} SHELL_CONTEXT "Software\Classes\.${EXT}" ""
  105. !macroend
  106. ; !defines for use with SHChangeNotify
  107. !ifdef SHCNE_ASSOCCHANGED
  108. !undef SHCNE_ASSOCCHANGED
  109. !endif
  110. !define SHCNE_ASSOCCHANGED 0x08000000
  111. !ifdef SHCNF_FLUSH
  112. !undef SHCNF_FLUSH
  113. !endif
  114. !define SHCNF_FLUSH 0x1000
  115. !macro UPDATEFILEASSOC
  116. ; Using the system.dll plugin to call the SHChangeNotify Win32 API function so we
  117. ; can update the shell.
  118. System::Call "shell32::SHChangeNotify(i,i,i,i) (${SHCNE_ASSOCCHANGED}, ${SHCNF_FLUSH}, 0, 0)"
  119. !macroend