Add Mozilla-like keyword functionality to Safari's search bar

May 20, '03 09:53:00AM

Contributed by: sapporo

To add Mozilla-like keyword functionality to Safari's search bar, point Safari's search engine to a local script, which looks for your personal keywords and redirects the HTTP request accordingly. This will let you type img madonna into Safari's search bar to search images.google.com for pictures of Madonna, for example.

To point Safari's search engine to the local script, replace http://%@.google.com/%@?q=%@&ie=UTF-8&oe=UTF-8 with http://%@@localhost/%@.php?q=%@&thisarg=adummy in Safari.app (control-click) -> Contents -> MacOS -> Safari (see nimatoad's original hint on how to do that).

Read the rest of the hint for the how-to details...

The rest can be done via the following script, which should be named search.php, and saved to your webserver's document root (usually /Library -> Webserver -> Documents). Of course, you must have Personal Web Sharing and PHP enabled for this to work.


<?
/*
Add Mozilla-like keyword functionality to Safari's search bar.

Author: sapporo@land.ru (inspired by a hint from nimatoad at
http://www.macosxhints.com/article.php?story=20030514035516436)

TO DO: german umlauts don't show up the way they should at the
target site

There's another method of redirecting Safari searches by editing
/etc -> hosts (as proposed here:
http://www.macosxhints.com/article.php?story=20030509004243460),
but this seemed to be the easiest one. You'll have to modifiy
Safari.app after you update it (and pray they didn't change the
way searching works).

*/

// add your own keywords here
$keywords = array(
    "y"    => "http://search.yahoo.com/bin/search?p=%query%",
    "atw"  => "http://www.alltheweb.com/search?q=%query%",
    "t"    => "http://s.teoma.com/search?q=%query%",
    "img"  => "http://images.google.com/images?q=%query%&ie=ISO-8859-1&hl=en&btnG=Google+Search",
    "php"  => "http://www.php.net/%query%"
    );

// the original search term
$q = urldecode($_GET['q']);

// show help screen
if (preg_match("/^\s*help/i", $q)) {
    echo "<html><body><center><h2>Search help:</h2><table>";
    while (list ($key, $location) = each ($keywords)) {
           echo "<tr><td>$key </td><td>$location</td></tr>\n";
      }
    echo "</table><p><a href='mailto:sapporo@land.ru'>sapporo@land.ru</a></body></html>";
    exit;
}

// search for matching keyword and redirect on success
while (list ($key, $location) = each ($keywords)) {
   if (preg_match("/^\s*$key\s+(.*)/i", $q, $matches)) {
       $val = str_replace("%query%", urlencode($matches[1]), $location);
         header("Location: $val");
         exit;
     }
}

// default to google.com
header("Location: http://www.google.com/search?q=".$_GET['q']."&ie=UTF-8&oe=UTF-8");

?>
[robg adds: I haven't tested this script myself.]

Comments (29)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20030519070642235