Invoking browser from Ruby script with URL argument: How?

35 views
Skip to first unread message

RichardOnRails

unread,
Aug 8, 2011, 2:10:57 PM8/8/11
to Ruby on Rails: Talk
Hi,

I've got the folowing script:
require "fileutils"

path = "K:/_Utilities/Apache/xampp/"
FileUtils::cd path
names = Dir.glob("**/index.html")
(0..4).each { |i|
name = names[i]
address = "file://" + path + name
puts address
exec address
}

It runs fine without line 10, "exec address", and produces:
file://K:/_Utilities/Apache/xampp/apache/htdocs/index.html
file://K:/_Utilities/Apache/xampp/htdocs/index.html
file://K:/_Utilities/Apache/xampp/htdocs/restricted/index.html
file://K:/_Utilities/Apache/xampp/licenses/gd/index.html
file://K:/_Utilities/Apache/xampp/php/docs/Structures_Graph/docs/html/index.html

But with line 10 active, I hang with:
K:\>ruby K:\_Projects\Ruby\_Ruby_Tests\TestDirTraversal
\TestDirTraversal.rb
file://K:/_Utilities/Apache/xampp/apache/htdocs/index.html
K:/_Projects/Ruby/_Ruby_Tests/TestDirTraversal/TestDirTraversal.rb:
10:in `exec': Invalid argument - file://K:/_Utilities/Apache/xampp/apache/htdocs/in
dex.html (Errno::EINVAL)
from K:/_Projects/Ruby/_Ruby_Tests/TestDirTraversal/
TestDirTraversal.rb:10:in `block in <main>'
[snip]

Of course, if I pasted these addresses on successive lines of Word
(with space characters appended to each), I'd have a series of
hyperlinks that could control-click to invoke the respective URLs in
Firefox.

Is there a way I can repair the code here to achieve the equivalent
effect with Ruby?

Thanks in advance for any education you may offer,
Richard

7stud --

unread,
Aug 8, 2011, 3:28:11 PM8/8/11
to rubyonra...@googlegroups.com
exec([env,] command... [,options])
Replaces the current process by running the given external command

but

file://K:/_Utilities/Apache/xampp/apache/htdocs/index.html

isn't a command.

--
Posted via http://www.ruby-forum.com/.

7stud --

unread,
Aug 8, 2011, 4:21:23 PM8/8/11
to rubyonra...@googlegroups.com

Colin Law

unread,
Aug 8, 2011, 4:32:18 PM8/8/11
to rubyonra...@googlegroups.com
On 8 August 2011 19:10, RichardOnRails

<RichardDummy...@uscomputergurus.com> wrote:
> Hi,
>
> I've got the folowing script:
> require "fileutils"
>
> path = "K:/_Utilities/Apache/xampp/"
> FileUtils::cd path
> names = Dir.glob("**/index.html")
> (0..4).each { |i|
>      name = names[i]
>      address = "file://" + path + name
>      puts address
>      exec address

Possibly
exec "firefox #{address}"

Colin

RichardOnRails

unread,
Aug 8, 2011, 4:58:28 PM8/8/11
to Ruby on Rails: Talk
Hi 7-stud,

Long time no "see". Thanks for looking into my question.

> file://K:/_Utilities/Apache/xampp/apache/htdocs/index.html
>
> isn't a command.

Yeah, I know, but my question is how to repair my code so that it
works. Or more specifically, what command can I take which accepts
the text of a URL as an argument and invokes my browser with that
command as an argument?

Do you have a further idea? I greatly appreciate learning any other
idea you might have on the question.

Best wishes,
Richard

RichardOnRails

unread,
Aug 8, 2011, 5:08:48 PM8/8/11
to Ruby on Rails: Talk
Colin,

> exec "firefox #{address}"

You're my hero!! I couldn't think of what command I could use to
invoke the browser on my machine. I wish I had possessed enough
imagination to at least try that idea before posting my question. I
did harbor the fancy of poking through Rails source code, thinking I
might find a clue there. But that seemed daunting to me.

Best wishes,
Richard

On Aug 8, 4:32 pm, Colin Law <clan...@googlemail.com> wrote:
> On 8 August 2011 19:10, RichardOnRails
>

Frederick Cheung

unread,
Aug 8, 2011, 5:39:35 PM8/8/11
to Ruby on Rails: Talk


On Aug 8, 10:08 pm, RichardOnRails
<RichardDummyMailbox58...@USComputerGurus.com> wrote:
> Colin,
>
> > exec "firefox #{address}"
>

I don't think this would work on a max, since GUI apps aren't normally
in $PATH. You might be interested in the launchy gem.

Fred

RichardOnRails

unread,
Aug 8, 2011, 6:54:49 PM8/8/11
to Ruby on Rails: Talk
Hi Fred,

I'm happy our paths crossed again.

The launchy gem looks cool. The page I found it on also advertised
Process Explorer from SysInternals that replaces Window's
StartMenu ... something I've wanted for a long time. So, it's a
double-hitter.

Thanks and best wishes,
Richard

On Aug 8, 5:39 pm, Frederick Cheung <frederick.che...@gmail.com>
wrote:

7stud --

unread,
Aug 8, 2011, 7:32:40 PM8/8/11
to rubyonra...@googlegroups.com
RichardOnRails wrote in post #1015562:

> Hi 7-stud,
>
> Long time no "see". Thanks for looking into my question.
>
>> file://K:/_Utilities/Apache/xampp/apache/htdocs/index.html
>>
>> isn't a command.
>
> Yeah, I know, but my question is how to repair my code so that it
> works. Or more specifically, what command can I take which accepts
> the text of a URL as an argument and invokes my browser with that
> command as an argument?
>
> Do you have a further idea?
>

Well, I provided a link.

Jarin Udom

unread,
Aug 8, 2011, 8:25:19 PM8/8/11
to rubyonra...@googlegroups.com
On a Mac, you don't need launchy, just do:

open http://example.com/whatever

Frederick Cheung

unread,
Aug 9, 2011, 5:30:53 AM8/9/11
to Ruby on Rails: Talk


On Aug 9, 1:25 am, Jarin Udom <jarin.u...@gmail.com> wrote:
> On a Mac, you don't need launchy, just do:
>
> openhttp://example.com/whatever

You don't need launchy on any platform but it takes away from you the
hassle of deciding which strategy to use for a given environment.

Fred

RichardOnRails

unread,
Aug 9, 2011, 10:18:17 AM8/9/11
to Ruby on Rails: Talk
Hi again,

>> Do you have a further idea?
>
> Well, I provided a link.

Thanks for that link in your second post. I only received it about an
hour after your original response, which only pointed out that I
lacked a function reference. Hence my request for further idea(s).

Now I have a wealth of good solutions: Yours, Colin's and Fred's

Thanks to you and them,
Richard
Reply all
Reply to author
Forward
0 new messages