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.]