How to Open File in Default Application. Ruby

How to open file in default application. Ruby

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}"}

how to open default application file with ruby?

Give a try to the "launchy" gem

Install the gem:

$ gem install launchy

Then in your Ruby program do the following:

require 'rubygems'
require 'launchy'

Launchy.open("/path/to/image.png")

Opening a Ruby program's source file in the default editor

The canonical way to open a file with the associated program is different from one operating system (or shell) to another. Here are 4 examples that will work on the specified OSes:

Opening the File

Windows

Use the standard command shell start, available beginning with Windows 95:

system %{cmd /c "start #{file_to_open}"}

Mac OS X

Use the standard open command:

system %{open "#{file_to_open}"}

Linux/Unix Gnome

Use the Gnome utility gnome-open:

system %{gnome-open "#{file_to_open}"}

Linux/Unix

Use the xdg-open utility:

system %{xdg-open "#{file_to_open}"}

Associated Programs

Detecting the associated program for a file type can be a fairly tall order on any one system. For instance, with Windows, you have to inspect the registry, whereas with Mac OS X you have to read the right plist values; Linux is a whole other world, altogether.

You're likely better off simply requiring the user to have a program associated to make things easy enough to get started. Once you have the other logic working in your application, you might be able to add interesting features like fallback to a default application in the absence of an existing file association.

How do I automatically open the file I created?

You can make a system call.

This is an example with Windows:

filename = "the_list.txt"
File.open(filename, "w"){|file|
file << "Some data\n"
}
`call notepad #{filename}`

This calls Notepad with the given filename.

Some variants to call an external program are:

`notepad #{filename}`
system( "notepad #{filename}")
system( "call notepad #{filename}")
%x{call notepad #{filename}}

You even don't need to add notepad:

%x{call #{filename}}

This depend on the main application, which is assigned to the extension of the file you create.

When you tell which system and which editor you need, more details are possible.

Another possibility:

require 'open3'
Open3.popen3("call #{filename}")
#or:
#Open3.popen3("call notepad #{filename}")

The advantage is the main program does not wait until the subprocess ends.

Variant as script: Store the following code as "file_build.rb".

filename = ARGV.first
File.open(filename, "w"){|file|
file << "Some data\n"
}

require 'open3'
puts "Call Editor"
Open3.popen3("call notepad #{filename}")
puts "End of script"

Now you can call file_build.rb test.txt. test.txt is created, an editor is called and the script closes. The editor keeps running, at least it did in my test (WinXP).

open file that has a space (or &) in its name, with default app

MANAGE SPACE

system('cmd /c start "" "C:\Users\kevin\Documents\LIN KED.png"')

I read here superuser.com/questions/511486/…

The first "" are for the shell window name and the second "" are for the command (parameter)

MANAGE &

Wasn't necessary when I used the line above.

In other cases, try adding a '^' before the & symbols

system('cmd /c start "" "C:\Users\kevin\Documents\LIN^&KED.png"')

from : How do I escape ampersands in batch files?

What does it mean to open a file in Ruby by using File.new?

The File.new method executes a call to IO::new (docs here).

The thing being "opened" in this case is an input/output stream which Ruby tracks using file descriptors. These file descriptors can be expensive to keep around which is why its good practice to call the close method on any instances of File or IO.



Related Topics



Leave a reply



Submit