Wednesday, March 4, 2020

Open a url selected from anywhere on your desktop

I'm sure something like this exists in gnome/kde, but there is nothing for fvwm.

The idea is quite simple: you select a url in a text editor, a terminal emulator or whatever, press a kbd shortcut & your default browser opens a new tab w/ it.

$ cat xprimary-xdg-open
#!/bin/sh
# open up to 5 urls from the 1st 10K of the current selection via xdg-open

alert() { xmessage -center -button ok -default ok -timeout 2 "$*"; }

uri='^https?://...'
idx=0; for line in `xsel | head -c 10240 | tr '[:space:]' '\n' | egrep -a "$uri" | head -5 | sort -u`; do
idx=1
xdg-open "$line" || alert Failed to xdg-open \`"$line"\` &
done

[ $idx -eq 0 ] && alert The PRIMARY selection contains no urls!

(It requires xsel, xorg-x11-apps & xdg-utils Fedora packages.)

Why so long a script if something like "xsel | xargs xdg-open" should suffice?

  • it reports if a selection was empty
  • it reports an error if xdg-open was unable to open a browser
  • you can open multiple urls concurrently
  • there is some protection against junk in the selection

E.g.,

$ head -c $((1024*1024)) < /dev/urandom | xsel
$ ./xprimary-xdg-open

& you get a gui error message:

instead of "omg, what was that" on the stderr. xmessage draws not exactly the prettiest dialog boxes, but who cares.

The last "important" question here is what shortcut to choose? I decided upon Win-Shift-C, for in the past it opened the nonsensical "charms" menu in Windows (does nothing in 1909) & wretched Linux desktops, of course, have no charms.

For ~/.fvwm/.fvwm2rc:

Key c A S4 Exec exec xprimary-xdg-open

3 comments: