Applescript and Address Book help please.

I have a unix script that incorporates Applescript to search the address book app and display aphone number in the terminal:


#!/bin/sh

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

scriptcode="tell application "Address Book"
	set thePerson to person "$1"
	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 

What I need to do now is add an applescript that allows a user to add a new person to the address book db. Any ideas how to do this?

Thanks.
SA :smiley:

What you have done is cool. Unfortunately it just returns an error code if there isn’t a perfect match for the name.

If you wanted to add a new entry to your address book, then try doing something like:


make new person with properties {first name:"Kevin", last name:"Meaney"}

for the elements of a person, hopefully somebody else who is a bit more cluey can help.

Kevin

Yep. :slight_smile:
This is one I use to add a sender from MailSmith as a new entry in the X address book:


tell application "Address Book"
	make new person at end of group "my_group" with properties {first name:"Joe", last name:"Shmoe", organization:"My, Inc."}
	make new email at end of person ("Joe" & " " & "Shmoe") of group "My, Inc." with properties {value:"me@meinc.com", label:"work"}
end tell

Why do:

rather than just:

Woops. That was just a typo/carry-over from my original script. I was concatonating for a reason in that one. Sorry.

Enjoy:


#!/bin/sh

findCode="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"

addCode="tell application "Address Book"
	set thePerson to make new person at end with properties {first name:"$2", last name:"$3"}
	make new phone at end of phones of thePerson with properties {label:"$4", value:"$5"}
end tell"
	
delCode="tell application "Address Book"
	delete person "$2"
end tell"

case $1 in
	h | help | -h | -help) 
		echo "Useage:"
  		echo "	To find a Phone Number: phone -f "FirstName LastName""
 		echo "	To add a Name and Phone Number to the phonebook do: phone add "FirstName" "LastName" "label" "123-456-789""
 		echo "	To remove a user from the phone book do: phone delete "FirstName LastName""
  		echo "	NOTE: Be sure to use quotes around entries"
  		;;
	f | find | -f | -find) 
		osascript -e "$findCode"
		#if ["$?" -ne "0"]; then
		#	echo "This name was not found in the Address Book database"
		#	echo "To add a Name and Phone Number to the phonebook do: phone add "FirstName" "LastName" "label" "123-456-789""
		#fi
		;;
	a | add | -a | -add) 
		osascript -e "$addCode"
		;;
	d | delete | -d | -delete) 
		osascript -e "$delCode"
		;;
esac

Any ideas on improving?

thanks.
SA :smiley:

I know this is not apple script related, but whatever way I try to cut and paste the shell script I get a whole lot of control codes as well. I am copying out of Safari.

Since others reading this get it to work, I thought I would ask how they do it. With yesterdays shorter script I just used vi to remove all the codes, but I got bored of that before getting anywhere near the end with the new longer script.

Kevin

It looks like the default view size of the forum has pushed code from one line to the next, Specifically:


echo "   To add a Name and Phone Number to the phonebook do: phone add "FirstName" "LastName" "label" "123-456-789"" 

and


   echo "To add a Name and Phone Number to the phonebook do: phone add "FirstName" "LastName" "label" "123-456-789"" 

So here is the code again in a friendlier cut and paste format:


 #!/bin/sh 
  
 findCode="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" 
  
 addCode="tell application "Address Book" 
    set thePerson to make new person at end with properties {first name:"$2", last name:"$3"} 
    make new phone at end of phones of thePerson with properties {label:"$4", value:"$5"} 
 end tell" 
     
 delCode="tell application "Address Book" 
    delete person "$2" 
 end tell" 
  
 case $1 in 
    h | help | -h | -help)  
       echo "Useage:" 
       echo "To find a Phone Number: phone -f "FirstName LastName"" 
        echo "To add a Name and Phone Number to the phonebook do: phone 
                      add "FirstName" "LastName" "label" "123-456-789"" 
        echo "To remove a user from the phone book do: phone delete "FirstName
                       LastName"" 
       echo "NOTE: Be sure to use quotes around entries" 
         ;; 
    f | find | -f | -find)  
       osascript -e "$findCode" 
       #if ["$?" -ne "0"]; then 
       #   echo "This name was not found in the Address Book database" 
       #   echo "To add a Name and Phone Number to the phonebook do: phone
                    add "FirstName" "LastName" "label" "123-456-789"" 
       #fi 
       ;; 
    a | add | -a | -add)  
       osascript -e "$addCode" 
       ;; 
    d | delete | -d | -delete)  
       osascript -e "$delCode" 
       ;; 
 esac 

If this still does not work, give me a holler and I’ll email it to you.

Good Luck.
SA