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

A command-line-only basic phonebook script UNIX
I find Apple's Address Book an excellant tool, but sometimes I just want to quickly lookup a person's phone number. Here's a shell script to create a phone book and search the phone book. It creates a phone book called ~/.phonebook in your home directory. To find out the useage of the script do: pb -help or pb help or just pb h. To add a user do pb add Joe Blow PhoneNumber. If you enter a phone number like (123)456-7890, then put double quotes around it: "(123)456-7890"

Cavaet: be sure to double check for linewraps (the backslash line endings) in the below script code:
#!/bin/sh
case $1 in
h | help | -h | -help) echo "Useage:"
  echo "To add a user to the phonebook do: pb add Joe Blow 123-456-8900"
  echo "To remove a user from the phone book do:
  echo "pb delete <first or last name>"
  echo "NOTE: when deleting a user be sure to use either \
  first or last name, whichever is unique."
  echo "If you use parens in your phone number be sure to put \
  double quotes around the phone number."
esac

case $1 in
  # add) echo $2 >> ~/.phonebook;;
  add) echo $2 $3 $4 >> ~/.phonebook;;
  delete) cat ~/.phonebook | grep -v $2 > /tmp/phonebook && \
    mv /tmp/phonebook ~/.phonebook;;
esac

case $1 in
  f | find | -f | -find) egrep $2 ~/.phonebook;;
esac
[robg adds: I haven't tested this script as I use my Palm or AddressBook to find information. But if you spend most of your time in the Terminal, this could be a helpful script.]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[17,283 views]  

A command-line-only basic phonebook script | 21 comments | Create New Account
Click here to return to the 'A command-line-only basic phonebook script' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
System address book via shell?
Authored by: marmoset on Jun 09, '03 12:51:57PM

What would be _really_ nice would be a command line interface (even read-only) to the system Address Book. I know Cocoa and Carbon apps can get to this data, it would be great if it were somehow exposed so that, say, shell scripts could get this data.



[ Reply to This | # ]
System address book via shell?
Authored by: macman13 on Jun 09, '03 01:09:30PM

The only way I can think of off hand is to have Addressbook export a vcard of all phone numbers. Then you could modify the script to parse all "FN:" and their associated telephone numbers. Since I am not a skilled programmer, it might take me a while to whip something like this up. This does not give you immediate access to the addressbook database, buyt would allow you to update the command line phone numbers based upon the current addressbook phone numbers.

Best.
SA

---
\\\"I can do everything on my Mac I used to do on my PC, plus alot more ...\\\"
--Me



[ Reply to This | # ]
System address book via shell?
Authored by: Krioni on Jun 09, '03 02:34:51PM

Oops. I posted the shell script outside your question. Just in case you're waiting for a response to post, look below. You'll probably want to remove the -s "s" option for shells to parse the results. You may also want to add to the AppleScript to return a line-delimited list. I think I'll do that myself. :-)



[ Reply to This | # ]
Command Line lookup for Address Book
Authored by: Krioni on Jun 09, '03 02:32:43PM
How about this possibility? Do a search of the OS X Address Book from the command line. Here's the shell script:

#!/bin/sh

if [ $# -ne 2 ]; then
    echo 1>&2 "usage: abook email (or phone) YOUR_SEARCH"
    exit 127
fi


scriptcode="tell app \"Address Book\" to get name of every person where ((value of $1s) contains \"$2\")"

osascript -s "s" -e "$scriptcode"

exit 0
Remove the -s "s" part to get a simple comman-separated list, rather than an AppleScript list of strings.

[ Reply to This | # ]
update - improved results
Authored by: Krioni on Jun 09, '03 02:49:53PM
Save this into a file named abook, then do a chmod 755 abook to make it executable. If you put it in a folder in your $PATH, make sure to rehash to allow your shell to find it. The usage is:

abook email someguy@yahoo.com

OR

abook phone 856-123-4567
This version returns a line-delimited list of names, rather than an AppleScript list of strings:

#!/bin/sh

if [ $# -ne 2 ]; then
    echo 1>&2 "usage: abook email (or phone) YOUR_SEARCH"
    exit 127
fi

scriptcode="tell app \"Address Book\" to set nameList to get name of every person where ((value of $1s) contains \"$2\")
set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ASCII character 10}
set lineList to (items of nameList) as string
set AppleScript's text item delimiters to od
return lineList"

osascript -e "$scriptcode"

exit 0
Any hard-core shell-scripters out there with advice on improvements are welcome.

[ Reply to This | # ]
script dosn't work
Authored by: macubergeek on Jun 09, '03 03:03:30PM

jamesk @ /Users/jamesk/desktop@Xmac-->./abook
usage: abook email (or phone) YOUR_SEARCH
jamesk @ /Users/jamesk/desktop@Xmac-->./abook email james.kelly@wap.org
## Component Manager: attempting to find symbols in a component alias of type (regR/carP/x!bt)
syntax error: Expected class name but found identifier. (-2741)
jamesk @ /Users/jamesk/desktop@Xmac-->./abook phone Glew
## Component Manager: attempting to find symbols in a component alias of type (regR/carP/x!bt)
syntax error: Expected class name but found identifier. (-2741)
jamesk @ /Users/jamesk/desktop@Xmac-->./abook phone 856-123-4567
## Component Manager: attempting to find symbols in a component alias of type (regR/carP/x!bt)
syntax error: Expected class name but found identifier. (-2741)
jamesk @ /Users/jamesk/desktop@Xmac-->



[ Reply to This | # ]
script dosn't work
Authored by: Krioni on Jun 09, '03 03:47:30PM

What shell are you using? Hmm, actually, that should not matter, since my script specifies the shell to use.

That Component Manager stuff looks strange - I haven't seen that error in AppleScript or in the shell. Any ideas, everyone?



[ Reply to This | # ]
script dosn't work
Authored by: macubergeek on Jun 09, '03 08:00:46PM

I've made my script mirror what I see on the web page, no luck.
then I figured the lines were wrapping...wicked hard to tell where tho..
...need a clue as to how this is supposed to format, since this still dosn't work only now I get:

## Component Manager: attempting to find symbols in a component alias of type (regR/carP/x!bt)
syntax error: Expected expression, ?Äú)?Äù, etc. but found property or key form. (-2741)
jamesk @ /Users/jamesk/desktop@Xmac-->



[ Reply to This | # ]
script does work
Authored by: vonleigh on Jun 09, '03 04:28:34PM

I've seen those error messages when trying to use applescript from the commandline, when my user isn't the logged in user. If that's the case, use it with sudo to get around it.



[ Reply to This | # ]
script does work
Authored by: macubergeek on Jun 09, '03 07:46:41PM

I was trying to do it over ssh connection.mebbe that was it?

Nah just tried again. Still broken

jamesk @ /Users/jamesk/desktop@Xmac-->./abook phone Kelly
## Component Manager: attempting to find symbols in a component alias of type (regR/carP/x!bt)
syntax error: Expected class name but found identifier. (-2741)
jamesk @ /Users/jamesk/desktop@Xmac-->./abook email james.kelly@wap.org
## Component Manager: attempting to find symbols in a component alias of type (regR/carP/x!bt)
syntax error: Expected class name but found identifier. (-2741)
jamesk @ /Users/jamesk/desktop@Xmac-->



[ Reply to This | # ]
update - improved results
Authored by: macman13 on Jun 09, '03 04:47:39PM

From what I see this returns the name of an email or phone number. Would it not be better to have the search name based and have it return the phone number? Or am I missing something here?

Thanks.
SA

---
\\\"I can do everything on my Mac I used to do on my PC, plus alot more ...\\\"
--Me



[ Reply to This | # ]
Name Lookup
Authored by: Krioni on Jun 09, '03 05:58:38PM
You're right. This version lets you search by partial-name (asking for email or phone), returning a list of all the possible matches and their email/numbers:

#!/bin/sh

if [ $# -ne 2 ]; then
    echo 1>&2 "usage: abook email (or phone) NAME"
    exit 127
fi

scriptcode="
tell application \"Address Book\"
        set idList to get (id) of (every person whose (name contains \"$2\"))
        set valueList to {}
        repeat with oneID in idList
                set oneID to contents of oneID
                set dataList to (get (every $1) of person id oneID)
                repeat with oneValue in dataList
                        copy (name of person id oneID & tab & label of oneValue & tab & value of oneValue) to end of valueList
                end repeat
        end repeat
end tell

set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ASCII character 10}
set lineList to (items of valueList) as string
set AppleScript's text item delimiters to od
return lineList
"

osascript -e "$scriptcode"

exit 0


[ Reply to This | # ]
Name Lookup - caveat
Authored by: Krioni on Jun 09, '03 06:06:13PM

Of course, if you want to search Business records by the person's name, you'll have to search by that, too. For now, it searches by the card's name, which would be the Company name if you've specified the card as a Business.



[ Reply to This | # ]
Name Lookup
Authored by: macman13 on Jun 09, '03 06:40:55PM

Just to rip you off slightly (since I am not a very experienced programmer):

#!/bin/sh

if [ $# -ne 2 ]; then
echo 1>&2 "usage: abook phone YOUR_SEARCH"
exit 127
fi

scriptcode="tell application \"Address Book\"
set thePerson to person \"$2\"
set theProps to the properties of thePerson
set the firstName to the first name of theProps
set the lastName to last name of theProps
set the phnList to the value of every phone of thePerson
set testList to firstName & \" \" & lastName & \" \" & phnList
return testList
end tell"

osascript -e "$scriptcode"

exit 0


This borrows from your script and a safari script to display the phone numbers of the person being named.

ie.
abook phone "Desired Name"

Displays:

Desired Name Phone number

This should do the same as the pb script except that you would still have to add a new name and phone number using the addressbook gui. But this would find a phone number in the terminal from a name.

Any more ideas?

Thanks.
SA

---
\\\"I can do everything on my Mac I used to do on my PC, plus alot more ...\\\"
--Me



[ Reply to This | # ]
hazzah!
Authored by: macubergeek on Jun 09, '03 09:58:09PM

now this works!
many thanks!



[ Reply to This | # ]
Name Lookup
Authored by: macubergeek on Jun 09, '03 08:02:33PM

formatting on this is unuseable...sorry but I'm at a loss for reconciling what I see on the web page with something that will work in shell.



[ Reply to This | # ]
Name Lookup
Authored by: Krioni on Jun 09, '03 08:37:22PM

weird...what web browser are you using? I used the code tag for posting here, as required.



[ Reply to This | # ]
A command-line-only basic phonebook script
Authored by: macman13 on Jun 10, '03 08:46:32AM
Here, let's see if this fixes the format question:

#!/bin/sh

if [ $# -ne 2 ]; then
    echo 1>&2 "usage: abook phone YOUR_SEARCH"
    exit 127
fi

scriptcode="tell application \"Address Book\"
	set thePerson to person \"$2\"
	set theProps to the properties of thePerson
	set the firstName to the first name of theProps
	set the lastName to last name of theProps
	set the phnList to the value of every phone of thePerson
	set testList to firstName & \" \" & lastName & \" \" & phnList
	return testList
end tell"

osascript -e "$scriptcode"

exit 0 

Let Me know if that helps.

Thanks.

SA

---
\\"I can do everything on my Mac I used to do on my PC, plus alot more ...\\"
--Me

[ Reply to This | # ]

a command line address book written in Objective-C
Authored by: semios on Jun 11, '03 02:19:22PM
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 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@axisofevil.org
George W Bush         202-276-4300 Bush43              big.dubya@whitehouse.gov
[...]


[ Reply to This | # ]
awesome!!!
Authored by: macubergeek on Jun 13, '03 09:47:37AM

absolutely awesome dude!!!!
-i am not worthy-- ;-)

everyone ya gotta get this neato cool little tool!!!



[ Reply to This | # ]
A command-line-only basic phonebook script
Authored by: Joey1978 on Aug 27, '10 11:20:38AM

I know this hint is 7 years old, but for future readers:
grep takes a file name so the use of cat is useless in the original script
AND
there is a program named abook already http://abook.sourceforge.net/
My $0.02 not adjusted for inflation.



[ Reply to This | # ]