Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Access the Address Book database from the shell UNIX
A while back, I started working on a utility to access the AddressBook database from the command line. I had a prototype, but had since stopped working on it. This related hint encouraged me to finish it up and release it. So, here it is: Contacts. Here's a sample of its output:

$ contacts
NAME                  PHONE        IM                  EMAIL                    
Kim Jong Il           +82-(001)385 License2KimJongill  mrill@...
George W Bush         202-276-4300 Bush43              big.dubya@...
[...]
[robg adds:I trimmed the email addresses to narrow the width a bit, but they do appear in full in the program's output. This program is also posted in the comments to the first article, but I felt that it was enough of an improvement in the methodology that it was worth posting on its own. The program includes, hopefully obviously, a means of searching for entries as well as simply displaying the Address Book's contents.]
    •    
  • Currently 2.33 / 5
  You rated: 3 / 5 (3 votes cast)
 
[8,669 views]  

Access the Address Book database from the shell | 29 comments | Create New Account
Click here to return to the 'Access the Address Book database from the shell' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
awesome!!!
Authored by: macubergeek on Jun 13, '03 10:49:51AM

Thanks for coding this!
Far and again better than my humble shell hack.



[ Reply to This | # ]
Re: awesome!!!
Authored by: semios on Jun 13, '03 04:20:50PM

My thanks to you for showing me that other people were interested in such a thing too, besides just me. :)



[ Reply to This | # ]
man page?
Authored by: macubergeek on Jun 13, '03 11:29:11AM

Hmmm
installer didn't seem to install a man page so I made one from scratch using the online web man page:
------------------------------------------------------------------
.Dd June 12, 2003
.Dt CONTACTS 1
.Os
.Sh NAME
.Nm contacts
.Nd list directory contents
.Sh SYNOPSIS
.Nm
.Op Fl contacts [-hHsnml] [-f format] [search]
.Op Ar file ...
.Sh DESCRIPTION
The utility contacts affords easy access to information in the Address-Book database from the shell.
.Pp
The utility contacts accepts the following flags:
.Bl -tag -width indent
.It Fl h
displays help/usage
.It Fl H
uppresses headers from being displayed
.It Fl s
sorts the list before displaying it
.It Fl m
show the person marked as "me"
.It Fl n
displays note below each record (by default only one line is
shown, to see entire note include the -l option)
.It Fl l
loose formatting - doesn't truncate the record values to align columns
.It Fl f
accepts a format string (see below)
.Pp
The format string can contain the following tokens:
.It %n
name (first and last name or company name)
.It %fn
first name
.It %ln
last name
.It %p
phone (order of preference: home, work, mobile, main, pager,
fax, other)
.It %hp
home phone
.It %wp
work phone
.It %mp
mobile phone
.It %Mp
main phone
.It %pp
pager phone
.It %fp
fax phone
.It %op
other phone
.It %e
email (order of preference: home, work, other)
.It %he
home email
.It %we
work email
.It %oe
other email
.It %t
title
.It %c
company
.It %g
group
.It %w
webpage/homepage
.It %b
birthday
.It %i
instant messanger name (order of preference: aim, yahoo, jabber,icq, msn)
.It %ai
AOL Instant Messenger (AIM)
.It %yi
Yahoo IM
.It %ji
Jabber IM
.It %ii
ICQ IM
.It %mi
MSN IM
.It %N
displays the note on a line by itself under the person's record. This token should usually be placed at the end of the formatter. The contacts utility exits 0 on success, 1 if no one is found in the search, 2 for usage problem, and >0 if any other error occurs.
.Sh EXAMPLES
.Pp
EXAMPLES
The command:
$ contacts
displays all contacts.
.Pp
The command:
$ contacts -h
displays the usage.
.Pp
The command:
$ contacts fred
displays all persons with the name "fred" somewhere in their name.
.Pp
The command:
$ contacts -m
displays person marked as me.
.Pp
The command:
$ contacts -mH
displays person marked as me with no header.
.Pp
The command:
$ contacts -mH -f '%e'
displays the-person-marked-as-me's email address with no header.
.Pp
The command:
$ contacts -f '%n %hp %wp %mp'
displays everyone's name and home phone, work phone, and mobile phone.
.Pp
The command:
$ contacts -f '%ln %fn %hp'
displays everyone's last name, first name, and home phone.
.Pp
.Sh BUGS
BUGS
.Pp
contacts only searches the first name, last name, email, company name, and AIM name in the AddressBook, whereas the AddressBook application's behavior appears to search everything in the record.
.Pp
The format string only recognizes its own tokens i.e. "blah %n" won't print "blah fred smith" instead it will just print "fred smith". Similarly, "%fn%ln" won't print "fredsmith"; it'll print "fred smith" instead.



[ Reply to This | # ]
man page?
Authored by: fxt on Jun 13, '03 11:51:53AM

the man page installed just fine for me.
check your manpath?

fxt



[ Reply to This | # ]
re: man page?
Authored by: huzzam on Jun 17, '03 06:56:43PM

seems to get put in /usr/local/man, rather than /usr/local/share/man, where other local man pages tend to end up. check there.

peter



[ Reply to This | # ]
Access the Address Book database from the shell
Authored by: famousactress on Jun 13, '03 11:54:05AM

This is great, but it's read only, yes?

My dilemma is that I'm trying to move contacts from Outlook to Mac. I'm new to Macs, and I see that I could export vcards, but Outlook won't let me do that in batch (dipsh!ts at microsoft).. I did however get them exported to a comma separated file.

I was hoping I'd be able to write an ant task / java program that would pile them into Address Book but I've been unable to figure out where/how the Address Book stores it's data. (I was praying for an xml file somewhere, but I'm beginning to doubt that it's that easy).

Anyone have a suggestion? If I get some help and complete the program, I'll be sure to make it publically available here.

thanks,
-phill



[ Reply to This | # ]
Access the Address Book database from the shell
Authored by: famousactress on Jun 13, '03 12:02:19PM

ahhight.. I found AddressBook.data

but... I have NO idea what effing format it's in. Anyone know where accessing this thing is documented?

Plz excuse my newbness :)

-Phill



[ Reply to This | # ]
Access the Address Book database from the shell
Authored by: mdzorn on Jun 13, '03 12:12:34PM

There are two ways to do that:
1. The long way: You can use another program, such as Netscape or Mozilla, to import the TAB-delimited file. This creates an LDIF file that can be imported into AddressBook.

2. The Highway: Create an entry in AddressBook. Export that entry onto your desktop as a vCard file. Examine the vCard file in a text editor and write a perl script (Java program, Objective-C, Fortran, ... ) to reformat your TAB-delimited file into a vCard file. Import vCard file into AddressBook.



[ Reply to This | # ]
Modify the Address Book
Authored by: Krioni on Jun 13, '03 01:02:17PM
The author of contacts could probably add the feature.

If he used AppleScript to get information from AB (and he probably did), then features could be added to let you edit/add information to AB.

However, the syntax for adding and/or modifying entries is more complex than for pulling out data. Also, you have to remember to send the "save database" change after every command.

[ Reply to This | # ]

Re: Modifying Address Book
Authored by: Krioni on Jun 13, '03 01:17:06PM

It seems he is using the actual Address Book framework that Apple made available to developers - not sending AppleScript commands through 'do shell script'. I opened up his code in ProjectBuilder (he released it under GPL), and it loads the Ab framework, and is coded in Objective C.

Cool! Of course, that means I'll have to learn more than the little Obj-C I know now if i wanted to make any changes. :-) Been busy in AppleScript, and haven't had time to learn Obj-C. Maybe someday...



[ Reply to This | # ]
Access the Address Book database from the shell
Authored by: at_sym on Jun 13, '03 03:06:15PM
Could you not use Address Book Importer 1.1.1? It has the ability to move a tab-separated file into the Address Book.

[ Reply to This | # ]
Try this
Authored by: jecwobble on Jun 13, '03 03:33:52PM

I submitted this in the Apple forums: Outlook 2000 macro for vCal & vCard export

I use this macro to batch export contacts and appointments to vCal and vCard format, which can be dropped right into Address Book and iCal.

[ Reply to This | # ]

Holy Helpful!
Authored by: famousactress on Jun 13, '03 04:36:04PM

Sheesh. Thanks a ton for all the advice. I imagine I'll be able to get this to work for sure now!

-phill



[ Reply to This | # ]
Holy Helpful!
Authored by: famousactress on Jun 13, '03 04:50:55PM
I used the utility over at roydesign with absolute success. If anyone else is dealing with the same problem, I highly recommend it.. Took all of 5 minutes to download and use it. :) -phill

[ Reply to This | # ]
Access the Address Book database from the shell
Authored by: dfbills on Jun 14, '03 08:37:56PM

Of course you can export your contacts in vcf from outlook for windows. You can even to them all in one batch to individual files. The import/export engine is quite good.

Once exported, just drop them all into addressbook.

That's all there is to it.



[ Reply to This | # ]
Access the Address Book database from the shell
Authored by: robJ on Jun 13, '03 11:55:55AM

Do you have any plans to add the ability to modify the AB database?



[ Reply to This | # ]
Re: Access the Address Book database from the shell
Authored by: semios on Jun 13, '03 04:30:23PM
I imagine someone will add the ability to add, modify, and delete records in contacts. I'm curious to see what some people come up with. I've got a few ideas myself. I don't want to see the utility blow up with a ton of options to facilitate that. It should be simple and not require a lot of tedious options. Imagine you want to synchronize your contacts from one machine to another. Maybe you could do something like this:

$ contacts | rsh $host contacts add
So yes, I probably will add some ability to write to contacts in the future, but I'm not going to invest a lot of time in it right now. And I would encourage anyone out there who's really interested in it to give it a hack. Again, I'm curious to see what people come up with.

[ Reply to This | # ]
Re: Access the Address Book database from the shell
Authored by: robJ on Jun 16, '03 04:26:33PM

Great, I look forward to it. I've already created an AppleScript that allows me to use contacts for quick searches. This is so much better than keeping Address Book running all of the time. Thanks!



[ Reply to This | # ]
lbdb (was: access addresbook...)
Authored by: sanandak on Jun 13, '03 12:59:22PM
Take a look at lbdb (the "little brother data base"):
lbdb

It has an ABQuery (along with a number of other functions).

Sridhar.

[ Reply to This | # ]

Fink
Authored by: wayne on Jun 14, '03 01:05:46PM

Pretty cool... You should add this to Fink...



[ Reply to This | # ]
Fink
Authored by: chris01 on Jun 17, '03 09:00:19AM
comments is now in fink. Check out: http://fink.sf.net/pdb/package.php/contacts

[ Reply to This | # ]
Access the Address Book database from the shell
Authored by: rocteur on Jun 15, '03 06:18:25AM

This is a GREAT program.

I wish there were more CLI programs to access data that otherwise requires a GUI..

Thanks!



[ Reply to This | # ]
Re: Access the Address Book database from the shell
Authored by: semios on Jun 15, '03 10:12:55PM

Me too! :)



[ Reply to This | # ]
Access the Address Book database from the shell
Authored by: adriaant on Jun 16, '03 12:57:06AM

In order to display foreign languages such as Japanese properly, please replace the "cString" calls in the "printPeopleWithFormat" routine with "UTF8String". Otherwise, the program bails with character conversion errors.



[ Reply to This | # ]
%ln truncates at 11 characters
Authored by: jecwobble on Jun 16, '03 11:15:29AM
I played around with this this weekend. I found that it was not displaying the last characters of some of the last names (including mine) when using the %ln format parameter. Seems to be limited to 11 characters. Looks as if that's true for %fn, too.

[ Reply to This | # ]
Re: %ln truncates at 11 characters
Authored by: semios on Jun 16, '03 02:02:55PM

That is true, but if you pass it the -l flag (loose formatting) then no values will be truncated. In the future, I'd like to see the tool use its own preferences (or defaults) to pick the column width for each token instead of just a bunch of hardcoded values. Next release, maybe.



[ Reply to This | # ]
Access the Address Book database from the shell
Authored by: 5chm31din6 on Jun 16, '03 08:55:46PM

This is one awesome program. Love it, love it, love it. Just one problem: I can't SSH from another machine outside of my network and access it. Everything else but Contacts works.

It is installed in /usr/local/bin and the error I get is "contacts: Command not found."

Am I missing something? TIA!



[ Reply to This | # ]
Access the Address Book database from the shell
Authored by: semios on Jun 17, '03 05:10:53PM
Your path probably isn't set right. Try /usr/local/bin/contacts. That'll probably work.

[ Reply to This | # ]
Access the Address Book database from the shell
Authored by: 5chm31din6 on Jun 26, '03 01:41:40PM
That did the trick! Thanks mucho grande!

[ Reply to This | # ]