Home > programming > Using googlecl to query google contacts from mutt

Using googlecl to query google contacts from mutt

googlecl is a neat tool for accessing the Google Contacts address book from the command line. mutt is a powerful email client that allows calling external tools for accessing an address book. Unfortunately, the output from googlecl is different from the input required by mutt. Here is a simple script that wraps googlecl to be used from mutt. Simply put the code in a file mutt-google-contacts and make it executable. Then call it from mutt by adding this to the ~/.muttrc file:

set query_command = "mutt-google-contacts '%s'"

Here is the script:


# Version 0.1 Copyright (C) 2011 Christof Schmitt
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.

# This script parses the output of 'google contacts list'
# (http://code.google.com/p/googlecl/wiki/ExampleScripts#list) and
# prints the queried email address in the format required by mutt
# (http://www.mutt.org/doc/manual/manual-4.html#ss4.5).
# Usage: Install googlecl from
# http://code.google.com/p/googlecl/downloads/list and add
# set query_command = "mutt-google-contacts '%s'"
# to ~/.muttrc

import subprocess
import string
import sys

if not len(sys.argv) >= 2:
    print 'Usage: %s querystring' % sys.argv[0]

sys.stdout.write('Calling "google contacts list": ')

query = ' '.join(sys.argv[1:])
args = ['/usr/bin/google', 'contacts', 'list', '--title', '(?i).*' + query,
        '--fields', 'name,email', '--delimiter=;']

google = subprocess.Popen(args, stdout=subprocess.PIPE)
stdout, stderr = google.communicate()

if google.returncode != 0:
    print 'error'

print 'success'

for line in stdout.split('\n'):
    if len(line) > 0:
        name, emails = line.split(';')
        for email in emails.split(', '):
            if string.find(email, ' ') != -1:
                # with two or more addresses there is an additional
                # type (home, work, ...)
                typeoraddr, emailaddr = string.split(email, ' ')
                print emailaddr + '\t' + name + '\t' + typeoraddr
                # with only one address, there is no type field
                print email + '\t' + name
Categories: programming Tags: , ,
  1. June 28, 2011 at 10:49 am

    This was just what I needed 🙂
    Thanks for sharing!

  2. September 11, 2011 at 8:35 pm

    I’ve made lots of tests but there is no way I can capture the output when I post to Blogger or upload a video to Youtube.

    If I run “google youtube list” it works fine, I capture the output.

    But if I post to Blogger, the output is output to the command line *before* I see “success”.

    Any idea why? I’m not an expert on pipes or Python so I need some help.

  3. September 11, 2011 at 10:09 pm

    Abe, sorry, i have no idea. Maybe you couldl try the forum at: http://groups.google.com/group/googlecl-discuss

  4. September 12, 2011 at 7:13 am

    Thanks, I did that.

  5. January 2, 2013 at 10:32 pm

    I know this is a bit old now but I would still like to chime in and say I found this useful. Somewhere along the line with updated versions of the googlecl utility this script stopped working showing an error about ‘error no such option: –fields’. Thankfully I *think* the option can simply be omitted from the ‘args = [ … ] ‘ line in the script and it works again.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s