Page 1 of 1 1
Topic Options
#184891 - 2008-01-28 11:02 PM RFC: Associate2() - Create file association per user.
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Inspired by Glenn's Associate() UDF and by the MS article referenced in the comments I altered the Associate UDF a bit (hope you don't mind Glenn \:\) ) to create file associations on a per user basis. It sets the association in hkcu\software\classes so it can be used in a logon script. A per user file association will override a per machine file association.

I would like to hear suggestion/comments/whatever from you guys before posting it in the UDF forum.


 Code:
;FUNCTION Associate2()
;
;ACTION:
;	Associates a per user file extension with a particular action.
;	A per use file association overrides the one set per system.
;
;AUTHOR:
;	Mart
;	Based on the Associate UDF by Glenn Barnas.
;	Inspired by: http://support.microsoft.com/kb/257592
;
;SYNTAX:
;	Associate2(extension, type, description, open, edit, icon)
;
;PARAMETERS:
;	Extension (required)
;		File extension to associate.
;
;	Type (required)
;		Short FileType description.
;
;	Description (required)
;		Description of File Type.
;
;	Open Command (required)
;		Command to Open (Execute) the associated file.
;
;	Edit Command (optional)
;		Command to EDIT the associated file.
;
;	Icon (optional)
;		Default icon for the specific filetype.
;		Can either be a .ico file or something like shell32.dll,3 (third icon in shell32.dll).
;		If none is given the default icon for unknown file types is used by windows.
;
;Remarks:
;	Should work with Windows 2000 and up.
;	Developed and tested on Windows XP SP2 and Kix4.60.
;
;RETURNS:
;	System error codes.
;
;DEPENDENCIES:
;	None
;
;EXAMPLES:
;	;Define a new association to open (execute) and edit .kix files and set an icon.
;		Associate2(".kix", "KixScript", "Kixtart Script", "Kix32.exe", "notepad.exe", "c:\someicon.ico")
;
Function Associate2($extension, $type, $description, $opencmd, optional $editcmd, $icon)

	Dim $rc

	;Make sure the dot is specified.
	If Left($extension, 1) <> "."
		$extension = "." + $extension
	EndIf
	
	;Exit if a required parameter is empty.
	Select
		Case $extension = ""
			Exit 87
		Case $type = ""
			Exit 87
		Case $description = ""
			Exit 87
		Case $opencmd = ""
			Exit 87
	EndSelect

	;Set file type and description.
	$rc = WriteValue("HKCU\SOFTWARE\Classes\" + $extension, "", $type, "REG_SZ")
	$rc = WriteValue("HKCU\SOFTWARE\Classes\" + $extension + "\" + $type, "", $description, "REG_SZ")
	
	;Create the values for the open command.
	$rc = WriteValue("HKCU\SOFTWARE\Classes\" + $extension + "\Shell\Open\Command", "", $opencmd, "REG_EXPAND_SZ")
	
	;Create the definitions for the edit command.
	If $editcmd <> ""
		$rc = WriteValue("HKCU\SOFTWARE\Classes\" + $extension + "\Shell\Edit\Command", "", $editcmd, "REG_EXPAND_SZ")
	EndIf
	
	;Set icon.
	If $icon <> ""
		$rc = WriteValue("HKCU\SOFTWARE\Classes\" + $extension + "\DefaultIcon", "", $icon, "REG_SZ")
	EndIf
	
	
	If @ERROR = 0
		$Asssociate2 = 0
	Else
		$Asssociate2 = @ERROR
	EndIf
EndFunction


Edited by Mart (2008-01-28 11:04 PM)
Edit Reason: Typo: His name is Glenn and not Gelnn.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#184901 - 2008-01-29 01:59 AM Re: RFC: Associate2() - Create file association per user. [Re: Mart]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Well, at least you got both "n"s in it! \:D

I don't mind updates to my UDFs at all! (but) "Associate2" implies "newer" rather than "different", so I've got a concern with the name. What if we modified Associate to accept a flag and simply updated the original post with your enhancements?

Take a look at "associate2" on my resources page - I incorporated your changes to the original code.

I got rid of the AddKey statements, which are no longer required (shows how old this is!), but kept the DelTree. Without that, you can't completely replace the keys properly. Also maintained the remote access capability, since I use that in my admin scripts, and changed the icon to use the standard windows icon file if none is specified.

Haven't tested it yet - if you can, and think the update is OK, I'd just as soon update the original post to reflect your changes. Make sure you have your credits defined the way you want then as well.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#184907 - 2008-01-29 09:57 AM Re: RFC: Associate2() - Create file association per user. [Re: Glenn Barnas]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Cool. I'm fine with an updated Associate function.

This HKCU stuff started with Win2K, I guess that’s also an indicator of how old this UDF is ;\)

I'll do some testing and get back to you.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#184934 - 2008-01-29 10:51 PM Re: RFC: Associate2() - Create file association per user. [Re: Mart]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Glenn,

Did several tests with a regular user (non local or domain admin) and everything seems to work as it should. The examples do not show the new options but that can be easily fixed.
Also when running it on an admin account (local or domain) it all seems to work as it should.

Dunno if anybody has other experiences but imho the original UDF can be updated with the updated code to accept per user associations. Will do some more test just to be sure but up until now it looks like all is ok.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#184937 - 2008-01-30 01:32 AM Re: RFC: Associate2() - Create file association per user. [Re: Mart]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Did you try to delete the association by specifying a null OpenCommand? That was something I added when I migrated your changes in.

Go ahead and create some examples of user-based associations from your testing and I'll update the code.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#184946 - 2008-01-30 09:23 AM Re: RFC: Associate2() - Create file association per user. [Re: Glenn Barnas]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Did not try deleting anything. Will do today. I'll get back to you with some results and examples.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#184947 - 2008-01-30 10:26 AM Re: RFC: Associate2() - Create file association per user. [Re: Mart]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Ok. Tried all the examples below and they all work as expected.
Deleting the association by specifying a null value fort the open command should be explained in the comments imho. Sure it can be found in the code but a small explanation in the comments would be nice.

This is much better then having an Associate2 UDF

 Code:
;Create per user association on remote system.
Associate(".kix","Kix Script", "KiXtart script file", "kix32.exe", "notepad.exe",,"system01","icon.ico",1)
;Delete per user association on remote system.
Associate(".kix","Kix Script", "KiXtart script file", "", "",,"system01",,)

;Create per system association on remote system.
Associate(".kix","Kix Script", "KiXtart script file", "kix32.exe", "notepad.exe",,"system01","icon.ico",)
;Delete per system association on remote system.
Associate(".kix","Kix Script", "KiXtart script file", "", "",,"system01",,)

;Create per user association on local system.
Associate(".kix","Kix Script", "KiXtart script file", "kix32.exe", "notepad.exe",,,"icon.ico",1)
;Delete per user association on local system.
Associate(".kix","Kix Script", "KiXtart script file", "", "",,,,)

;Create per system association on local system.
Associate(".kix","Kix Script", "KiXtart script file", "kix32.exe", "notepad.exe",,,"icon.ico",)
;Delete per system association on remote system.
Associate(".kix","Kix Script", "KiXtart script file", "", "",,,,)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#184948 - 2008-01-30 12:48 PM Re: RFC: Associate2() - Create file association per user. [Re: Mart]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
The delete was a last-minute addition to the code - I figured it was best to test it before documenting it! \:D

I'll update the header and post the new code shortly. Thanks for the efforts on this!

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#184971 - 2008-01-31 11:09 AM Re: RFC: Associate2() - Create file association per user. [Re: Glenn Barnas]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
There was an error in two of the examples.
New examples posted below.

 Code:
;Create per user association on remote system.
Associate(".kix","Kix Script", "KiXtart script file", "kix32.exe", "notepad.exe",,"system01","icon.ico",1)
;Delete per user association on remote system.
Associate(".kix","Kix Script", "KiXtart script file", "", "",,"system01",,1)

;Create per system association on remote system.
Associate(".kix","Kix Script", "KiXtart script file", "kix32.exe", "notepad.exe",,"system01","icon.ico",)
;Delete per system association on remote system.
Associate(".kix","Kix Script", "KiXtart script file", "", "",,"system01",,)

;Create per user association on local system.
Associate(".kix","Kix Script", "KiXtart script file", "kix32.exe", "notepad.exe",,,"icon.ico",1)
;Delete per user association on local system.
Associate(".kix","Kix Script", "KiXtart script file", "", "",,,,1)

;Create per system association on local system.
Associate(".kix","Kix Script", "KiXtart script file", "kix32.exe", "notepad.exe",,,"icon.ico",)
;Delete per system association on remote system.
Associate(".kix","Kix Script", "KiXtart script file", "", "",,,,)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 2145 anonymous users online.
Newest Members
BeeEm, min_seow, Audio, Hoschi, Comet
17882 Registered Users

Generated in 0.061 seconds in which 0.027 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org