File Association: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Reverted edits by 203.113.13.3 to last version by Joost)
 
(Replaced the register/unregister file extension macro/function)
Line 1: Line 1:
{{PageAuthor|Vytautas}}
{{PageAuthor|Vytautas}}
{{PageAuthor|intersol}}


== Description ==
== Description ==
You can put NSIS code in a section to associate the file:
Macro for registering and unregistering file extensions in NSIS scripts.


'''[UPDATE: 2003-11-12]'''
== Usage ==
Added 'Index' to enable this code to be used more than once in the same script.
<highlight-nsis>
!include "registerExtension.nsh"
...
# later, inside a section:
${registerExtension} "c:\myplayer.exe" ".mkv" "MKV File"


== The Script ==
${unregisterExtension} ".mkv" "MKV File"
<highlight-nsis>
  ; back up old value of .opt
!define Index "Line${__LINE__}"
  ReadRegStr $1 HKCR ".opt" ""
  StrCmp $1 "" "${Index}-NoBackup"
    StrCmp $1 "OptionsFile" "${Index}-NoBackup"
    WriteRegStr HKCR ".opt" "backup_val" $1
"${Index}-NoBackup:"
  WriteRegStr HKCR ".opt" "" "OptionsFile"
  ReadRegStr $0 HKCR "OptionsFile" ""
  StrCmp $0 "" 0 "${Index}-Skip"
WriteRegStr HKCR "OptionsFile" "" "Program Options File"
WriteRegStr HKCR "OptionsFile\shell" "" "open"
WriteRegStr HKCR "OptionsFile\DefaultIcon" "" "$INSTDIR\execute.exe,0"
"${Index}-Skip:"
  WriteRegStr HKCR "OptionsFile\shell\open\command" "" \
    '$INSTDIR\execute.exe "%1"'
  WriteRegStr HKCR "OptionsFile\shell\edit" "" "Edit Options File"
  WriteRegStr HKCR "OptionsFile\shell\edit\command" "" \
    '$INSTDIR\execute.exe "%1"'


  System::Call 'Shell32::SHChangeNotify(i 0x8000000, i 0, i 0, i 0)'
!undef Index
  ; Rest of script
</highlight-nsis>
</highlight-nsis>
In the uninstaller you might want to restore the original file association, if it existed, the following code will accomplish this:


<highlight-nsis>
  ;start of restore script
!define Index "Line${__LINE__}"
  ReadRegStr $1 HKCR ".opt" ""
  StrCmp $1 "OptionsFile" 0 "${Index}-NoOwn" ; only do this if we own it
    ReadRegStr $1 HKCR ".opt" "backup_val"
    StrCmp $1 "" 0 "${Index}-Restore" ; if backup="" then delete the whole key
      DeleteRegKey HKCR ".opt"
    Goto "${Index}-NoOwn"
"${Index}-Restore:"
      WriteRegStr HKCR ".opt" "" $1
      DeleteRegValue HKCR ".opt" "backup_val"
 
    DeleteRegKey HKCR "OptionsFile" ;Delete key with association settings
    System::Call 'Shell32::SHChangeNotify(i 0x8000000, i 0, i 0, i 0)'
"${Index}-NoOwn:"
!undef Index
  ;rest of script
</highlight-nsis>
{| border=1 cellspacing=0
{| border=1 cellspacing=0
|-
|-
Line 73: Line 34:
|}
|}


== registerExtension.nsh ==
<highlight-nsis>
!define registerExtension "!insertmacro registerExtension"
!define unregisterExtension "!insertmacro unregisterExtension"
!macro registerExtension executable extension description
      Push "${executable}"  ; "full path to my.exe"
      Push "${extension}"  ;  ".mkv"
      Push "${description}" ;  "MKV File"
      Call registerExtension
!macroend
; back up old value of .opt
Function registerExtension
!define Index "Line${__LINE__}"
  pop $R0 ; ext name
  pop $R1
  pop $R2
  push $1
  push $0
  ReadRegStr $1 HKCR $R1 ""
  StrCmp $1 "" "${Index}-NoBackup"
    StrCmp $1 "OptionsFile" "${Index}-NoBackup"
    WriteRegStr HKCR $R1 "backup_val" $1
"${Index}-NoBackup:"
  WriteRegStr HKCR $R1 "" $R0
  ReadRegStr $0 HKCR $R0 ""
  StrCmp $0 "" 0 "${Index}-Skip"
WriteRegStr HKCR $R0 "" $R0
WriteRegStr HKCR "$R0\shell" "" "open"
WriteRegStr HKCR "$R0\DefaultIcon" "" "$R2,0"
"${Index}-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 $0
  pop $1
!undef Index
FunctionEnd
!macro unregisterExtension extension description
      Push ${extension}  ;  ".mkv"
      Push ${description}  ;  "MKV File"
      Call unregisterExtension
!macroend
Function unregisterExtension
  pop $R1 ; description
  pop $R0 ; extension
!define Index "Line${__LINE__}"
  ReadRegStr $1 HKCR $R0 ""
  StrCmp $1 $R1 0 "${Index}-NoOwn" ; only do this if we own it
  ReadRegStr $1 HKCR $R0 "backup_val"
  StrCmp $1 "" 0 "${Index}-Restore" ; if backup="" then delete the whole key
  DeleteRegKey HKCR $R0
  Goto "${Index}-NoOwn"
"${Index}-Restore:"
  WriteRegStr HKCR $R0 "" $1
  DeleteRegValue HKCR $R0 "backup_val"
  DeleteRegKey HKCR $R1 ;Delete key with association name settings
"${Index}-NoOwn:"
!undef Index
FunctionEnd
</highlight-nsis>
== Info ==
Any refereces to execute.exe should be replaced with the appropriate command to perform the specified action.
Any refereces to execute.exe should be replaced with the appropriate command to perform the specified action.



Revision as of 11:25, 24 November 2007

Author: Vytautas (talk, contrib)


Author: intersol (talk, contrib)


Description

Macro for registering and unregistering file extensions in NSIS scripts.

Usage

!include "registerExtension.nsh"
...
# later, inside a section:
${registerExtension} "c:\myplayer.exe" ".mkv" "MKV File"
 
${unregisterExtension} ".mkv" "MKV File"
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


registerExtension.nsh

!define registerExtension "!insertmacro registerExtension"
!define unregisterExtension "!insertmacro unregisterExtension"
 
!macro registerExtension executable extension description
       Push "${executable}"  ; "full path to my.exe"
       Push "${extension}"   ;  ".mkv"
       Push "${description}" ;  "MKV File"
       Call registerExtension
!macroend
 
; back up old value of .opt
Function registerExtension
!define Index "Line${__LINE__}"
  pop $R0 ; ext name
  pop $R1
  pop $R2
  push $1
  push $0
  ReadRegStr $1 HKCR $R1 ""
  StrCmp $1 "" "${Index}-NoBackup"
    StrCmp $1 "OptionsFile" "${Index}-NoBackup"
    WriteRegStr HKCR $R1 "backup_val" $1
"${Index}-NoBackup:"
  WriteRegStr HKCR $R1 "" $R0
  ReadRegStr $0 HKCR $R0 ""
  StrCmp $0 "" 0 "${Index}-Skip"
	WriteRegStr HKCR $R0 "" $R0
	WriteRegStr HKCR "$R0\shell" "" "open"
	WriteRegStr HKCR "$R0\DefaultIcon" "" "$R2,0"
"${Index}-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 $0
  pop $1
!undef Index
FunctionEnd
 
!macro unregisterExtension extension description
       Push ${extension}   ;  ".mkv"
       Push ${description}   ;  "MKV File"
       Call unregisterExtension
!macroend
 
Function unregisterExtension
  pop $R1 ; description
  pop $R0 ; extension
!define Index "Line${__LINE__}"
  ReadRegStr $1 HKCR $R0 ""
  StrCmp $1 $R1 0 "${Index}-NoOwn" ; only do this if we own it
  ReadRegStr $1 HKCR $R0 "backup_val"
  StrCmp $1 "" 0 "${Index}-Restore" ; if backup="" then delete the whole key
  DeleteRegKey HKCR $R0
  Goto "${Index}-NoOwn"
"${Index}-Restore:"
  WriteRegStr HKCR $R0 "" $1
  DeleteRegValue HKCR $R0 "backup_val"
  DeleteRegKey HKCR $R1 ;Delete key with association name settings
"${Index}-NoOwn:"
!undef Index
FunctionEnd


Info

Any refereces 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