searching address book (was Re: [TxMt] create email from document)

Grant Hollingworth grant at antiflux.org
Thu Nov 16 17:22:19 UTC 2006


* Niels Kobschätzki <n.kobschaetzki at googlemail.com> [2006-11-07 23:34]:
>It seems that "Find EMail Address in Address Book" works only with  
>plain-ASCII and not extended. I can't find names with umlauts (the  
>vowels with the dots above - äöü ÄÖÜ) in their name, like mine  
>"Kobschätzki".

I worked out a fix for this.  If you encode the search text as Mac Roman instead of UTF-8, lbdb and contacts will find it.  I changed the command to translate for contacts.  The fix for lbdb is a bit more involved, because lbdb is broken into modules and only the Address Book module needs help.

These directions assume you installed lbdb with MacPorts.

1. mkdir ~/.lbdb
2. save the attached m_osx_addressbook in ~/.lbdb
3. add the following to ~/.lbdbrc:
   MODULES_PATH="$HOME/.lbdb /opt/local/lib/lbdb"

Note that these commands assume your TextMate text is in UTF-8.
-------------- next part --------------
#! /bin/bash -posix
#
# -*-sh-*-
#
#     Copyright (C) 2003  Brendan Cully <brendan at kublai.com>
#     Copyright (C) 2005  Roland Rosenfeld <roland at spinnaker.de>
# 
#     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 2 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
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.
# 
#     You should have received a copy of the GNU General Public License
#     along with this program; if not, write to the Free Software
#     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
#

libdir=/opt/local/lib/lbdb/

m_osx_addressbook_query()
{
  $libdir/ABQuery `echo "$@" | iconv -f 'utf-8' -t 'macroman'` | grep -ia "$@"
}
-------------- next part --------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>beforeRunningCommand</key>
	<string>nop</string>
	<key>bundleUUID</key>
	<string>B7BC3FFD-6E4B-11D9-91AF-000D93589AF6</string>
	<key>command</key>
	<string>#!/usr/bin/env ruby -wKU
require 'iconv'

SUPPORT = ENV['TM_SUPPORT_PATH']
DIALOG = SUPPORT + '/bin/tm_dialog'
ENV['PATH'] = %x{sh -c '. "$BASH_ENV"; echo $PATH'}
require SUPPORT + '/lib/escape'
require SUPPORT + '/lib/plist'
require SUPPORT + '/lib/exit_codes'
search_text = STDIN.read

def found?(executable)
  ENV['PATH'].split(/:/).find do |dir|
    File.executable?(File.join(dir, executable))
  end
end

# convert between UTF-8 and Mac Roman encoding
macroman = Iconv.new('macroman', 'utf-8')
utf8 = Iconv.new('utf-8', 'macroman')

## these commands are tried in order

# lbdb is The Little Brother's Database
# http://www.spinnaker.de/lbdb/
# it can search Mutt's alias files, abook, LDAP directories, etc.

# contacts searches the Address Book.
# http://gnufoo.org/contacts/
# It returns only one email address for each person.

# Both lbdb and contacts can be easily installed with MacPorts:
# http://www.macports.org/

# The fallback option is querying Address Book with AppleScript.
# Unlike lbdb and contacts, it requires Address Book to be open.

COMMANDS = [
  ['lbdbq', 
    proc do
      %x{lbdbq "#{search_text}" | tail -n +2}.map do |line|
        line.split(/\t/)[0..1].map {|f| f.strip}.reverse
      end
    end
  ],
  ['contacts',
    proc do
      %x{contacts -Ss -f "%n:%e" "#{macroman.iconv(search_text)}" | tail -n +2}.map do |line|
        line.chomp.split(/:/).map {|field| utf8.iconv(field)}
      end
    end
  ],
  ['osascript',
    proc do
      %x{osascript <<END
tell application "Address Book"
  set matchList to {}
  repeat with oneID in (get id of every person whose name contains "#{search_text}")
    set oneID to contents of oneID
    set dataList to (get every email of person id oneID)
    repeat with oneValue in dataList
      copy (name of person id oneID & tab & value of oneValue) to end of matchList
    end repeat
  end repeat
end tell

set AppleScript's text item delimiters to ASCII character 10
return items of matchList as string
END
}.map {|line| line.chomp.split(/\t/)}
    end
  ],
]

matches = COMMANDS.find {|command| found? command.first }.last.call

names = matches.map do |name, email|
	{'title' => "#{name} (#{email})", 'email' => "#{name} <#{email}>"} if email
end.compact
TextMate.exit_show_tool_tip "No matches found" if names.empty?
TextMate.exit_replace_text names[0]['email'] if names.length == 1

plist = { 'menuItems' => names }.to_plist
res = PropertyList::load(`#{e_sh DIALOG} -up #{e_sh plist}`)
TextMate.exit_discard unless res.has_key? 'selectedMenuItem'

print res['selectedMenuItem']['email']</string>
	<key>fallbackInput</key>
	<string>word</string>
	<key>input</key>
	<string>selection</string>
	<key>keyEquivalent</key>
	<string>^@A</string>
	<key>name</key>
	<string>Find Email Address in Address Book</string>
	<key>output</key>
	<string>replaceSelectedText</string>
	<key>uuid</key>
	<string>575C4CAF-71B1-4F92-886B-8D89E0D0EFDD</string>
</dict>
</plist>


More information about the textmate mailing list