Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Geocodes addresses on the command line and print the geo coordinates (latitude and longitude). See for more info on how I am using this script.
#!/usr/bin/env ruby
# Determines the coordinates (latitude and longitude) of the places or
# addresses passed as arguments (either on the command line or via stdin)
# and prints the results.
# This script requires the Ruby Geocoder gem by Alex Reisner
# (
# To install the gem, run this command from a Terminal session:
# sudo gem install geocoder
# If you are using a Ruby version manager such as rbenv or RVM, make sure
# to install the Geocoder gem in the operating system's Ruby installation
# if you use this script inside an OS X Automator action.
# For example, in rbenv, call rbenv global system before installing.
require 'geocoder'
if ARGV.count > 0
input = ARGV
input = ARGF
input.set_encoding("utf-8") # required to handle non-ASCII characters in Automator action
output = ""
input.each do |address|
trimmed_address = address.strip
if trimmed_address.empty?
output += "\n"
geocode_result =
if geocode_result.first
coordinates = geocode_result.first.coordinates.join(", ")
coordinates = "Not found"
output += "#{coordinates}\n"
puts output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment