Create a keyword search function for Safari
Fri, Dec 14 2007 at 7:30AM PST • Contributed by: ocroquette
Fri, Dec 14 2007 at 7:30AM PST • Contributed by: ocroquette
I really miss the keyword feature of Firefox when I use Safari. For those who don't know it, it allows to define keywords, like for example img and link it to a search engine, for example, images.google.com. You just have to type img dog in the address bar to get the results of Google Image directly, without even going to the query page. I have written the following AppleScript to get a similar behaviour using Safari.
When run, the script will ask you for a query string. The first word is the keyword, the rest are the parameters. For example, type gg Macos to search for Macos in Google, or img cats dogs to search for cats dogs on Google Images. Here's the code: To install it, open the AppleScript Editor (from /Applications/AppleScript), copy/paste the script, and save it in your user's Library/Scripts folder as MySearch. Then, you should bind the script to a hot key (a shortcut). I personally use FastScripts to bind the key Command-Shift-K, but there are probably other solutions. With this configuration, a difference with the Firefox keywords (which is also a feature) is that Safari doesn't need to be in the foreground. Just press the hot key from whereever you are, and it will bring Safari to the front and ask you for your query.
To add search engines, you will have to edit the function keywordToURL at the beginning, which contains already some examples, and is hopefuly easily understandable. First, use the browser normally to search for (on the desired site), for example, xyz123. Have a look at the resulting URL. Notice where your xyz123 is in it. We will call everything before the search term PREFIX, and everything after it SUFFIX. Add a new section to the script similar to the existing ones:
When run, the script will ask you for a query string. The first word is the keyword, the rest are the parameters. For example, type gg Macos to search for Macos in Google, or img cats dogs to search for cats dogs on Google Images. Here's the code: To install it, open the AppleScript Editor (from /Applications/AppleScript), copy/paste the script, and save it in your user's Library/Scripts folder as MySearch. Then, you should bind the script to a hot key (a shortcut). I personally use FastScripts to bind the key Command-Shift-K, but there are probably other solutions. With this configuration, a difference with the Firefox keywords (which is also a feature) is that Safari doesn't need to be in the foreground. Just press the hot key from whereever you are, and it will bring Safari to the front and ask you for your query.
To add search engines, you will have to edit the function keywordToURL at the beginning, which contains already some examples, and is hopefuly easily understandable. First, use the browser normally to search for (on the desired site), for example, xyz123. Have a look at the resulting URL. Notice where your xyz123 is in it. We will call everything before the search term PREFIX, and everything after it SUFFIX. Add a new section to the script similar to the existing ones:
else if theKeyword is equal to "YOURKEYWORDHERE" then
return "PREFIX" & p "SUFFIX"
The p will be replaced by your parameters.
[robg adds: This worked as described for me when I tested it.]
•
[5,416 views]
