Old 17th October 2010, 18:26   #1
Majek
Junior Member
 
Join Date: Oct 2010
Posts: 9
File association/defaults question

Hello again, I'm trying to associate my application with .txt files and make it the default program for opening .txt files. My app only runs on Vista/Windows 7 so I figured IApplicationAssociationRegistration would do the trick.

I found this plugin on the nsis website:
http://nsis.sourceforge.net/Applicat...ration_plug-in

I have a custom InstallOptions page with a checkbox "Make Notpad the default text editor". First it associates .txt files then reads the ini to get the checkbox state. If checked, it sets the app as default:

Function CustomPageEnd
WriteRegStr HKLM "Software\Classes\Notpad.TextFile" "" "Text Document"
WriteRegStr HKLM "Software\Classes\Notpad.TextFile\DefaultIcon" "" "$INSTDIR\Notpad.exe, 1"
WriteRegStr HKLM "Software\Classes\Notpad.TextFile\shell\open\command" "" "$INSTDIR\Notpad.exe %1"

SetRegView 64
WriteRegStr HKLM "Software\Notpad\Capabilities" "ApplicationDescription" "Notpad is not Notepad"
WriteRegStr HKLM "Software\Notpad\Capabilities\FileAssociations" ".txt" "Notpad.TextFile"

WriteRegStr HKLM "Software\RegisteredApplications" "Notpad" "Software\Notpad\Capabilities"
SetRegView 32

!insertmacro MUI_INSTALLOPTIONS_READ $R1 "notpad.ini" "Field 3" "State"
${If} $R1 == 1
AppAssocReg::SetAppAsDefault "Notpad" ".txt" "file"
Pop $0
MessageBox MB_ICONINFORMATION|MB_OK "SetAppAsDefault Result = $0"
${EndIf}
FunctionEnd

It seems to associate the type correctly, I can right-click .txt files and Notpad appears in the "Open with" submenu. The problem is the SetAppAsDefault function always returns "method failed". I don't know if I'm using it properly the documentation on the plugin page is a little confusing.

I looked in the registry and everything looks good, except it creates 2 copies of the RegisteredApplications\Notpad key. One in HKLM\Software and one in HKLM\Software\Wow6432Node, even when I use SetRegView 64 in the script. Maybe this is whats causing SetAppAsDefault to fail. Am I doing something wrong?
Majek is offline   Reply With Quote
Old 17th October 2010, 19:13   #2
Wizou
Senior Member
 
Join Date: Aug 2007
Location: Paris, France
Posts: 304
Try to contact the plugin's author ?

Anybody can list his plugin on http://nsis.sourceforge.net/
I'm not sure the author reads this forum regularly.
Wizou is offline   Reply With Quote
Old 18th October 2010, 19:04   #3
Majek
Junior Member
 
Join Date: Oct 2010
Posts: 9
Well I've done a few tests and noticed something strange...
First I created a managed wrapper around the IApplicationAssociationRegistration COM interface, and tried using it in my application.

http://pastebin.com/L5xfjdij

private void SetDefault()
{
var aar = new ApplicationAssociationRegistration();
var iaar = (IApplicationAssociationRegistration)aar;
string currentDefault = iaar.QueryCurrentDefault(".txt", Global.AssociationType.AT_FILEEXTENSION, Global.AssociationLevel.AL_EFFECTIVE);
if (currentDefault != "Notpad.TextFile") iaar.SetAppAsDefault("Notpad", ".txt", AssociationType.AT_FILEEXTENSION);
}

From c# everything works fine, QueryCurrentDefault returns "txtfile", then it sets the default to my app. But when I try the same code in my script using the plugin:

AppAssocReg::QueryCurrentDefault ".txt" "file" "effective"
Pop $0
${If} $0 != "Notpad.TextFile"
AppAssocReg::SetAppAsDefault "Notpad" ".txt" "file"
Pop $1
MessageBox MB_ICONINFORMATION|MB_OK "SetAppAsDefault Result: $1"
${EndIf}

QueryCurrentDefault works, but SetAppAsDefault still returns "method failed". The strange thing is when I try other applications and association types like "Internet Explorer" "http" "protocol", the function succeeds and sets the default.

Has anyone else used this plugin before or the IApplicationAssociationRegistration api in their script? I don't understand why it works for other apps but not mine
Majek is offline   Reply With Quote
Old 18th October 2010, 20:22   #4
demiller9
Senior Member
 
Join Date: Mar 2006
Location: Dallas
Posts: 462
Would it make a difference if Notepad were spelled correctly?
demiller9 is offline   Reply With Quote
Old 18th October 2010, 22:22   #5
Majek
Junior Member
 
Join Date: Oct 2010
Posts: 9
Notpad is the name of my application. It's a replacement for notepad with more features like tabs, desktop composition and windows 7 jumplists. To replace notepad as the default text editor I need to use IApplicationAssociationRegistation api in my nsis script.

The creator of the plugin says it works 100% on vista/7. The api works fine when I run it from c#, it only fails when I run it from my script using the plugin.
Majek is offline   Reply With Quote
Old 20th October 2010, 04:14   #6
Majek
Junior Member
 
Join Date: Oct 2010
Posts: 9
After trying some more tests the error the function is really returning is HRESULT 0x80070002 (file not found). I get the same error from c# so the plugin seems to be working fine. I still can't figure out whats causing it to fail, I'll keep trying I guess

If anyone knows what is causing this error please let me know!
Majek is offline   Reply With Quote
Old 20th October 2010, 05:22   #7
Majek
Junior Member
 
Join Date: Oct 2010
Posts: 9
Sorry for double posting, but I noticed something else very strange...

SetAppAsDefault fails when called from the installer
SetAppAsDefault fails when called from Notpad.vshost.exe (Debug mode)
SetAppAsDefault succeeds when called from Notpad.exe (Release mode)

It appears the function only succeeds if the process is open, but that still doesn't explain why it works for other programs like ie.

I guess my only option is to launch Notpad.exe silently from the installer and set the default that way. I still need a way to reset the default back to notepad when Notpad is uninstalled.
Majek is offline   Reply With Quote
Reply
Go Back   Winamp & Shoutcast Forums > Developer Center > NSIS Discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump