;; 
;;=====================================================================================----- 
;; 
;;FUNCTION       Associate() 
;; 
;;ACTION         Create, Update, Add, or Delete OPEN and EDIT file associations 
;;               Supports System (default) or per-user associations. 
;;               Can define system associations remotely. 
;; 
;;AUTHOR         Glenn Barnas (original code) & Mart (HKCU support) 
;; 
;;VERSION        2.0  - 2014/12/22 
;; 
;;HISTORY        1.0  - 2003/02/19 - Original release 
;;               2.0  - 2014/12/22 - Updated by Mart to handle Per User associations, 
;;                                   added variable validation, Updated to handle Delete 
;;                                   action, code cleanup to current coding standards. 
;; 
;;SYNTAX         Associate(Extension, Type [, Description] [, OPEN command [, EDIT command] [, Add] [, System] [, Icon] [, UFlag]) 
;; 
;;PARAMETERS     Extension - REQUIRED - String, File extension to associate 
;; 
;;               Type - REQUIRED - String, Short FileType description (no spaces) 
;; 
;;               Description - OPTIONAL - String, Description of File Type 
;; 
;;               OPEN Command - OPTIONAL - String, Command to OPEN (execute) the 
;;               associated file 
;; 
;;               EDIT Command - OPTIONAL - String, Command to EDIT the associated file 
;; 
;;               ADD - OPTIONAL - Boolean, if set, adds a new Extension to an existing 
;;               association definition. Should be 0 if defining a new association. 
;; 
;;               System - OPTIONAL - String, System where association is to be made. 
;;               Use null for local system 
;; 
;;               Icon - OPTIONAL - String, String with "PATH,INDEX" format specifying 
;;               the Icon file, optional Icon index.  
;; 
;;               UFlag - OPTIONAL - Boolean, Perform HKCU instead of HKLM association 
;; 
;; 
;;REMARKS        Specifying a null Open Command will delete the association 
;; 
;;RETURNS        nothing 
;; 
;;DEPENDENCIES   none 
;; 
;;TESTED WITH    NT4, W2K, WXP, W2K3, Vista, X64 
;; 
;;EXAMPLES       ; Create an association for Kix open/edit 
;;               Associate('.KIX', 'KixScript', 'Kixtart Script', 'Kix32.exe', 'notepad.exe') 
;; 
;;               ; Add a .SCR extension to the KixScript association 
;;               Associate('.SCR', 'KixScript', '', '', '', 1) 
;; 
;;               ;Create current user association on local system. 
;;               Associate('.KIX', 'KixScript', 'KiXtart script file', 'kix32.exe', 'notepad.exe',,,$Icon,1) 
;; 
;;               ;Delete current user association on local system. 
;;               Associate('.KIX', 'KixScript',,,,,,,1) 
;; 
;;               ;Delete system association on local system. 
;;               Associate('.KIX', 'KixScript') 
;;               Associate('.SCR', 'KixScript') 
; 
Function Associate($_Extension, $_Type, OPTIONAL $_Description, OPTIONAL $_OCmd,  OPTIONAL $_ECmd,  OPTIONAL $_AddFlag,  OPTIONAL $_Target, OPTIONAL $_ICON, OPTIONAL $_UFlag)
 
  Dim $_WSPath
  Dim $_Root
  Dim $_RTN
 
  ; make sure the "dot" is specified 
  If Left($_Extension, 1) <> '.'
    $_Extension = '.' + $_Extension
  EndIf
 
  ; required Extension and Type args can't be null 
  If Not $_Extension Or Not $_Type
    Exit 87
  EndIf
 
  $_AddFlag = VAl($_AddFlag)					; convert to numeric 
 
  ; Clear Current User flag if target hostname is defined - HKCU only works interactively on the local machine 
  If $_Target $_UFlag = 0 EndIf
 
  ; insure that "$_Target" has the right format if it's specified 
  $_Target =  IIf(CStr($_Target) <> '', '\\' + Join(Split(CStr($_Target), '\'), '', 3) + '\', '')
 
  ; Define the registry root path 
  $_Root = IIf($_UFlag, 'HKEY_CURRENT_USER\SOFTWARE\Classes\', 'HKEY_CLASSES_ROOT\')
 
  ; Obtain the Windows System Path value from the target system - use this reg entry 
  ; instead of the %SystemRoot% variable so it is relative to the target and not the 
  ; local machine. 
  $_WSPath = ReadValue($_Target + 'HKEY_Local_Machine\SOFTWARE\Microsoft\Windows NT\CurrentVersion',  'SystemRoot')
 
  ; Delete and then define the Extension if the Open command is specified or Add mode is active 
  $_RTN = DelTree($_Target + $_Root + $_Extension)
  If $_OCmd or $_AddFlag 
    $_RTN = WriteValue($_Target + $_Root + $_Extension, '', $_Type, 'REG_SZ')
  EndIf
 
  ; just return if we're adding a new Extension to an existing association 
  If $_AddFlag
    Exit 0
  EndIf
 
  ; Create the definitions for the OPEN command 
  $_RTN = DelTree($_Target + $_Root + $_Type)
  If $_OCmd
    $_RTN = WriteValue($_Target + $_Root + $_Type, '', $_Description, 'REG_SZ')
    $_RTN = WriteValue($_Target + $_Root + $_Type + '\Shell\Open\Command', '', $_OCmd, 'REG_EXPAND_SZ')
 
    ; Use specific or standard icon 
    If $_Icon
      $_RTN = WriteValue($_Target + $_Root + $_Type + '\DefaultIcon', '', $_Icon, 'REG_SZ')
    Else
      $_RTN = WriteValue($_Target + $_Root + $_Type + '\DefaultIcon', '', $_WSPath + '\system32\SHELL32.dll,21', 'REG_SZ')
    EndIf
 
    ; Create the association for the EDIT command, if specified 
    If $_ECmd <> ''
      $_RTN = WriteValue($_Target + $_Root + $_Type + '\Shell\Edit\Command', '', $_ECmd, 'REG_EXPAND_SZ')
    EndIf
  EndIf
 
  Exit 0
 
EndFunction