MW

open URL a in variable browser

I was recently annoyed by how URLs were started in KDE: I used an antiquated firefox command to run an URL in a new tab (firefox-remote "OpenURL(%u, new-window)") which does the job quite well. But: If you have no running firefox instance this command will simply do nothing. Right - nothing! You’ll have to start firefox manually and click once again on the link (in konversation, kopete or wherever).

But the newer firefox versions (I don’t know when this feature was added) support the firefox -newtab %u command. This is all you need if you only use firefox. Configure KDE via kcontrol to use this command as a standard webbrowser.

This was still not enough for me though, as I regularly switch to konqueror or opera, because firefox is sometimes to slow for a quick browse. This is what I came up with:

open_url script

what it does

Via open_url it is possible to use one of the currently open browsers. If you for example have a running konqueror instance but no firefox it is more likely (in my point of view) to open a new URL in konqueror instead of launching a new firefox instance (which would take a long time).

open_url goes through the commands in your specified order and executes them and exits on success. The following commands are available:

example

The default order (see below) will do the following:

Open a new tab in swiftfox if it’s running. Else open a new tab in firefox if this one is running. Else do the same for opera or konqueror . If none of these is running finally open a new konqueror window with the given URL.

the source code

    #!/bin/bash
     
    # the order in which to execute the commands
    # breaks on success
    # available are the following commands:
    #
    # swiftfox_new_tab
    # firefox_new_tab
    # konqueror_new_tab
    # opera_new_tab
    #
    # swiftfox_new_win
    # firefox_new_win
    # konqueror_new_win
    # opera_new_win
    #
    order=(
      swiftfox_new_tab
      firefox_new_tab
      opera_new_tab
      konqueror_new_tab
      konqueror_new_win
    )
     
    # nothing should be changed below
     
    # new konqueror tab
    function konqueror_new_tab {
      pid=(`pidof konqueror`)
      if [ ${pid[0]} ]; then
        if [ `dcop konqueror-$pid konqueror-mainwindow#1 hidden` == "true" ]; then
          dcop konqueror-$pid konqueror-mainwindow#1 show
        fi
        dcop konqueror-$pid konqueror-mainwindow#1 newTab "$1"
      else
        return 1
      fi
    }
    # new konqueror window
    function konqueror_new_win {
      konqueror "$1"&
    }
    # helper function for firefox_new_tab and swiftfox_new_tab
    function mozlike_new_tab {
      if [ "`pidof $2-bin`" ]; then
        $2 -new-tab "$1"&
      else
        return 1
      fi
    }
    # new firefox tab
    function firefox_new_tab {
      mozlike_new_tab $1 firefox
    }
    # new swiftfox tab
    function swiftfox_new_tab {
      mozlike_new_tab $1 swiftfox
    }
    # helper function for firefox_new_win and swiftfox_new_win
    function mozlike_new_win {
      $2 -new-tab "$1"&
    }
    # new firefox window
    function firefox_new_win {
      mozlike_new_win $1 firefox
    }
    # new swiftfox window
    function swiftfox_new_win {
      mozlike_new_win $1 swiftfox
    }
    # new opera tab
    function opera_new_tab {
      if [ "`pidof opera`" == "" ]; then
        return 1
      fi
      opera -newpage $1
    }
    # new opera win
    function opera_new_win {
      opera -newwindow $1
    }
     
    # call functions based on order defined above
    for i in `seq 1 ${#order[@]}`;
    do
      ${order[$i-1]} $1 && exit
    done

how to install it

Copy and paste the code to a new file and save it under a name of your choice. I’ll use open_url as the filename from now on - substitute it with yours.

Now make the script executable, e.g. via konqueror right-click or via the command line: chmod +x open_url. That’s all.

how to configure it

You can edit the order in which commands are executed by editing the order (well, that’s kinda obvious :) ). See line 3 and below for an explanation.

how to use it

Finally you have to tell KDE to use this script for URL handling, do this in kcontrol under components / standard programs. The webbrowser should execute the following line (assuming the script is located in your $PATH):

    open_url %u

Comments

Want to comment? Send me an email!

Comment by Anonymous (not verified) (2010-03-23 22:03:00)

dcop has been removed in KDE 4. From KDE forums (dunno about the hide/show stuff, that’s probably not essential, though): qdbus qdbus | grep -m1 konqueror /konqueror/MainWindow_1 org.kde.Konqueror.MainWindow.newTab ” False

Thread is http://forum.kde.org/viewtopic.php?f=14&t=32104

Comment by Anonymous (not verified) (2008-05-25 21:33:00)

Good script, very useful. They should add a flag option in console to konqueror to open a new tab.

Comment by Rolando (not verified) (2007-10-03 10:49:00)

I was just looking for a way to open pages in a new tab in konqueror, and one Google search later I landed here. And it works perfectly.

Thanks!

Comment by Eric Davis (not verified) (2007-07-22 20:20:00)

This worked just fine on my Kubuntu system. I changed the order so Konqueror is first and now I can open urls from any application in a new Konqueror tab.

Thanks

Published on July 22, 2007.