Archive for October, 2008

open-with for the command-line

Update 2008/11/18: Use xargs 😛

Here’s a bash script that you can pipe output into and tell it to run a specific program with the output as arguments. I’ve named it open-with and placed it in my personal script directory: /home/steve/bin/. Look within the script at a couple of the examples for how to use it.

#!/bin/bash
 
PROG=`basename $0`
 
DESC="Read arguments from standard input and run a specified program
with them.  Meant to be used as output for a pipe."
 
 
USAGE="OUTPUT | $PROG \"PROGRAM-NAME [OPTIONS AND ARGUMENTS]\""
 
EXAMPLES="
# List (with details), all the files that include \"downloads\" in their name:
  find /home/ -iname \"*downloads*\" | $PROG \"ls -l\"
 
# Queue all AVI files in current directory in vlc:
  ls *.avi | $PROG vlc
 
# View all time-related icons with eye-of-gnome:
  find /usr/share/icons/gnome/ -iregex \".*[^mul]time.*\" | open-with eog
"
 
function Usage() {
  echo "DESCRIPTION: $DESC" ; echo
  echo "USAGE: $USAGE" ; echo
  echo -n "EXAMPLES: $EXAMPLES"
}
 
# Quick check to see it's being called correctly, if not, print Usage and exit
if [ ${#@} -ne 1 ]
then
	Usage
	exit
fi
 
files=()						# empty array
while read -r					# read from stdin
do
	files+=( "$REPLY" )			# add result of read to array
done
 
# assume $1 is a valid program
$1 "${files[@]}"				# pass arguments to specified program

Motivation

You’re sitting at the command line and have a list of images (in different locations) that you would like to browse. Most image viewers let you iterate over a set of images, but only within the same directory. What would be great is if they accepted input from stdin through a pipe!

$ cat my_list_of_images | my_image_viewer

I didn’t find anything that did that, but using the open-with script, you can do something similar:

$ cat my_list_of_images | open-with my_image_viewer


Browsing a select list of images is actually kind of nice. It’s like a playlist for your image viewer – a viewlist. 🙂 Anyway, I’m sure there’s some problems with this script. Feel free to provide suggestions in the comments. But I certainly don’t want to look at it for a while….

I hate shell scripting

With a passion. It’s not a great surprise… many programmers do. I’m willing to go on record and state that I hate it even more than PERL programming. I rarely do it, and when I decide to try something that seems like it would be simple, it turns out taking forever due mostly to quirks. The rest of this post is a bit about how I went about writing this script, which ended up being mostly given to me by some #bash gurus on IRC. And it’s a bit of a rant.

Although not many GUI programs seem to accept stdin as input, most accept filenames as arguments:

$ eog image1.jpg image2.jpg image3.jpg "/home/steve/image seven.jpg"

So I figured I would just have to convert the list of images into an acceptable format: quoted, absolute filenames, separated by space. Doing this depends entirely on the format the list is currently in, but it’s likely a list of unquoted filenames separated by newlines:

/home/steve/image1.jpg
/home/steve/my images/wow.jpg
/tmp/anotherimage.png

For me, my test list looked like this:

/usr/share/icons/gnome/48x48/stock/generic/stock_timezone.png
/usr/share/icons/gnome/16x16/stock/generic/stock_timezone.png
/usr/share/icons/gnome/16x16/stock/generic/stock_timer.png
/usr/share/icons/gnome/16x16/stock/generic/stock_timer_stopped.png
/usr/share/icons/gnome/16x16/stock/form/stock_form-time-field.png
/usr/share/icons/gnome/24x24/stock/generic/stock_timezone.png
/usr/share/icons/gnome/24x24/stock/generic/stock_timer.png
/usr/share/icons/gnome/24x24/stock/generic/stock_timer_stopped.png
/usr/share/icons/gnome/24x24/stock/form/stock_form-time-field.png

I was looking for icons of clocks or representations of time, and I obtained that list using find:

$ find /usr/share/icons/gnome/ -iregex ".*[^mul]time.*"

So I can’t pipe it into my image viewer, but I can use the output as the command-line arguments if I change the newlines to spaces. find has an option for formatting the output which is perfect:

$ find /usr/share/icons/gnome/ -iregex ".*[^mul]time.*" -printf "'%p' "
'/usr/share/icons/gnome/48x48/stock/generic/stock_timezone.png' '/usr/share/icons/gnome/16x16/stock/generic/stock_timezone.png' '/usr/share/icons/gnome/16x16/stock/generic/stock_timer.png' '/usr/share/icons/gnome/16x16/stock/generic/stock_timer_stopped.png' '/usr/share/icons/gnome/16x16/stock/form/stock_form-time-field.png' '/usr/share/icons/gnome/24x24/stock/generic/stock_timezone.png' '/usr/share/icons/gnome/24x24/stock/generic/stock_timer.png' '/usr/share/icons/gnome/24x24/stock/generic/stock_timer_stopped.png' '/usr/share/icons/gnome/24x24/stock/form/stock_form-time-field.png'

That’s great for me, because I’m using find. But not very useful if I’m not, so I wanted something more generic. Of course, there are many ways to do this, and again, I’m by no means a command-line guru. But here’s how I started:

$ find /usr/share/icons/gnome/ -iregex ".*[^mul]time.*" | sed -e 's/^/"/' | sed -e :a -e '$!N;s/\n/" /; ta' | tr '\n' '"'

This wrapped the lines in double-quotes and join them together with a space in between. The find command is the same as before, minus the formatting because that’s what I was trying to find an alternative to. The output of find is piped into sed, which adds a " at the beginning of each line. This output is then sent to another sed which replaces the newline character at the end of each line with a closing double-quote and a space, joining all lines into a single line. Finally, that output is piped into tr which replaces the one remaining newline with a final double-quote.

If the files don’t include spaces or other troublesome characters (mine didn’t), then you could get away with simply changing the newlines into spaces. But again, I wanted something generic.

$ find /usr/share/icons/gnome/ -iregex ".*[^mul]time.*" | tr '\n' ' '

Anyway, now that we have something that creates the desired input, we just have to wrap it in back-ticks and put the whole mess as the argument to the image viewer! In my case, I’m using eye-of-gnome (or eog).

$ eog `find /usr/share/icons/gnome/ -iregex ".*[^mul]time.*" | sed -e 's/^/"/' | sed -e :a -e '$!N;s/\n/" /; ta' | tr '\n' '"'`

Wait a second. That doesn’t actually work. Why not? Running the backtick contents by itself seemed to produce the correct output. Copying this output verbatim as arguments to eog worked as expected. The problem was that when the quoted arguments were manually entered on the command-line, bash (silently) escapes the filenames, removing the quotes, adding backslashes before space,s etc. But when the pipeline is wrapped in backticks, the result is not escaped, and eog complains about not being able to find files that begin with quotes. Fine. I was not about to write a bash “escape” script – something like that should already exist, right? And it should be built in to bash! Perhaps it’s even called “escape“. Well, if there exists such a built-in, I couldn’t find it. But I had to be going about this the wrong way. Heading over to IRC, it was kind of difficult to explain the problem, but ferret eventually gave me the meat of the script above:

files=(); while read -r; do files+=( "$REPLY" ); done < <(find /usr/share/icons/gnome/ -iregex ".*[^mul]time.*"); eog "${files[@]}"

It worked! I thanked him and began studying it. There were still a couple things I didn’t understand:

  • Why two angle brackets with a space between them? I understand the first one is probably redirecting input, but I don’t understand that one next to the opening parenthesis.
    Answer: The <(command) actually puts command‘s output in a temporary file and produces that filename. So <(command) becomes tempfile. (Thanks, kojiro.)
  • If I open some video or audio files in totem using open-with, there’s an odd delay. Using vlc doesn’t produce a delay, however.

And to think this was originally going to be a micro-blog post in Twitter and identi.ca. Wow. It’s safe to say I still hate shell scripting. 🙂

What I did today – Time Tracking with hamster-applet

In GNOMEe 2.24, hamster-applet will be included by default. When I use it, I love it. I should use it more often.

I should note that “laundry” is actually inclusive to other room-cleaning activities. 🙂

WiiWare and WordPress Update

It was a nice fall day a couple days ago. I really enjoyed this sight, in my backyard. 🙂 Quite overcast and dull, the last couple of days, though.

Games

I updated my About page with my Tetris Party friend code. (Add me!!) It’s pretty fun, but I say that as someone who has never owned a Tetris game before, so I’m not a Tetris vet… (unlike Shirley and Alex, who think the DS game is far superior, apparently). My only real complaint with the game so far is the music – they could have done AWESOME things with it. I want a hoppin 8bit remix of these classic tunes… the included midi, even the classic stuff, is pretty dry and slow. Need something with a faster pace. The computer moves at seemingly impossible speeds at levels above 12, but I suspect that some people out there can play like that, as well. Scary. (Especially the top ranked in the Americas, Java AI … hmmm…) Anyway, I can’t beat level 13 yet. And I think I still like Dr Mario for VS more… so fun. … but Tetris Party offers tonnes of modes, including 4-player vs! That’s pretty cool. Maybe I’ll become a Tetris snob, yet.

While on the topic of Wii(Ware), I also got World of Goo. It’s a really great physics based puzzle title. I really love the dark and comical graphic design (reminds me of Tim Burton’s stuff). The music in Goo is epic, as well. I can’t believe everything was made by two guys (+1 for Wii optimizations); two guys in debt! They’re my heroes. Seriously. Both Tetris Party and World of Goo are the most expensive games on WiiWare ($12 US, $15 US, respectively), but I haven’t been disappointed. And with Goo especially, I don’t have any problem supporting the little guys making great stuff. That’s where I’d like to be. 🙂

WordPress

I updated my wp-upgrade script to display a big warning to deactivate all your plugins before continuing (wouldn’t want to damage your database!). Then I used it to update to WordPress 2.6.3. I think that makes me largely up-to-date.

New Shoes x 3

I got some good deals on new shoes. I’m not one to go shopping, but I happened to be in a shoe store (Shoe Warehouse in downtown Vancouver) and decided to walk over to the clearance section. Buying 3 pairs of shoes at once is NOT normal for me. But… I needed to replace both my casual and running shoes. I got the dress shoes because… well, I really wasn’t given a choice at that price. Saving money makes me happy. 😀

Regular Price and Sale Price are highlighted

Regular Price and Sale Price are highlighted

Photo of the shoes, as requested, minus the nudity. Be thankful.

Rhythmbox Plugin: Jump to Playing 0.3(.1)

Update 2012-01-29 – Deprecated:
Brief: This plugin has been deprecated. For future versions and updates, please go here.

Less Brief:If you’re using a newer version of Rhythmbox, you’ll need a newer version of this plugin. I’m not sure exactly which RB version the plugin format changed, but I am currently using Rhythmbox 2.90.1 on Ubuntu 11.10 and this plugin no longer works. However, Timo Loewe has ported jump-to-playing to Rhythmbox 3, all properly hosted and everything! Get it and any future updates here (https://github.com/dmo60/JumpToPlaying). This is the version I’m now using. Thanks, Timo! 😀

– – – – –

The following pertains to the deprecated version of the plugin…

– – – – –

This plugin will display the View : Jump to Playing Song link as a button in the toolbar and/or as link in the Browser’s context menu. Other Rhythmbox plugins can be found here.

Screenshots


Using a future version of Rhythmbox – patch here – the menu item will appear in a plugin placeholder, above Properties. Otherwise, it will appear at the bottom, like previous versions.


From version 0.3, the Open Folder plugin will also be placed in the plugin placeholder.

Changes Since 0.2

Just a couple small changes since 0.2.

  • Selecting the context menu option in the preferences will now display the link in PodcastView and PlaylistView popups, as well as BrowserView and QueuePlaylistView.
  • Assuming the patch on bug 557152 is applied, this will place the context menu items in a plugin placeholder and allow the Preferences to remain the last menu item.
  • Update (Nov 10 2008): Modified version of patch has been applied to RB development trunk, so the next version of Rhythmbox will have this update. Yay! Version 0.3.1 of jump-to-playing is to account for the modifications. Please use it. 🙂

Download

jump-to-playing-0.3.tar.gz jump-to-playing-0.3.1.tar.gz
Browse the Source: Here
Grab the Source: bzr branch "http://stevenbrown.ca/src/jump-to-playing/"

Installation

  1. Extract the jump-to-playing folder into your ~/.gnome2/rhythmbox/plugins/ directory (or ~/.local/share/rhythmbox/plugins/ directory, if the .gnome2 directory doesn’t exist). Completely replace any previous versions.
  2. (Re)Start Rhythmbox and enable the plugin in Edit : Plugins.

Todo

From my previous post.

  • the gconf keys in gconf-editor say they have no schema. The main plugins’ keys have a schema and don’t give a warning. Definitely not serious, though.
  • it currently adds/removes the ui string when the options are toggled in the configure dialog. I have a feeling it might be better to only add/remove them in the activation/deactivation, and just hide/show here. Maybe faster?
  • it currently hides the browser button in small display mode. That has nothing to do with the jump-to-playing button. That should be in core, if it was decided that was the desired behaviour.
  • to hide the buttons in the small display, it checks the value at activation, and it connects to the View menu’s toggleButton’s “toggled” signal. So whenever it’s toggled, the gconf value for the small display mode is checked, but I think there’s a delay sometimes. Pushing Ctrl D quickly a few times may result in incorrect UI presented. I remember deciding that this is due to a delay set on the gconf callback to overcome some other bug….

All patches are welcome! 🙂

Update 2008/10/26: Added screenshot, descriptions to screenshots, and link to main plugin page.

Update 2008/11/10: Added link to version 0.3.1 and added description.

Update 2010/12/08: Added alternative installation directory.

Jump-to-Playing Rhythmbox Plugin TODO

I haven’t looked at the plugin for a while, but I’ve been meaning to reproduce my “todo” list for it that I wrote on the rb-dev list a while back.

  • the gconf keys in gconf-editor say they have no schema. The main plugins’ keys have a schema and don’t give a warning. Definitely not serious, though.
  • it currently adds/removes the ui string when the options are toggled in the configure dialog. I have a feeling it might be better to only add/remove them in the activation/deactivation, and just hide/show here. Maybe faster?
  • it currently hides the browser button in small display mode. That has nothing to do with the jump-to-playing button. That should be in core, if it was decided that was the desired behaviour.
  • to hide the buttons in the small display, it checks the value at activation, and it connects to the View menu’s toggleButton’s “toggled” signal. So whenever it’s toggled, the gconf value for the small display mode is checked, but I think there’s a delay sometimes. Pushing Ctrl D quickly a few times may result in incorrect UI presented. I remember deciding that this is due to a delay set on the gconf callback to overcome some other bug….
  • in the context menus, ‘Properties’ should really be the last item. They need a placeholder put in the UI core. UPDATE: I’ve filed a bug with a patch attached here. Jump-to-Playing and other plugins will need to be updated when the patch is applied. I’ve just done it on my local copy w/Rhythmbox HEAD…. Looks like this:
    Before (red) and After (green) applying the patch and using an updated plugin

    Before (red) and After (green) applying the patch and using an updated plugin

  • Show the context menu item in PodcastView and PlaylistView popups, as well. I’ve added this in my local copy, already. Maybe I should just bump the version and release….

A Guide to Email for that Forward-Loving Family Member

Dear Dad,

I’m happy to read something written by you. Really, I am.

However, I’m tired of reading reams of forwarded drivel. (But please do not think you are responsible for this.) Being one of these “modern generation” kiddies (and especially in my case) I spend a lot of time on the computer already. I have to learn to skip through huge amounts of junk to find small morsels of information. This is a rather typical characteristic of anyone that has an office-like job (or lifestyle, in my case….). …Unless they’re in the HR department… my god, I can only imagine.

Evaluation

When I open my email inbox, I always look at my unread emails. First, I look at who sent it – you get bonus points here, since you’re my dad. 🙂 Then, I look at the subject of the email and mentally assign an importance value to it. All forwarded email has “FWD” or “FW” (or something equivalent) in the subject and is almost immediately classified as optional, in my mind. (In the past, I’ve even automated this process by creating email filters that sort all forwards into an ‘unimportant’ folder I can browse later.) I scan the rest of the subject and decide if it interests me enough to open it. If not, I delete it.

Delete

If it gets this far, I’ve opened it and I’m scanning the first few sentences. If I’m then assaulted by multicoloured, size 64 text, I click “Delete.” If I’m told to read to the bottom, I click “Delete.” If some generic text telling me the person sending this email cares about me and warns me that if I don’t send it to five more of my friends then I’m a horrible person… yup, I click “Delete” – but faster, this time. If I actually read the forwarded email (not incredibly likely), it might be something I find a little amusing and it may even make me smile. It’s true, sometimes I smile. Then I’ll click “Delete.” In fact, the ultimate location for all forwards will most likely be the Trash. They’re generally trivial monologues that don’t yield discussion and I don’t care to pass them on.

Nothing Personal

99% of the email you send me are forwards. No questions directed at me. Rarely information about you. And I’m one of many recipients, so it couldn’t have been that important.

If you’re going to attach personal commentary to FWDs, I really recommend putting it at the top to maximize the possibility that it is seen. Please do not get upset if I didn’t read that hilarious joke about the fisherman and the hunter, or I missed the 20 unbelievable factoids about pears and the 10-step guide to common sense and self-betterment. They probably ended up in the trash, along with the others. This is typical of email forwards and, just like forwards themselves, nothing personal.

Bottom Line

I’m not asking you to stop sending forwards. Reducing the quantity certainly couldn’t hurt, I suppose…. But please try to understand the value of a forward to someone like me. I would value one short sentence written for me, lacking punctuation, within an email that has no subject much more than any forward (with or without supplementary commentary). Tell ya what, I’ll even reply. 😉

Love always,

Your geeky son, Steven.