fileassoc.nsh 4.1 KB

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