Skip to content

Instantly share code, notes, and snippets.

@raphaelschaad
Last active September 26, 2015 04:08
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 raphaelschaad/1037296 to your computer and use it in GitHub Desktop.
Save raphaelschaad/1037296 to your computer and use it in GitHub Desktop.
I created a systemwide OS X service bound to ⇧⌘1 to translate GER-ENG with this ThisService http://wafflesoftware.net/thisservice/ script.
#!/usr/bin/env ruby
# ThisService Ruby service script example
# Service type: Acts on input.
# Revision 1.
# Unicode considerations:
# Set $KCODE to 'u'. This makes STDIN and STDOUT both act as containing UTF-8.
$KCODE = 'u'
# Since any Ruby version before 1.9 doesn't much care for Unicode,
# patch in a new String#utf8_length method that returns the correct length
# for UTF-8 strings.
UNICODE_COMPETENT = ((RUBY_VERSION)[0..2].to_f > 1.8)
unless UNICODE_COMPETENT # lower than 1.9
class String
def utf8_length
i = 0
i = self.scan(/(.)/).length
i
end
end
else # 1.9 and above
class String
alias_method :utf8_length, :length
end
end
input = STDIN.gets nil
# input now contains the contents of STDIN.
# Write your script here.
# LEO
# Raphael Schaad, 2011-02-01
require 'cgi'
url = "http://dict.leo.org/ende?lp=ende&lang=de&searchLoc=0&cmpType=relaxed&sectHdr=on&spellToler=&search="
url += CGI::escape(input)
system("open -a safari '" + url + "'");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment