Code Snippetsopen URL a in variable browser Syndicate content

Mon, 03/12/2007 - 20:44

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:

  • to open a new tab:

    • firefox_new_tab
    • swiftfox_new_tab
    • konqueror_new_tab
    • opera_new_tab
  • to open a new window
    note: these functions will always be successful (if you have the application installed), thus it does not make sense to use them before any *_new_tab.

    • firefox_new_win
    • swiftfox_new_win
    • konqueror_new_win
    • opera_new_win
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
  1. #!/bin/bash
  2.  
  3. # the order in which to execute the commands
  4. # breaks on success
  5. # available are the following commands:
  6. #
  7. # swiftfox_new_tab
  8. # firefox_new_tab
  9. # konqueror_new_tab
  10. # opera_new_tab
  11. #
  12. # swiftfox_new_win
  13. # firefox_new_win
  14. # konqueror_new_win
  15. # opera_new_win
  16. #
  17. order=(
  18. swiftfox_new_tab
  19. firefox_new_tab
  20. opera_new_tab
  21. konqueror_new_tab
  22. konqueror_new_win
  23. )
  24.  
  25. # nothing should be changed below
  26.  
  27. # new konqueror tab
  28. function konqueror_new_tab {
  29. pid=(`pidof konqueror`)
  30. if [ ${pid[0]} ]; then
  31. if [ `dcop konqueror-$pid konqueror-mainwindow#1 hidden` == "true" ]; then
  32. dcop konqueror-$pid konqueror-mainwindow#1 show
  33. fi
  34. dcop konqueror-$pid konqueror-mainwindow#1 newTab "$1"
  35. else
  36. return 1
  37. fi
  38. }
  39. # new konqueror window
  40. function konqueror_new_win {
  41. konqueror "$1"&
  42. }
  43. # helper function for firefox_new_tab and swiftfox_new_tab
  44. function mozlike_new_tab {
  45. if [ "`pidof $2-bin`" ]; then
  46. $2 -new-tab "$1"&
  47. else
  48. return 1
  49. fi
  50. }
  51. # new firefox tab
  52. function firefox_new_tab {
  53. mozlike_new_tab $1 firefox
  54. }
  55. # new swiftfox tab
  56. function swiftfox_new_tab {
  57. mozlike_new_tab $1 swiftfox
  58. }
  59. # helper function for firefox_new_win and swiftfox_new_win
  60. function mozlike_new_win {
  61. $2 -new-tab "$1"&
  62. }
  63. # new firefox window
  64. function firefox_new_win {
  65. mozlike_new_win $1 firefox
  66. }
  67. # new swiftfox window
  68. function swiftfox_new_win {
  69. mozlike_new_win $1 swiftfox
  70. }
  71. # new opera tab
  72. function opera_new_tab {
  73. if [ "`pidof opera`" == "" ]; then
  74. return 1
  75. fi
  76. opera -newpage $1
  77. }
  78. # new opera win
  79. function opera_new_win {
  80. opera -newwindow $1
  81. }
  82.  
  83. # call functions based on order defined above
  84. for i in `seq 1 ${#order[@]}`;
  85. do
  86. ${order[$i-1]} $1 && exit
  87. 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):

  1. open_url %u

Comments

dcop has been removed in KDE Tue, 03/23/2010 - 22:03 — Anonymous (not verified)

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

Good script, very useful. Sun, 05/25/2008 - 21:33 — Anonymous (not verified)

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

Lovely Wed, 10/03/2007 - 10:49 — Rolando (not verified)

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!

Sun, 07/22/2007 - 20:20 — Eric Davis (not verified)

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

Post new comment

  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <pre>.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options