Skip to content

Instantly share code, notes, and snippets.

@marckohlbrugge
Created March 21, 2013 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marckohlbrugge/5215601 to your computer and use it in GitHub Desktop.
Save marckohlbrugge/5215601 to your computer and use it in GitHub Desktop.
TextExpander snippet which pastes the URL of the first search result it finds based on what's in your clipboard. For example copy "TextExpander" to your clipboard and run this snippet and you'll find it pastes the [TextExpander homepage](http://www.smilesoftware.com/textexpander). Recommend shortcut: /url Adapted from Brett Terpsta's [Markdown S…
#!/usr/bin/env ruby -wKU
require 'net/http'
clipboard = %x{__CF_USER_TEXT_ENCODING=$UID:0x8000100:0x8000100 pbpaste}.strip
def e_url(string)
string.gsub(/([^a-zA-Z0-9_.-]+)/n) do
'%' + $1.unpack('H2' * $1.size).join('%').upcase
end
end
def do_search(phrase)
res = Net::HTTP.get(URI.parse("http://duckduckgo.com/?q=%5C#{e_url(phrase)}"))
match = res.match(/window.location.replace\('(.*?)'\)/)
return match.nil? ? false : match[1]
end
res = do_search(clipboard.strip)
print %Q{#{res.strip}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment