Skip to content

Instantly share code, notes, and snippets.

@joech4n
Created March 20, 2015 01:18
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 joech4n/fbf9590e03a58cc89bc0 to your computer and use it in GitHub Desktop.
Save joech4n/fbf9590e03a58cc89bc0 to your computer and use it in GitHub Desktop.
Google Calculator - Alfred Workflow Script Filter
load 'alfred_feedback.rb'
require 'logger'
require 'uri'
# Set up logging
file = File.open('/tmp/alfred-workflow-gcalc.log', File::WRONLY | File::APPEND | File::CREAT)
logger = Logger.new(file)
logger.level = Logger::WARN
#logger.level = Logger::DEBUG # Uncomment this for debug output
orig="{query}"
url='http://www.google.com/search?q=' + URI.encode(orig)
logger.debug("Orig: " + orig)
command="/usr/bin/curl -sA Mozilla '#{url}' | /usr/local/bin/html2text -width 80 2>&1 |grep 'Any time'" # brittle, will break if Google makes any changes to their HTML
result=`#{command}`
logger.debug("Raw result: " + result)
# Cleanup result
result.chomp!
result.sub!(/\s+.*time\s+/, '')
result.gsub!(/\*/,'')
result.tr!('^A-Za-z0-9\.=', '')
result.gsub!(/(.)./, '\1') # Hack until I can figure this out. #{result} ends up with dupe characters. This only when run in Alfred workflow. Cannot repro in standalone script.
logger.debug("Clean result: " + result)
# Create XML
feedback = Feedback.new
feedback.add_item({:title => result, :subtitle => "Description", :arg => "Value", :uid => "ID", :args => result, :icon => {:type => "filetype", :name => "icon.png"}})
logger.debug("XML: " + feedback.to_xml)
logger.debug("END --------------------------------------------------------------------------------")
puts feedback.to_xml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment