Mark Szymanski
Mark Szymanski

Reputation: 58060

Open local HTML file in default web browser

IS there any way in Ruby to open a local HTML file in the user's default web browser? I could do something like:

system("open /path/to/file.html")

But that would only work on Mac OS X. Are there any solutions that work on any platform?

Upvotes: 3

Views: 4911

Answers (3)

htanata
htanata

Reputation: 36944

You can use the launchy gem.

First, install the gem:

$ [sudo] gem install launchy

Then, in your ruby code:

require 'rubygems'
require 'launchy'

Launchy::Browser.run("/path/to/file.html")

Upvotes: 9

Mike Lewis
Mike Lewis

Reputation: 64137

I know it's not desirable, however worst case, you can check the value of RUBY_PLATFORM. This returns the operating system platform.

Semi-Pseudo Code:

cmd = case RUBY_PLATFORM
  when /darwin/
    "open /path/to/file.html"
  when /windows/ #fix this, I'm not sure.
    "explorer file:///C:/path_to_file"
  else
    "default"
end

system(cmd)

Upvotes: 0

jwwishart
jwwishart

Reputation: 2895

I think you will have to do system specific calls.

system() is like writing the command on the local system command line (that's my understanding anywayz)

I can't do what you have done on Windows 7. I have to call explorer and it opens in my default browser.

Windows 7 Example: (opens in Chrome, my default browser)

system("explorer file:///C:/path_to_file")

Note: I needed to put file:/// at the start otherwise it opens in Explorer instead of the browser.

Upvotes: 0

Related Questions