Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2008-12

Open URL in "default" browser X-platform

12 messages · 7 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Bart

Hi,

I come from Windows/Delphi (3.0) background and I'm gradually moving towards Lazarus/fpc.

Opening an URL in Windows is a piece of cake, you just let the OS take care of the assotiations with the http protocol to open it (using shellexecute).
This of course will not do for other platforms.

So I set myself the task of launching an URL in an external browser on other platforms.
Since I only have Linux (besides Win) I began with that.

I tried to find examples on the net, but had no luck. I might well be re-inventing the wheel though...

Here's my approach:

- try to detect the windowmanger (for now I know how to do this for KDE and GNOME, XFCE is on my whishlist)

On KDE
- lookup DESKTOK_LAUNCH environment variable
- if not there, look for kde-open
- if not there look for kfmclient
- if not there look for xdg-open
- if not there try fallback
(possibly using gnome-open might also work under KDE ???)

On GNOME
- look fo gnome-open
- if not there, look for xdg-open
- if not there try fallback

Fallback
- search for know browsers (firefox, iceweasel, mozilla, opera, konqueror, ...(suggestions are welcome))

If browser (or URL handler like kde-open, xdg-open etc.) is found launch it with the appropriate URL.

This approach (the finding mechanism) seems to work at least on my Suse KDE desktop.

I would like your opinions on this approach, and better still, I would like some suggestions how to achieve something similar on a Mac. (I suppose on Mac OSX the linux approach should work also???)

And as a final rather dumb question: do all supported OS's have an environmentvariable PATH (as this is used in the fallback mechanism)?

Bart

Reply by email

Mac Programmer

Here's what I use:

{$IFDEF LINUX}
function SearchForBrowser(const BrowserFileName : string) : string;
  {Search path for specified browser file name, returning
    its expanded file name that includes path to it.}
begin
   Result :=
    SearchFileInPath(BrowserFileName, '', GetEnvironmentVariable
('PATH'),
                     PathSeparator, [sffDontSearchInBasePath]);
end;
function GetBrowserPath : string;
  {Return path to first browser found.}
begin
   Result := SearchForBrowser('firefox');
   if Result = '' then
     Result := SearchForBrowser('konqueror');  {KDE browser}
   if Result = '' then
     Result := SearchForBrowser('epiphany');  {GNOME browser}
   if Result = '' then
     Result := SearchForBrowser('mozilla');
   if Result = '' then
     Result := SearchForBrowser('opera');
end;
{$ENDIF}

You can use Shell(GetBrowserPath + ' ' + URL) although some frown on that since Shell is deprecated, but I still use it.

With OS X, it's very easy: Shell('Open ' + URL) -- this will open the URL in the default browser, so no need to search for it and in fact you don't want to search for it.

Thanks.

-Phil

Reply by email

Michael Schneider
to Bart: I come from Windows/Delphi (3.0) background and I'm gradually moving towards Lazarus/fpc.…
Bart wrote
- try to detect the windowmanger (for now I know how to do this for KDE and GNOME, XFCE is on my whishlist)

parse the Processlist for:

gnome-session : Gnome
ksmserver :KDE3
sfce-mcs-manager:XFCE

Windowmanager:

the same
eg. kwin in the Processlist: Kwin
metacity,fluxbox,xfcm4..........

You can take a look at the script to:

http://bbs.archlinux.org/viewtopic.php?id=24208&p=1

Reply by email

ik
to Bart: I come from Windows/Delphi (3.0) background and I'm gradually moving towards Lazarus/fpc.…

On Linux you can use the alternatives to decide what browser to open. On debian (I don't know about non debian based distro) you can use the sensible-browser command.
It points to the default web browser.

Ido

http://ik.homelinux.org/

Bart wrote 58 lines
Hi, I come from Windows/Delphi (3.0) background and I'm gradually moving towards Lazarus/fpc. Opening an URL in Windows is a piece of cake, you just let the OS take care of the assotiations with the http protocol to open it (using shellexecute). This of course will not do for other platforms. So I set myself the task of launching an URL in an external browser on other platforms. Since I only have Linux (besides Win) I began with that. I tried to find examples on the net, but had no luck. I might well be re-inventing the wheel though... Here's my approach: - try to detect the windowmanger (for now I know how to do this for KDE and GNOME, XFCE is on my whishlist) On KDE - lookup DESKTOK_LAUNCH environment variable - if not there, look for kde-open - if not there look for kfmclient - if not there look for xdg-open - if not there try fallback (possibly using gnome-open might also work under KDE ???) On GNOME - look fo gnome-open - if not there, look for xdg-open - if not there try fallback Fallback - search for know browsers (firefox, iceweasel, mozilla, opera, konqueror, ...(suggestions are welcome)) If browser (or URL handler like kde-open, xdg-open etc.) is found launch it with the appropriate URL. This approach (the finding mechanism) seems to work at least on my Suse KDE desktop. I would like your opinions on this approach, and better still, I would like some suggestions how to achieve something similar on a Mac. (I suppose on Mac OSX the linux approach should work also???) And as a final rather dumb question: do all supported OS's have an environmentvariable PATH (as this is used in the fallback mechanism)? Bart _______________________________________________ Lazarus mailing list http://www.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.lazarus.freepascal.org/pipermail/lazarus/attachments/20081204/90d23c55/attachment-0001.html

Reply by email

Graeme Geldenhuys
to Michael Schneider: parse the Processlist for: gnome-session : Gnome ksmserver :KDE3 sfce-mcs-manager:XFCE…
Michael Schneider wrote
You can take a look at the script to: http://bbs.archlinux.org/viewtopic.php?id=24208&p=1

Interesting idea and I'm sure I can adapt it to a Free Pascal program. Unfortunately I couldn't get any of those scripts working on my system (Ubuntu 7.10). :-(

Regards,
- Graeme -

_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/

Reply by email

Mark Morgan Lloyd
to Bart: I come from Windows/Delphi (3.0) background and I'm gradually moving towards Lazarus/fpc.…
Bart wrote 51 lines
Hi, I come from Windows/Delphi (3.0) background and I'm gradually moving towards Lazarus/fpc. Opening an URL in Windows is a piece of cake, you just let the OS take care of the assotiations with the http protocol to open it (using shellexecute). This of course will not do for other platforms. So I set myself the task of launching an URL in an external browser on other platforms. Since I only have Linux (besides Win) I began with that. I tried to find examples on the net, but had no luck. I might well be re-inventing the wheel though... Here's my approach: - try to detect the windowmanger (for now I know how to do this for KDE and GNOME, XFCE is on my whishlist) On KDE - lookup DESKTOK_LAUNCH environment variable - if not there, look for kde-open - if not there look for kfmclient - if not there look for xdg-open - if not there try fallback (possibly using gnome-open might also work under KDE ???) On GNOME - look fo gnome-open - if not there, look for xdg-open - if not there try fallback Fallback - search for know browsers (firefox, iceweasel, mozilla, opera, konqueror, ...(suggestions are welcome)) If browser (or URL handler like kde-open, xdg-open etc.) is found launch it with the appropriate URL. This approach (the finding mechanism) seems to work at least on my Suse KDE desktop. I would like your opinions on this approach, and better still, I would like some suggestions how to achieve something similar on a Mac. (I suppose on Mac OSX the linux approach should work also???) And as a final rather dumb question: do all supported OS's have an environmentvariable PATH (as this is used in the fallback mechanism)?

There's already a standard for that: xdg-open. Granted that a fallback for older distreaux that don't include it is useful, but apart from that rather than trying to reinvent the wheel your time would be better spent cooperating with the people who've implemented it- I'm sure they'd appreciate your help.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Michael Schneider
to Graeme Geldenhuys: Interesting idea and I'm sure I can adapt it to a Free Pascal program. Unfortunately I…
Show quoted text 5 lines
Interesting idea and I'm sure I can adapt it to a Free Pascal program. Unfortunately I couldn't get any of those scripts working on my system (Ubuntu 7.10). :-(

Q 'n D approach
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Archiv.tar.gz
Type: application/x-tgz
Size: 3398 bytes
Desc: not available
Url : http://www.lazarus.freepascal.org/pipermail/lazarus/attachments/20081205/f116e279/attachment.bin

Reply by email

Graeme Geldenhuys
to Michael Schneider

2008/12/5 Michael Schneider :

Q 'n D approach

You read my mind! Once I figure out how to get the scripts to work, I was going to port that to a Free Pascal application. I actually want to integrate it into fpGUI, to detect some theme settings under Linux/FreeBSD, so fpGUI apps can blend in a bit better.

Your code just saved me some work. Thanks. :-)

Regards,
- Graeme -

_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/

Reply by email

Graeme Geldenhuys
to Michael Schneider: Q 'n D approach -------------- next part -------------- A non-text attachment was…

2008/12/5 Michael Schneider :

Show quoted text 5 lines
> Interesting idea and I'm sure I can adapt it to a Free Pascal program. > Unfortunately I couldn't get any of those scripts working on my system > (Ubuntu 7.10). :-( > Q 'n D approach

BTW: That application was seriously full of memory leaks. ;-)

Attached is a new version, free of memory leaks including a few bug fixes. I have tested it successfully under KDE, Gnome, FVWM and XFCE.

Regards,
- Graeme -

_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: sysinfo.tar.gz
Type: application/x-gzip
Size: 3395 bytes
Desc: not available
Url : http://www.lazarus.freepascal.org/pipermail/lazarus/attachments/20081205/fbe3a602/attachment.gz

Reply by email

Michael Schneider
to Graeme Geldenhuys
Graeme Geldenhuys wrote 9 lines
BTW: That application was seriously full of memory leaks. ;-) Attached is a new version, free of memory leaks including a few bug fixes. I have tested it successfully under KDE, Gnome, FVWM and XFCE. Regards, - Graeme -

Sorry,was just a quick and dirty hack . ;)
I hope it's usefull for your fpGUI Project.

Thanks for looking over the code.

Reply by email

Bart
to Michael Schneider

Hi there,

Thank you all very much for your input.
I cooked up some code that kind of works AFAICS.
Only tested on my Suse for now, will test under Win9x some other time.

Some questions of a more general nature remain:

1) When I execute the browser, using TProcess.Execute, how do I know if the process started OK (that is the executable started up at least)?

2) I found out that if you try to execute a file which does not have execute permission, the app gets killed, even if this a try..except block.
"Xlib: unexpected async reply (sequence 0x4cf)!"
Is this normal linux behaviour (I'm a newbie)?

If someone wants to see what I came up with and even test it under different operating systems and desktop environments, I'll put the unit on my website.

Bart

Reply by email