FileAssociation.nsh 5.1 KB

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