Skip to content

Instantly share code, notes, and snippets.

@noahcoad
Last active March 15, 2016 04:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noahcoad/4422992 to your computer and use it in GitHub Desktop.
Save noahcoad/4422992 to your computer and use it in GitHub Desktop.
Ruby script to open the contents of the clipboard. Just assign it to a global hotkey, like Ctrl+O, using Sparkle, Quicksilver, Alfred Powerpack, or your favorite hotkey app. Copy something to the clipboard, hit the hotkey, and it's opened up.
#!/usr/bin/env ruby
# Opens the clipboard contents
# 1. Is it a file or folder?
# 2. Is it obviously a URL?
# 3. Google It
# clipboard contents
clip = IO.popen('pbpaste', 'r+').read
# open appropriately
if File.exist? clip
system("open", clip)
elsif /\w[^\s]*\.(?:com|co|uk|gov|edu|tv|net|org|tel|me|us|mobi|es|io)(?:[ \r\n]{0,2}\z|\/.*)[ \r\n]{0,2}\z/.match(clip)
clip = "http://#{clip}" unless (clip.index '://')
system("open -a 'Google Chrome' #{clip}")
else
google = "http://www.google.com/search?q=#{clip}"
system("open -a 'Google Chrome' '%s'" % [google])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment