File Association: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
(Replaced the register/unregister file extension macro/function)
m (cannot have spaces in the last paramater)
 
(9 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{PageAuthor|Vytautas}}
{{PageAuthor|Vytautas}}
{{PageAuthor|intersol}}
{{PageAuthor|intersol}}
{{PageAuthor|chefkoch}}


== Description ==
== Description ==
Macro for registering and unregistering file extensions in NSIS scripts.  
Macro for registering and unregistering file extensions in NSIS scripts.
 
Can easily be used in installer AND uninstaller sections and functions.
Won't overwrite any variables like $0 $1 ..... and $R0 $R1....... .


== Usage ==
== Usage ==
<highlight-nsis>
<highlight-nsis>
!include "registerExtension.nsh"
!include "FileAssociation.nsh"
...
...
# later, inside a section:
# later, inside a section:
${registerExtension} "c:\myplayer.exe" ".mkv" "MKV File"
${registerExtension} "c:\myplayer.exe" ".mkv" "MKV_File"


${unregisterExtension} ".mkv" "MKV File"
${unregisterExtension} ".mkv" "MKV File"
</highlight-nsis>
== FileAssociation.nsh ==
<highlight-nsis>
/*
_____________________________________________________________________________
                      File Association
_____________________________________________________________________________
Based on code taken from http://nsis.sourceforge.net/File_Association
Usage in script:
1. !include "FileAssociation.nsh"
2. [Section|Function]
      ${FileAssociationFunction} "Param1" "Param2" "..." $var
    [SectionEnd|FunctionEnd]
FileAssociationFunction=[RegisterExtension|UnRegisterExtension]
_____________________________________________________________________________
${RegisterExtension} "[executable]" "[extension]" "[description]"
"[executable]"    ; executable which opens the file format
                  ;
"[extension]"      ; extension, which represents the file format to open
                  ;
"[description]"    ; description for the extension. This will be display in Windows Explorer.
                  ;
${UnRegisterExtension} "[extension]" "[description]"
"[extension]"      ; extension, which represents the file format to open
                  ;
"[description]"    ; description for the extension. This will be display in Windows Explorer.
                  ;
_____________________________________________________________________________
                        Macros
_____________________________________________________________________________
Change log window verbosity (default: 3=no script)
Example:
!include "FileAssociation.nsh"
!insertmacro RegisterExtension
${FileAssociation_VERBOSE} 4  # all verbosity
!insertmacro UnRegisterExtension
${FileAssociation_VERBOSE} 3  # no script
*/
!ifndef FileAssociation_INCLUDED
!define FileAssociation_INCLUDED
!include Util.nsh
!verbose push
!verbose 3
!ifndef _FileAssociation_VERBOSE
  !define _FileAssociation_VERBOSE 3
!endif
!verbose ${_FileAssociation_VERBOSE}
!define FileAssociation_VERBOSE `!insertmacro FileAssociation_VERBOSE`
!verbose pop
!macro FileAssociation_VERBOSE _VERBOSE
  !verbose push
  !verbose 3
  !undef _FileAssociation_VERBOSE
  !define _FileAssociation_VERBOSE ${_VERBOSE}
  !verbose pop
!macroend
!macro RegisterExtensionCall _EXECUTABLE _EXTENSION _DESCRIPTION
  !verbose push
  !verbose ${_FileAssociation_VERBOSE}
  Push `${_DESCRIPTION}`
  Push `${_EXTENSION}`
  Push `${_EXECUTABLE}`
  ${CallArtificialFunction} RegisterExtension_
  !verbose pop
!macroend
!macro UnRegisterExtensionCall _EXTENSION _DESCRIPTION
  !verbose push
  !verbose ${_FileAssociation_VERBOSE}
  Push `${_EXTENSION}`
  Push `${_DESCRIPTION}`
  ${CallArtificialFunction} UnRegisterExtension_
  !verbose pop
!macroend


</highlight-nsis>


{| border=1 cellspacing=0
|-
! Token
! Description
|-
| ".opt"
| Is the extention of your choice
|-
| "OptionsFile"
| Is the general description of the file. Make sure NOT TO USE GENERIC DESCRIPTIONS or descriptions IN USE BY OTHER APPLICATIONS. Use a format like ApplicationName.FileType. If the application name is too generic add for example the company name.
|-
| "Program Options File"
| Is the windows description of the file
|-
| "open" & "Edit Options File"
| Are descriptions of the action performed by the following commands
|}


!define RegisterExtension `!insertmacro RegisterExtensionCall`
!define un.RegisterExtension `!insertmacro RegisterExtensionCall`


== registerExtension.nsh ==
!macro RegisterExtension
<highlight-nsis>
!macroend
!define registerExtension "!insertmacro registerExtension"
!define unregisterExtension "!insertmacro unregisterExtension"


!macro registerExtension executable extension description
!macro un.RegisterExtension
      Push "${executable}"  ; "full path to my.exe"
      Push "${extension}"  ;  ".mkv"
      Push "${description}" ;  "MKV File"
      Call registerExtension
!macroend
!macroend


; back up old value of .opt
!macro RegisterExtension_
Function registerExtension
  !verbose push
!define Index "Line${__LINE__}"
  !verbose ${_FileAssociation_VERBOSE}
   pop $R0 ; ext name
 
   pop $R1
   Exch $R2 ;exe
   pop $R2
  Exch
   push $1
   Exch $R1 ;ext
   push $0
  Exch
   ReadRegStr $1 HKCR $R1 ""
  Exch 2
   StrCmp $1 "" "${Index}-NoBackup"
   Exch $R0 ;desc
    StrCmp $1 "OptionsFile" "${Index}-NoBackup"
  Exch 2
     WriteRegStr HKCR $R1 "backup_val" $1
   Push $0
"${Index}-NoBackup:"
   Push $1
   WriteRegStr HKCR $R1 "" $R0
 
   ReadRegStr $1 HKCR $R1 "" ; read current file association
   StrCmp "$1" "" NoBackup ; is it empty
  StrCmp "$1" "$R0" NoBackup ; is it our own
     WriteRegStr HKCR $R1 "backup_val" "$1"  ; backup current value
NoBackup:
   WriteRegStr HKCR $R1 "" "$R0"  ; set our file association
 
   ReadRegStr $0 HKCR $R0 ""
   ReadRegStr $0 HKCR $R0 ""
   StrCmp $0 "" 0 "${Index}-Skip"
   StrCmp $0 "" 0 Skip
WriteRegStr HKCR $R0 "" $R0
    WriteRegStr HKCR "$R0" "" "$R0"
WriteRegStr HKCR "$R0\shell" "" "open"
    WriteRegStr HKCR "$R0\shell" "" "open"
WriteRegStr HKCR "$R0\DefaultIcon" "" "$R2,0"
    WriteRegStr HKCR "$R0\DefaultIcon" "" "$R2,0"
"${Index}-Skip:"
Skip:
   WriteRegStr HKCR "$R0\shell\open\command" "" '$R2 "%1"'
   WriteRegStr HKCR "$R0\shell\open\command" "" '"$R2" "%1"'
   WriteRegStr HKCR "$R0\shell\edit" "" "Edit $R0"
   WriteRegStr HKCR "$R0\shell\edit" "" "Edit $R0"
   WriteRegStr HKCR "$R0\shell\edit\command" "" '$R2 "%1"'
   WriteRegStr HKCR "$R0\shell\edit\command" "" '"$R2" "%1"'
   pop $0
 
   pop $1
   Pop $1
!undef Index
  Pop $0
FunctionEnd
   Pop $R2
  Pop $R1
  Pop $R0
 
  !verbose pop
!macroend
 
 
 
!define UnRegisterExtension `!insertmacro UnRegisterExtensionCall`
!define un.UnRegisterExtension `!insertmacro UnRegisterExtensionCall`
 
!macro UnRegisterExtension
!macroend


!macro unregisterExtension extension description
!macro un.UnRegisterExtension
      Push ${extension}  ;  ".mkv"
      Push ${description}  ;  "MKV File"
      Call unregisterExtension
!macroend
!macroend


Function unregisterExtension
!macro UnRegisterExtension_
   pop $R1 ; description
  !verbose push
   pop $R0 ; extension
  !verbose ${_FileAssociation_VERBOSE}
!define Index "Line${__LINE__}"
 
   Exch $R1 ;desc
   Exch
  Exch $R0 ;ext
  Exch
  Push $0
  Push $1
 
   ReadRegStr $1 HKCR $R0 ""
   ReadRegStr $1 HKCR $R0 ""
   StrCmp $1 $R1 0 "${Index}-NoOwn" ; only do this if we own it
   StrCmp $1 $R1 0 NoOwn ; only do this if we own it
   ReadRegStr $1 HKCR $R0 "backup_val"
   ReadRegStr $1 HKCR $R0 "backup_val"
   StrCmp $1 "" 0 "${Index}-Restore" ; if backup="" then delete the whole key
   StrCmp $1 "" 0 Restore ; if backup="" then delete the whole key
   DeleteRegKey HKCR $R0
   DeleteRegKey HKCR $R0
   Goto "${Index}-NoOwn"
   Goto NoOwn
"${Index}-Restore:"
 
Restore:
   WriteRegStr HKCR $R0 "" $1
   WriteRegStr HKCR $R0 "" $1
   DeleteRegValue HKCR $R0 "backup_val"
   DeleteRegValue HKCR $R0 "backup_val"
   DeleteRegKey HKCR $R1 ;Delete key with association name settings
   DeleteRegKey HKCR $R1 ;Delete key with association name settings
"${Index}-NoOwn:"
 
!undef Index
NoOwn:
FunctionEnd
 
  Pop $1
  Pop $0
  Pop $R1
  Pop $R0
 
  !verbose pop
!macroend
 
!endif # !FileAssociation_INCLUDED
</highlight-nsis>
</highlight-nsis>


== Info ==
== Info ==
Any refereces to execute.exe should be replaced with the appropriate command to perform the specified action.
Any references to execute.exe should be replaced with the appropriate command to perform the specified action.


This page was based on information provided in "Examples\makensis.nsi" and this thread: [http://forums.winamp.com/showthread.php?s=&threadid=140254 http://forums.winamp.com/showthread.php?s=&threadid=140254]
This page was based on information provided in "Examples\makensis.nsi" and this thread: [http://forums.winamp.com/showthread.php?s=&threadid=140254 http://forums.winamp.com/showthread.php?s=&threadid=140254]

Latest revision as of 00:41, 4 June 2010

Author: Vytautas (talk, contrib)


Author: intersol (talk, contrib)


Author: chefkoch (talk, contrib)


Description

Macro for registering and unregistering file extensions in NSIS scripts.

Can easily be used in installer AND uninstaller sections and functions. Won't overwrite any variables like $0 $1 ..... and $R0 $R1....... .

Usage

!include "FileAssociation.nsh"
...
# later, inside a section:
${registerExtension} "c:\myplayer.exe" ".mkv" "MKV_File"
 
${unregisterExtension} ".mkv" "MKV File"

FileAssociation.nsh

/*
_____________________________________________________________________________
 
                       File Association
_____________________________________________________________________________
 
 Based on code taken from http://nsis.sourceforge.net/File_Association 
 
 Usage in script:
 1. !include "FileAssociation.nsh"
 2. [Section|Function]
      ${FileAssociationFunction} "Param1" "Param2" "..." $var
    [SectionEnd|FunctionEnd]
 
 FileAssociationFunction=[RegisterExtension|UnRegisterExtension]
 
_____________________________________________________________________________
 
 ${RegisterExtension} "[executable]" "[extension]" "[description]"
 
"[executable]"     ; executable which opens the file format
                   ;
"[extension]"      ; extension, which represents the file format to open
                   ;
"[description]"    ; description for the extension. This will be display in Windows Explorer.
                   ;
 
 
 ${UnRegisterExtension} "[extension]" "[description]"
 
"[extension]"      ; extension, which represents the file format to open
                   ;
"[description]"    ; description for the extension. This will be display in Windows Explorer.
                   ;
 
_____________________________________________________________________________
 
                         Macros
_____________________________________________________________________________
 
 Change log window verbosity (default: 3=no script)
 
 Example:
 !include "FileAssociation.nsh"
 !insertmacro RegisterExtension
 ${FileAssociation_VERBOSE} 4   # all verbosity
 !insertmacro UnRegisterExtension
 ${FileAssociation_VERBOSE} 3   # no script
*/
 
 
!ifndef FileAssociation_INCLUDED
!define FileAssociation_INCLUDED
 
!include Util.nsh
 
!verbose push
!verbose 3
!ifndef _FileAssociation_VERBOSE
  !define _FileAssociation_VERBOSE 3
!endif
!verbose ${_FileAssociation_VERBOSE}
!define FileAssociation_VERBOSE `!insertmacro FileAssociation_VERBOSE`
!verbose pop
 
!macro FileAssociation_VERBOSE _VERBOSE
  !verbose push
  !verbose 3
  !undef _FileAssociation_VERBOSE
  !define _FileAssociation_VERBOSE ${_VERBOSE}
  !verbose pop
!macroend
 
 
 
!macro RegisterExtensionCall _EXECUTABLE _EXTENSION _DESCRIPTION
  !verbose push
  !verbose ${_FileAssociation_VERBOSE}
  Push `${_DESCRIPTION}`
  Push `${_EXTENSION}`
  Push `${_EXECUTABLE}`
  ${CallArtificialFunction} RegisterExtension_
  !verbose pop
!macroend
 
!macro UnRegisterExtensionCall _EXTENSION _DESCRIPTION
  !verbose push
  !verbose ${_FileAssociation_VERBOSE}
  Push `${_EXTENSION}`
  Push `${_DESCRIPTION}`
  ${CallArtificialFunction} UnRegisterExtension_
  !verbose pop
!macroend
 
 
 
!define RegisterExtension `!insertmacro RegisterExtensionCall`
!define un.RegisterExtension `!insertmacro RegisterExtensionCall`
 
!macro RegisterExtension
!macroend
 
!macro un.RegisterExtension
!macroend
 
!macro RegisterExtension_
  !verbose push
  !verbose ${_FileAssociation_VERBOSE}
 
  Exch $R2 ;exe
  Exch
  Exch $R1 ;ext
  Exch
  Exch 2
  Exch $R0 ;desc
  Exch 2
  Push $0
  Push $1
 
  ReadRegStr $1 HKCR $R1 ""  ; read current file association
  StrCmp "$1" "" NoBackup  ; is it empty
  StrCmp "$1" "$R0" NoBackup  ; is it our own
    WriteRegStr HKCR $R1 "backup_val" "$1"  ; backup current value
NoBackup:
  WriteRegStr HKCR $R1 "" "$R0"  ; set our file association
 
  ReadRegStr $0 HKCR $R0 ""
  StrCmp $0 "" 0 Skip
    WriteRegStr HKCR "$R0" "" "$R0"
    WriteRegStr HKCR "$R0\shell" "" "open"
    WriteRegStr HKCR "$R0\DefaultIcon" "" "$R2,0"
Skip:
  WriteRegStr HKCR "$R0\shell\open\command" "" '"$R2" "%1"'
  WriteRegStr HKCR "$R0\shell\edit" "" "Edit $R0"
  WriteRegStr HKCR "$R0\shell\edit\command" "" '"$R2" "%1"'
 
  Pop $1
  Pop $0
  Pop $R2
  Pop $R1
  Pop $R0
 
  !verbose pop
!macroend
 
 
 
!define UnRegisterExtension `!insertmacro UnRegisterExtensionCall`
!define un.UnRegisterExtension `!insertmacro UnRegisterExtensionCall`
 
!macro UnRegisterExtension
!macroend
 
!macro un.UnRegisterExtension
!macroend
 
!macro UnRegisterExtension_
  !verbose push
  !verbose ${_FileAssociation_VERBOSE}
 
  Exch $R1 ;desc
  Exch
  Exch $R0 ;ext
  Exch
  Push $0
  Push $1
 
  ReadRegStr $1 HKCR $R0 ""
  StrCmp $1 $R1 0 NoOwn ; only do this if we own it
  ReadRegStr $1 HKCR $R0 "backup_val"
  StrCmp $1 "" 0 Restore ; if backup="" then delete the whole key
  DeleteRegKey HKCR $R0
  Goto NoOwn
 
Restore:
  WriteRegStr HKCR $R0 "" $1
  DeleteRegValue HKCR $R0 "backup_val"
  DeleteRegKey HKCR $R1 ;Delete key with association name settings
 
NoOwn:
 
  Pop $1
  Pop $0
  Pop $R1
  Pop $R0
 
  !verbose pop
!macroend
 
!endif # !FileAssociation_INCLUDED

Info

Any references to execute.exe should be replaced with the appropriate command to perform the specified action.

This page was based on information provided in "Examples\makensis.nsi" and this thread: http://forums.winamp.com/showthread.php?s=&threadid=140254

Vytautas