Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open file in default application. Ruby

Tags:

file

ruby

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.

like image 232
Jackie Chan Avatar asked Dec 28 '22 05:12

Jackie Chan


2 Answers

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}"}
like image 128
Andrew Marshall Avatar answered Jan 18 '23 18:01

Andrew Marshall


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)
like image 33
RedFred Avatar answered Jan 18 '23 18:01

RedFred