Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Create a keyword search function for Safari Web Browsers
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:
--    This program is free software; you can redistribute it and/or modify
--    it under the terms of the GNU General Public License as published by
--    the Free Software Foundation; either version 2 of the License, or
--    (at your option) any later version.

--    This program is distributed in the hope that it will be useful,
--    but WITHOUT ANY WARRANTY; without even the implied warranty of
--    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--    GNU General Public License for more details.

--    You should have received a copy of the GNU General Public License
--    along with this program; if not, write to the Free Software
--    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

-- Copyright Olivier Croquette 2007 ocroquette at free dot fr

on keywordToURL(theKeyword, theParameters)
  set p to theParameters as text
  if theKeyword is equal to "gg" then
    return "http://www.google.com/search?q=" & p
  else if theKeyword is equal to "yt" then
    return "http://youtube.com/results?search=Search&search_query=" & p
  else if theKeyword is equal to "img" then
    return "http://images.google.com/images?btnG=Recherche+d%27images=2=" & p
  end if
  return ""
end keywordToURL

on openTab(theURL)
  tell application "Safari"
    -- Open a new window if none available
    if (count of every window) is equal to 0 then
      make new document
    end if
    
    set isUrlEmpty to true
    try
      if URL of document 1 is not equal to "" then
        set isUrlEmpty to false
      end if
    end try
    
    if not isUrlEmpty then
      tell window 1
        -- Open a new tab in the existing window
        set theTab to make new tab
        set current tab to theTab
      end tell
    else
      set theTab to current tab of window 1
    end if
    set URL of theTab to theURL
  end tell
end openTab

on run
  set AppleScript's text item delimiters to " "
  set theURL to ""
  set theQuery to "gg " -- set your default keyword here
  set theErrorText to ""
  set theDialogText to "Enter your query:"
  set theDialogText to "Enter your query:"
  repeat while theURL is equal to ""
    tell application "Safari"
      activate
      set theQuery to text returned of (display dialog theErrorText & theDialogText default answer theQuery)
      set theErrorText to "Bad keyword. Try again.
"
    end tell
    set theQueryList to text items of theQuery
    try
      set theKeyword to item 1 of theQueryList
      set theParameters to (items 2 thru -1 of theQueryList)
      set theParametersString to theParameters as text
      set theURL to my keywordToURL(theKeyword, theParameters)
    end try
  end repeat
  openTab(theURL)
end run
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.]
    •    
  • Currently 1.17 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (6 votes cast)
 
[10,711 views]  

Create a keyword search function for Safari | 10 comments | Create New Account
Click here to return to the 'Create a keyword search function for Safari' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Create a keyword search function for Safari
Authored by: Jonsi on Dec 14, '07 08:50:45AM

Great! But you must put & before the "SUFFIX":

else if theKeyword is equal to "YOURKEYWORDHERE" then
return "PREFIX" & p & "SUFFIX"



[ Reply to This | # ]
Isn't this exactly what Sogudi does?
Authored by: riffraf on Dec 14, '07 09:45:04AM
It's free and very easily configurable.

I've defined "u" to be a youtube search, "w" to search wikipedia, "d" to search merriam-webster.com, "gi" to search google images, etc. So I would type "gi norwegian elkhound" into the address bar to find pictures of that type of dog.

Read more at http://www.kitzkikz.com/Sogudi.

[ Reply to This | # ]
Isn't this exactly what Sogudi does?
Authored by: nickv2002 on Dec 14, '07 02:39:16PM

Yes, this hint duplicates the functionality of Sogudi.

If you want an easier setup, use Sogudi. However the advantage of this hack is that it doesn't use input managers which some people have an aversion to.

You can also get the same functionality from utilities like LaunchBar, Quicksilver, and Butler (search for them here on Mac OS X Hints to learn more).



[ Reply to This | # ]
Isn't this exactly what Sogudi does?
Authored by: hembeck on Dec 16, '07 09:09:18AM

Yes, Sogudi does this too. This is alternative method which is quick and easy to implement, with no additional software to download.

---
--Health, Wealth and Happiness--



[ Reply to This | # ]
Create a keyword search function for Safari
Authored by: matters on Dec 16, '07 08:39:32AM

I just use Quicksilver to do this with Safari. :)



[ Reply to This | # ]
Another alternative: AcidSearch
Authored by: chp on Dec 17, '07 01:29:38AM
Another alternative: AcidSearch
Authored by: Sedokil on Feb 16, '08 03:35:50AM

AcidSearch Thanks Isolagen initiates a Phase III clinical program to investigate Isolagen Therapy for the treatment of moderate to severe acne scars. Isolagen also has a pivotal Phase III clinical program underway to evaluate the Isolagen Therapy in treating nasolabial folds, or wrinkles.
http://www.veromaxx.com



[ Reply to This | # ]
Create a keyword search function for Safari
Authored by: dandj on Feb 16, '08 09:02:42PM
I used Sogudi a lot with Tiger but I found it doesn't work reliably with Leopard. I then discovered Keywurl, which does the same job and can import abbreviations produced for Sogudi.

See http://purefiction.net/keywurl/




[ Reply to This | # ]
Create a keyword search function for Safari
Authored by: MuffinDCC on Jan 23, '09 11:21:40AM

Is there a way to do this with webkit?



[ Reply to This | # ]
Create a keyword search function for Safari
Authored by: MuffinDCC on Jan 23, '09 11:24:01AM

Doh, change Safari to WebKit in the script :P



[ Reply to This | # ]