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


Click here to return to the 'Command Line lookup for Address Book' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
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 | # ]