开发者

Posixy way to launch browser?

开发者 https://www.devze.com 2022-12-14 21:00 出处:网络
Is there a \'Posixy\' way to open an URL, preferrably in the default browser? I would l开发者_JAVA技巧ike to do something like

Is there a 'Posixy' way to open an URL, preferrably in the default browser? I would l开发者_JAVA技巧ike to do something like

ShellExecute(0, _T("open"), url, 0, 0, SW_SHOWDEFAULT);

that works on GNU/Linux and MAC. I read some answer saying that`

if (fork() == 0)
    system("sensible-browser http://wherever.com");

does the trick on Debian systems at least. Is there an easy way to extend this to something that works on other distributions and Mac OS X?


A user's browser preference is not really something the POSIX standard concerns itself with that I'm aware of.


On Linux you should try launching your URL with the xdg-open command. Newer distributions should launch the user's preferred browser when that command is called. It'll even try to launch links if there's no GUI running.

Obviously there's nothing in POSIX with respect to a GUI desktop environment. Projects like freedesktop.org exist to fill that role. It attempts to define some basic functionality that will work in all Unix-like desktops, such as GNOME and KDE.

Doing fork() and calling system(), instead of exec() is undefined behavior on POSIX, so I don't recommend that.


On a Mac, you can just use the open command. open http://www.google.com from the Terminal opens a new Chrome tab for me. Just wrap that up in a system call.


One way might be the $BROWSER variable. Xorg provides xrdb, which is the preffered way for this environment, but this wont get you anywhere on Mac OS X, I assume.


There’s no portable way to achieve this. On Mac OS X the solution would be to use LSOpenCFURLRef or other LaunchServices API to open the URL with the default handler for the URL scheme. Other platforms will do things differently.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号