Skip to content

Instantly share code, notes, and snippets.

@simX
Created February 10, 2012 23:21
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simX/1793942 to your computer and use it in GitHub Desktop.
Save simX/1793942 to your computer and use it in GitHub Desktop.
Add contact command line program
#!/bin/bash
osascript -e "tell application \"Address Book\"" \
-e " set firstParam to \"$1\"" \
-e " set secondParam to \"$2\"" \
-e " set thirdParam to \"$3\"" \
-e " set fourthParam to \"$4\"" \
-e " set contactEmailLabel to \"\"" \
-e " set spaceOffset to (offset of \" \" in firstParam)" \
-e " if (spaceOffset is not equal to 0) then" \
-e " set AppleScript's text item delimiters to \" \"" \
-e " set firstName to (text item 1 of firstParam)" \
-e " set lastName to (text item 2 of firstParam)" \
-e " set firstEmailParam to secondParam" \
-e " set secondEmailParam to thirdParam" \
-e " else" \
-e " set firstName to firstParam" \
-e " set lastName to secondParam" \
-e " set firstEmailParam to thirdParam" \
-e " set secondEmailParam to fourthParam" \
-e " end if" \
-e " set atOffset to (offset of \"@\" in firstEmailParam)" \
-e " if (atOffset is not equal to 0) then" \
-e " set contactEmail to firstEmailParam" \
-e " else" \
-e " set contactEmailLabel to firstEmailParam" \
-e " set contactEmail to secondEmailParam" \
-e " end if" \
-e " if (contactEmailLabel is equal to \"\") then" \
-e " set contactEmailLabel to \"Home\"" \
-e " end if" \
-e " set newPerson to (make new person with properties {first name:firstName, last name:lastName})" \
-e " make new email at end of emails of newPerson with properties {label:contactEmailLabel, value:contactEmail}" \
-e " save" \
-e " if (newPerson exists) then" \
-e " if (firstName exists) then" \
-e " set returnString to (\"First name: \" & firstName)" \
-e " end if" \
-e " if (lastName exists) then" \
-e " set returnString to (returnString & \"; Last name: \" & lastName)" \
-e " end if" \
-e " if (contactEmailLabel exists) then" \
-e " set returnString to (returnString & \"; Email label: \" & contactEmailLabel)" \
-e " end if" \
-e " if (contactEmail exists) then" \
-e " set returnString to (returnString & \"; Email address: \" & contactEmail)" \
-e " end if" \
-e " return returnString" \
-e " end if" \
-e "end tell"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment