How do I search firefox Smart Search from the command line in OS X?
February 19, 2007 8:33 PM   Subscribe

I absolutely love Firefox Smart Keyword Searches. Beyond just the default preinstalled ones, I also use custom ones for easynews, rottentomatoes, metafilter, etc. Here's my question: Is it possible to launch these from the command line in OS X? For example, ideally I would enter (into a bash prompt) rt apple to open a new tab in firefox with a search result for "Apple" in rotten tomatoes, like this.. Similarly, I would like to be able to enter easy creative commons to have it launch the keyword bookmark "easy" which searches easynews global for "creative commons". Is this possible to do, preferably without using Applescript or Automator?
posted by crazyray to Computers & Internet (14 answers total) 10 users marked this as a favorite
 
I don't know about using the command line, but you can replicate the behavior in Safari using Sogudi.
posted by ardgedee at 8:44 PM on February 19, 2007


Not sure about the command line. You can do it with Quicksilver, though.
posted by danb at 9:00 PM on February 19, 2007


Second using Quicksilver.
posted by Blazecock Pileon at 9:09 PM on February 19, 2007


Not much to add here but just wanted to say that I use Quicksilver's Web Search Module and it has actually kind of changed my life. In my opinion it's better than doing it from the terminal because you don't have to switch windows. If you've never checked out Quicksilver you should definitely give it a look.
posted by subclub at 9:10 PM on February 19, 2007


Response by poster: Actually I have tried QuickSilver for several days not just once but two seperate times, and for some reason I just don't connect with it the way so many other people seem to.
posted by crazyray at 9:30 PM on February 19, 2007


Best answer: Full disclosure; I haven't used a Mac since 1997, so I may be barking up the wrong tree, here. Therefore, I will restrict my answer to be relevant to Linux, and assume that Macs include Bash or something similar.

Anyway, in Linux, you could do a bash script like this. Make a file called "wiki". Put this in it:

#!/bin/bash
firefox “http://en.wikipedia.org/wiki/$1″


then do

chmod +x wiki

In your terminal, to make the file executable, then move it to your /usr/bin folder (ie. where executable files are kept).

Henceforth, typing:

wiki Vodka

At the terminal will open up a new Firefox window, loaded to the Wikipedia entry for Vodka. Rinse and repeat for the other searches you want to do. Not very helpful, maybe, but doing this will require some kind of script to be written if you're doing it from the terminal.
posted by Jimbob at 9:41 PM on February 19, 2007 [2 favorites]


The script for Rotten Tomatoes, by the way, would look a little like this:

#!/bin/bash
firefox "http://www.rottentomatoes.com/search/full_search.php?search=$1"

posted by Jimbob at 9:44 PM on February 19, 2007


Response by poster: Jimbob, thank you so very much! Based on your idea, and the idea of avoiding problems with spaces, here is what I ended up with:

#!/bin/bash
query=""
for this_query_term in $@
do
query="${query}${this_query_term}+"
done
url="http://www.rottentomatoes.com/search/full_search.php?search=${query}"
open "$url"


If I may ask one more little thing, will this approach work for all the others as well? i.e. what would ebay look like?
posted by crazyray at 10:28 PM on February 19, 2007


Visit the website and do a few searches. The URL pattern will become clear.
posted by Blazecock Pileon at 10:37 PM on February 19, 2007


Does anyone know how to do this in *one line* in quicksilver? I want to open quicksilver, and type "g foosball" to search google for 'foosball'. With the web search module you have to do "g", TAB, TAB, "foosball", ENTER -- due to the way you are chaining the commands together...
posted by rsanheim at 10:59 PM on February 19, 2007


crazyray, bash can do string substitutions in parameter expansions, which would let you get rid of the for this_query_term loop and the redundant "+" it adds to the end of your query string. Try running

#!/bin/bash
args="$*"
echo ${args// /+}

with a few arguments, and you'll get the idea. The bash manpage has all the gory details in the "parameter expansion" section.

For a command shell, bash is actually a pretty useful string processing language.
posted by flabdablet at 1:30 AM on February 20, 2007


#!/bin/bash
IFS=+
echo "search=$*"

works too.
posted by flabdablet at 1:37 AM on February 20, 2007


Hmm, it doesn't look like the other solutions above do URI escaping. If you use Bash and have Perl's URI::Escape module installed (or are willing to install it), you can define shell functions such as this in your .bashrc:

function goo { open "http://www.google.com/search?q=`perl -MURI::Escape -e \"print uri_escape(\\"$1\\")\"`&hl=en" ; }

You invoke this shell function like so:

[you@yourhost]$ goo "if (flag1 && flag2)"

(Note that the entire set of arguments is quoted.) The function generates URL's such as

http://www.google.com/search?q=if%20(flag1%20%26%26%20flag2)&hl=en

and uses the OS X open command, which will open the URL in your default browser.
posted by harmfulray at 8:44 AM on February 20, 2007


Bash can do it without perl, though it's a bit of a stretch :-)

Save this as /usr/bin/goo and chmod +x it:

#!/bin/bash

function chr {
    printf -v chr \\\\%o $1
    printf -v chr $chr
}

function asc {
    local arg="${1:0:1}"
    local -i upper=256
    local -i lower=0
    until
        let asc=(upper+lower)/2
        chr $asc
        [ "$chr" == "$arg" ]
    do
        if [ "$chr" '>' "$arg" ]
        then
            let upper=asc
        else
            let lower=asc
        fi
    done
}

function url_escape {
    local c
    url_escape=''
    for ((i=0; i<${#1}; i++))
    do
        c="${1:$i:1}"
        [[ "$c" =~ '[-!$'\''()*+,.0-9A-Z_a-z]' ]] || {
            asc "$c"
            printf -v c %%%02X $asc
        }
        url_escape="$url_escape$c"
    done
}

function google_search {
    url_escape "$1"
    open "http://www.google.com/search?q=$url_escape&hl=en"
}

IFS=+
google_search "$*"


Once you've done that,

goo penguin will generate
open "http://www.google.com/search?q=penguin&hl=en",

goo penguin cafe orchestra will get you
open "http://www.google.com/search?q=penguin+cafe+orchestra&hl=en",

goo 'penguin cafe orchestra' runs
open "http://www.google.com/search?q=penguin%20cafe%20orchestra&hl=en",

goo \"penguin cafe orchestra\" yields
open "http://www.google.com/search?q=%22penguin+cafe+orchestra%22&hl=en", and

goo '"penguin cafe orchestra"' makes
open "http://www.google.com/search?q=%22penguin%20cafe%20orchestra%22&hl=en".

Given the insane amount of fartarsing about required to do the character escaping, this runs surprisingly briskly - probably because it's all done with bash builtins and doesn't launch any external processes or subshells.

Here's a link to goo to save you copying and pasting possibly MeFi-mangled code.

If anybody can think of a faster way for bash to convert characters to numbers and back than the chr and asc functions presented here, I'd love to see it.
posted by flabdablet at 7:33 AM on February 21, 2007


« Older Some questions about fonts specified in a colophon   |   Photoshop: clean up newsprint background? Newer »
This thread is closed to new comments.