Faster and System Wide Web Search for Safari

Finding for Safari

Here’s a great script that creates a search function through AppleScript which, after being made into a Quicksilver trigger can be used ubiquitously.

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

 

 You add in your own searches by adding in: else if theKeyword is equal to "YOURKEYWORDHERE" thenreturn "PREFIX" & p "SUFFIX"

This is great because it seems like Saft, Sogudi and other Safari InputManagers are getting less and less reliable all the time. It’s good to have this low-fi solution that is available system wide.

Via: macosxhints.com 

Just in case someone doesn’t know what to do with the script:

  1. Copy all text in the box above
  2. Open ScriptEditor located at /Applications/AppleScript
  3. Paste the code you copied
  4. Save in some folder (I have mine in ~/Library/Scripts)

For those who want to use this by making it into a Quicksilver trigger:

  1. Go to Quicksilver Triggers Tab in Preferences (have Quicksilver in focus and press ⌘,)
  2. Click the “+” sign –> HotKey
  3. Either Navigate to, or drop the Script on to the “Items” selection
  4. Have the action as: Run
  5. Click on the “Trigger” column on the row that your script is on
  6. Assign it a Hot Key

Similar Posts:

I Like This Tip!


2 Responses to “Faster and System Wide Web Search for Safari”

  1. can you fix the text? thanks

  2. 4Avatars
    atmac
    For the non-Quicksilver users who would like to use this script with a global hotkey, I suggest you install the free "FastScripts Light" from Red Sweater software. I use the paid/full version of FastScripts but their "Light" version will let you install a script and assign it a hotkey which will work just fine for this and any other scripts that people might want global or application-specific hotkeys for.

Post a comment to "Faster and System Wide Web Search for Safari"



MacTips Newsletter


Twitter MacTips

  • Stay up to date with the latest Mac tips, tricks and news by following us on twitter: MacintoshTips.

AppleGazette Headlines

Most Popular Tips (30 Days)

Most Commented (30 Days)

Good Tips

Recent Discussions

MacTips Poll

What level of tips would you like to see?

View Results

Loading ... Loading ...

Friends of MacTips

Subscribe to MacTips

Site Links