How do I make Firefox open a file with an ampersand in the filename? - ruby

I've written some Ruby code to inspect ZIP-files as part of an internal company process. The way we usually launch this code is from a web browser. When you click to download the file, you select "open with" and specify the full path to a small batch file. This one-line batch file looks like this:
\\mathworks\public\Matthew_Simoneau\ruby-1.8.7-p72-i386-mswin32\bin\ruby.exe "%~dp0inspect.rb" %1
As far as I know, this technique is the only easy way to launch my Ruby code on a Windows machine which doesn't have Ruby installed, but does have access to the company internal filesystem.
I'm having a problem when the filename of the ZIP file contains an ampersand.
This works fine on IE and Chrome, where the above line gets "expanded" out to the following:
C:\WINNT\Profiles\matthew\Desktop>\\mathworks\public\Matthew_Simoneau\ruby-1.8.7-p72-i386-mswin32\bin\ruby.exe "\\mathworks\public\Matthew_Simoneau\sandbox\inspect\inspect.rb" "C:\WINNT\Profiles\matthew\Local Settings\Temporary Internet Files\Content.IE5\VNATJ3X0\park&park_paper_LMI_neuralN[1].zip"
On Firefox, however, everything after the ampersand in the filename gets dropped on the floor:
H:\>\\mathworks\public\Matthew_Simoneau\ruby-1.8.7-p72-i386-mswin32\bin\ruby.exe "\\mathworks\public\Matthew_Simoneau\sandbox\inspect\inspect.rb" C:\Temp\park
I've tried putting the %1 in quotes in the batch file, but that has no effect.
I suspect this is a bug in Firefox. I've searched the Firefox bug list, but didn't find anything.
Am I doing something wrong here? Is this a Firefox bug? If so, is there a way I can work around it? Is there a more robust way to launch my Ruby code from a web browser?
Update: I filed a bug report with Bugzilla#Mozilla, but there hasn't been a response yet.

Since you apparently have no control over the zip filenames, you could do the following in the Ruby code...
If the filename passed does not exist, look in the same folder for any file with the base passed filename followed by "&*.zip".
This will work for "park&park.zip" as long as there isn't also a zip file already in the folder named, say, "park&foo.zip".
If there is a real potential for filename collision (i.e., the zips aren't being cleaned off the user's machine and ampersands are common), the only other solution might be using a download manager plug-in in Firefox that has filename re-writing capability to fix any ampersands on the way down.

I've just tested something similar with Firefox 3.5.2 on Linux and it works well (i.e. shell script gets the correct path).
You should first try to use a script like that:
echo %1 > c:\temp\test.txt
(hope that's the right syntax, I didn't use cmd for a long time)
…and see if you get right path in that file. If the ampersand is stripped in that file too, and you're using the latest version of Firefox, that means you found bug specific to Windows platform and you should report it.

Use %24 for the & character. See the URL Encoding Table for other such problematic characters: http://webdesign.about.com/library/bl_url_encoding_table.htm

Encode your ampersand for best results...
&
As in
C:\WINNT\Profiles\matthew\Desktop>\\mathworks\public\Matthew_Simoneau\ruby-1.8.7-p72-i386-mswin32\bin\ruby.exe "\\mathworks\public\Matthew_Simoneau\sandbox\inspect\inspect.rb" "C:\WINNT\Profiles\matthew\Local Settings\Temporary Internet Files\Content.IE5\VNATJ3X0\park&park_paper_LMI_neuralN[1].zip"

Related

FTYPE/ASSOC priority and adding to OpenWithList from the command line

(Not sure if this belongs on superusers, but it seems there is a cmd.exe tag here, so here goes...)
As background, I'm working on a Firefox add-on (This question does not require knowledge of Firefox, btw, as Firefox add-ons can call the command line.) The add-on aims to build different kinds of shortcuts to cmd.exe (especially for the sake of my project https://github.com/brettz9/webappfind which allows files to be opened directly from the desktop into web apps).
Anyways, I'd like to give users the option to associate these shortcuts:
As the default handler for specific file extensions or file types.
To show up within the Open With list of applications (even if the user opts not to make the apps as default handlers)
As far as the default handling, I have found the ftype and assoc (and associate) commands, but I have read that user selections will override their behavior. Is there some way to ensure that I can get priority from the command line in associating file extensions to types and specific executables (until the user changes it again), or if it is not possible, then at least through C++ or the like?
As far as the Open With list:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\<file extension>\OpenWithList
...in my testing (with an exe), this command:
reg add HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.svg\OpenWithList /v d /d D:\wamp\www\webappfind\cplusplus\WebAppFinder-view-mode-Firefox.exe
...did cause the exe file to show up in:
reg query HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.svg\OpenWithList
...but it did not show up when I subsequently right-clicked a file with the ".svg" extension.
I would really appreciate any help with these two points.
REGEDIT4
[HKEY_CURRENT_USER\Software\Classes\Applications\MYFOO.exe\shell\open\command]
#="\"C:\\MYFOO.exe\" \"%1\""
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.myfoo]
"Application"="MYFOO.EXE"
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.myfoo\OpenWithList]
"a"="MYFOO.EXE"
"MRUList"="a"
So I got to an investigation what makes those file associations. It appears that you have to create a mapping from the bare EXE name to the full path as shown in the first two long-ish lines. Then you must only use the EXE name in the .extension branch. Setting the .extension's Application value will give you your default app instantly. Remember, only use APP.EXE, its full path must be defined as above. This was your main error. The "%1" part allows you to customize the parameters of your program so that it doesn't have to be just the opened document in quotes, as shown here. The backslashes are just escape characters for Regedit, you may discard them as you see fit.
The OpenWithList is tricky in the sense that there are letters for entries and just a blind write may overwrite some of the user's favorite apps. One approach would be to call your item "z" to lower the probability of overwriting. The right way would be enumerating the key and giving your app the first free letter. The MRUList is not essential, although it should have each used letter once and yours bumped to the start.
Note about user friendliness: Explorer will cache these values until next reboot. Make sure you update the registry and place exe first and create your file later. Although the caching only fully influences the display of the file and when it is run, the registry is read again and it will execute as you want.
TIP: If you decide to use Regedit instead of reg, the /s parameter skips the confirmation message and applies the values right away. Make sure you use double backslashes in the full path as shown. When preparing your temporary .reg file, make sure you append two CRLF's to the end or a glitch may cause your last line of code to be ignored. This sample starts with REGEDIT4 which signifies an ANSI file. If you need support for Unicode in your app path, you'll have to start the file with Windows Registry Editor Version 5.00 and store it in UTF16. This is already a superior solution to calling reg because there's no way you could get CMD.EXE to process special UTF stuff through the command line without mangling.

Bat file to open webpage with parameters to local file

Creating a bat file with:
start http://www.google.com/search?q=test
Does just what I would want it to do, it opens my default web browser (Chrome in my case) and browses to the URL http://www.google.com/search?q=test.
However, a bat file with:
start file:///C:/Users/d92495j/Desktop/OracleCDs/WebLogic/template.html?wbt=1
Only opens my default browser and browses to file:///C:/Users/d92495j/Desktop/OracleCDs/WebLogic/template.html
Note the lack of ?wbt=1. In order to fix this I've tried:
URL encoding the question mark
Running the start command parameters "window name" "file path in quotes"
Putting the file path in
variable and passing the variable to start
But none of those work. How can I get this to work?
I tested this and got the same result. I'm not really sure, but I guess this belongs to the question mark. The local file system of Windows can never have file names with ?, because this is a "wildcard" like *. I think it is possible that the file name is truncated there.
The following codes also doesn't work or produces only error messages:
start "file:///C:/Users/d92495j/Desktop/OracleCDs/WebLogic/template.html?wbt=1"
start file:///"C:/Users/d92495j/Desktop/OracleCDs/WebLogic/template.html?wbt=1"
The best solution I've come up with so far is:
powershell -noprofile -command "[void][System.Diagnostics.Process]::Start('chrome', 'file:///C:/Users/d92495j/Desktop/OracleCDs/WebLogic/template.html?wbt=1')"
This solution has the batch file launch PowerShell and then use the .NET System.Diagnostics.Process.Start method to launch Chrome with the correct parameter. The only downside is that it makes a browser choice for me and I'd prefer it to use my default browser, but I can live with that. I'll accept another answer (that is not much more complex) that uses the default browser.

Opening a CHM file produces: "navigation to the webpage was canceled"

I am trying to open a .chm file.
I downloaded the source, extracted it, and double clicked on Waffle.chm and clicked "Open" but no matter what element in the chm file I click, I get the message:
Navigation to the webpage was canceled.
What you can try:
Retype the address.
What's going on here?
Summary
Microsoft Security Updates 896358 & 840315 block display of CHM file contents when opened from a network drive (or a UNC path). This is Windows' attempt to stop attack vectors for viruses/malware from infecting your computer and has blocked out the .chm file that draw data over the "InfoTech" protocol, which this chm file uses.
Microsoft's summary of the problem: http://support.microsoft.com/kb/896054
Solutions
If you are using Windows Server 2008, Windows 7, windows has created a quick fix. Right click the chm file, and you will get the "yourfile.chm Properties" dialog box, at the bottom, a button called "Unblock" appears. Click Unblock and press OK, and try to open the chm file again, it works correctly. This option is not available for earlier versions of Windows before WindowsXP (SP3).
Solve the problem by moving your chm file OFF the network drive. You may be unaware you are using a network drive, double check now: Right click your .chm file, click properties and look at the "location" field. If it starts with two backslashes like this: \\epicserver\blah\, then you are using a networked drive. So to fix it, Copy the chm file, and paste it into a local drive, like C:\ or E:. Then try to reopen the chm file, windows does not freak out.
Last resort, if you can't copy/move the file off the networked drive. If you must open it where it sits, and you are using a lesser version of windows like XP, Vista, ME or other, you will have to manually tell Windows not to freak out over this .chm file. HHReg (HTML Help Registration Utility) Utility Automates this Task. Basically you download the HHReg utility, load your .chm file, press OK, and it will create the necessary registry keys to tell Windows not to block it. For more info: http://www.winhelponline.com/blog/fix-cannot-view-chm-files-network-xp-2003-vista/
Windows 8 or 10? --> Upgrade to Windows XP.
"unblocking" the file fixes the problem. Screenshot:
Win 8 x64:
just move it to another folder or rename your folder (in my case: my folder was "c#").
avoid to use symbol on folder name. name it with letter.
done.
In addition to Eric Leschinski's answer, and because this is stackoverflow, a programmatical solution:
Windows uses hidden file forks to mark content as "downloaded". Truncating these unblocks the file. The name of the stream used for CHM's is "Zone.Identifier". One can access streams by appending :streamname when opening the file. (keep backups the first time, in case your RTL messes that up!)
In Delphi it would look like this:
var f : file;
begin
writeln('unblocking ',s);
assignfile(f,'some.chm:Zone.Identifier');
rewrite(f,1);
truncate(f);
closefile(f);
end;
I'm told that on non forked filesystems (like FAT32) there are hidden files, but I haven't gotten to the bottom of that yet.
P.s. Delphi's DeleteFile() should also recognize forks.
The definitive solution is to allow the InfoTech protocol to work in the intranet zone.
Add the following value to the registry and the problem should be solved:
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\HTMLHelp\1.x\ItssRestrictions]
"MaxAllowedZone"=dword:00000001
More info here: http://support.microsoft.com/kb/896054
Go to Start
Type regsvr32 hhctrl.ocx
You should get a success message like:
" DllRegisterServer in hhctrl.ocx succeeded "
Now try to open your CHM file again.
other way is to use different third party software. This link shows more third party software to view chm files...
I tried with SumatraPDF and it work fine.
I fixed this programmatically in my software, using C++ Builder.
Before I assign the CHM help file, Application->HelpFile = HelpFileName, I check to see if it contains the "Zone.Identifier" stream, and when it does, I simply remove it.
String ZIStream(HelpFileName + ":Zone.Identifier") ;
if (FileExists(ZIStream))
{ DeleteFile(ZIStream) ; }
There are apparently different levels of authentication. Most articles I read tell you to set the MaxAllowedZone to '1' which means that local machine zone and intranet zone are allowed but '4' allows access for 'all' zones.
For more info, read this article:
https://support.microsoft.com/en-us/kb/892675
This is how my registry looks (I wasn't sure it would work with the wild cards but it seems to work for me):
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\HTMLHelp]
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\HTMLHelp\1.x]
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\HTMLHelp\1.x\ItssRestrictions]
"MaxAllowedZone"=dword:00000004
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\HTMLHelp\1.x\ItssRestrictions]
"UrlAllowList"="\\\\<network_path_root>;\\\\<network_path_root>\*;\\ies-inc.local;http://www.*;http://*;https://www.*;https://*;"
As an additional note, weirdly the "UrlAllowList" key was required to make this work on another PC but not my test one. It's probably not required at all but when I added it, it fixed the problem. The user may have not closed the original file or something like that. So just a consideration. I suggest try the least and test it, then add if needed. Once you confirm, you can deploy if needed. Good Luck!
Edit: P.S. Another method that worked was mapping the path to the network locally by using mklink /d (symbolic linking in Windows 7 or newer) but mapping a network drive letter (Z: for testing) did not work. Just food for thought and I did not have to 'Unblock' any files. Also the accepted 'Solution' did not resolve the issue for me.
Moving to local folder is the quickest solution, nothing else worked for me esp because I was not admin on my system (can't edit registery etc), which is a typical case in a work environment.
Create a folder in C:\help drive, lets call it help and copy the files there and open.
Do not copy to mydocuments or anywhere else, those locations are usually on network drive in office setup and will not work.

Programmatically passing # in URL to a browser

I need to pass the character # as part of the URL to the browser and open it programatically. When I do the following:
google-chrome path_to_some_file.pdf#view=Fit
The opened page on the browser is path_to_some_file.pdf%23view=Fit, which is not the intended URL. When I manually delete %23 and type # in the address bar of the browser, then it works. How can I pass the character # to a browser programmatically?
You need to specify a fully qualified file:/// URL in order to include ? query or # hash strings.
More details:
The following approach works in IE10, Firefox 28, and Chrome 36.
If you are doing this from a batch file in Windows with any of those browsers, you can use backward slashes in the path as long as you prefix with file:///. E.g., file:///C:/blah/blah/file.pdf#etc is the proper URL, but if yours ends up formatted file:///C:\blah\blah\file.pdf#etc, that will work too.
Here is how you can get the full path using a batch file.
file:///%CD%/file.pdf#blah
will resolve to a file relative to the working directory (usually the batch file's directory unlike explicitly changed via command prompt or programmatically).
file:///%~dp0/file.pdf#blah
will resolve to a file relative to the batch file's directory. I usually go with this.
You can use ../ to navigate up relative to the batch file's directory. The resulting URL should still work fine.
If you are using something a bit more robust than batch files, you can translate all the \s into /s to create a proper URL.
E.g., in .NET, I think you can do new Uri("C:\blah\file.pdf"), and it'll give you a file URI (I think, not sure), which you can then grab and append the hash onto it.

how to let ruby call default browser to open localfile

In this question, I find that using system('start http://www.google.com') is OK. If the file is in local disk, though, using system('start file:///c:/temp/a.html') doesn't work. How do I have Ruby get the default browser to open a local file?
What do you get when you double click a .html file in Windows Explorer? If it isn't the browser then that is your problem. The 'start' keyword pushes the path through the ShellExecute function, for http:// URLs is knows to open that in a browser, if it is a file it depends on the extension of the file, if your system has .html pointing to notepad for example (because in the past you have set it to notepad) it is always going to open it in that program unless you go and specifically change it.
From a generic work around point of view there is not much you can do, if you can access the Windows registry under Ruby then you can query the HKEY_CLASSES_ROOT\http\shell\open\command default value which contains a command line for the current browser bound to the HTTP protocol, you could use that to construct a full path (replace the %1 with the URL string).

Resources