Created
March 21, 2013 18:48
-
-
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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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