I'm Feeling Lucky

The script now uses Google's "Browse By Name" feature, which automatically defaults to a standard Google search if there's no obvious "lucky" match. Here's a little script I find useful. Pretty simple takes a string and runs a Google I'm Feeling Lucky search on it. It's especially useful if you assign it a global shortcut using FastScripts (mine is Space). It's a great time saver, since it doesn't take much to construct a search that's likely to take you right to the page you need. Copy and paste into Script Editor (or open it directly) and save it in ~/Scripts/. If you use a browser other than Safari, make sure to change the default_browser property. Setting the browser is necessary because I believe FastScripts eats commands that implicitly steal focus (such as open location). (Normally this is a good thing.)

--  I'm Feeling Lucky
--
--  Created by Christopher Biagini on 2007-05-09.
--  Copyright (c) 2007 . All rights reserved.
--

property default_location : "apple inc"
property default_browser : "Safari"

try
    set dialog_result to display dialog -
        "Where do you want to go?" default answer default_location -
        with icon 1 -
        buttons {"Cancel", "I'm Feeling Lucky"} -
        default button "I'm Feeling Lucky"
    set button_pressed to button returned of dialog_result
    set default_location to text returned of dialog_result

    if button_pressed is "I'm Feeling Lucky" then
        tell application default_browser to activate
        open location "https://www.google.com/search?" & -
            "ie=UTF-8&sourceid=navclient&gfns=1&q=" & -
            url_encode(default_location)
    end if
on error the_error number errNumber
    if (errNumber is equal to -128) then
        return
    else
        display alert "The script done broke!" message the_error -
            as warning buttons {"D'oh!"} default button "D'oh!"
    end if
end try

on url_encode(the_text)
    return do shell script "echo " & quoted form of the_text & -
        " | ruby -e 'require \"cgi\"; puts CGI.escape(gets.strip)'"
end url_encode