I have an application that launches a webpage in the "current" browser when the user selects it. This part of my app works fine in the Windows version but I can't figure out how to do this in Linux build.
Right now the Linux version is hardcoded for Firefox in a specific directory and runs a new instance of it each time and doesn't show the URL that I pass in. I would like it to NOT launch a new version each time but just open a new page in the current open one if it is already running.
For windows I use:
ShellExecute(NULL,"open",filename,NULL,NULL,SW_SHOWNORMAL);
For Linux I currently use:
pid_t pid;
char *args[2];
char *prog=0;
char firefox[]={"/usr/bin/firefox"};
if(strstri(filename,".html"))
prog=firefox;
if(prog)
{
args[0]=(char *)filename;
args[1]=0;
pid=fork();
if(!pid)
execvp(prog,args);
}
xdg-open opens a file or URL in the user's preferred application. If a URL is provided the URL will be opened in the user's preferred web browser. gnome-open works as well as xdg-open but neither knows what to do with naked domain. So gnome-open http://askubuntu.com works but not gnome-open askubuntu.com .
If you're writing this for modern distros, you can use xdg-open
:
$ xdg-open http://google.com/
If you're on an older version you'll have to use a desktop-specific command like gnome-open
or exo-open
.
xdg-open is the new standard, and you should use it when possible. However, if the distro is more than a few years old, it may not exist, and alternative mechanisms include $BROWSER (older attempted standard), gnome-open (Gnome), kfmclient exec (KDE), exo-open (Xfce), or parsing mailcap yourself (the text/html handler will be likely be a browser).
That being said, most applications don't bother with that much work -- if they're built for a particular environment, they use that environment's launch mechanisms. For example, Gnome has gnome_url_show, KDE has KRun, most terminal programs (for example, mutt) parse mailcap, etc. Hardcoding a browser and allowing the distributor or user to override the default is common too.
I don't suggest hardcoding this, but if you really want to open a new tab in Firefox, you can use "firefox -new-tab $URL".
A note for xdg-open: check http://portland.freedesktop.org/wiki/ , section "Using Xdg-utils"; it states that you can include the xdg-open script in your own application and use that as fallback in case the target system doesn't have xdg-open already installed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With