开发者

how to let ruby call default browser to open localfile

开发者 https://www.devze.com 2022-12-18 05:49 出处:网络
In this question, I find that using system(\'start http://www.go开发者_JAVA百科ogle.com\') is OK.If the file is in local disk, though, using system(\'start file:///c:/temp/a.html\') doesn\'t work. How

In this question, I find that using system('start http://www.go开发者_JAVA百科ogle.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).

0

精彩评论

暂无评论...
验证码 换一张
取 消