Reputation: 2662
How to open file in its default application using Ruby scripts?
Let us say, I have folder with files .txt .doc .rb and I would like open them with Notepad, Word, and RubyMine respectively. I assume that all files have their default applications to open.
Upvotes: 4
Views: 4778
Reputation: 3899
If you are happy to add a dependency, there is a gem launchy.
Launchy.open(path_or_uri)
Upvotes: 0
Reputation: 1
On Windows:
UI.openURL(filepath)
On MacOS:
system %{open '#{filepath}'}
Upvotes: 0
Reputation: 1039
The best way to do this is to use the OS gem
require 'os'
OS.open_file_command
=> "start" # or open on mac, or xdg-open on linux (all designed to open a file)
Upvotes: 3
Reputation: 96934
This should work (untested, as I'm not on a Windows machine now):
file_to_open = "c:\path\to\file.txt"
system %{cmd /c "start #{file_to_open}"}
For reference, this can also be done on OS X:
file_to_open = "/path/to/file.txt"
system %{open "#{file_to_open}"}
Upvotes: 8