How to use AppleScript to Create a New Contact

AppleScript is an English based scripting language; Apple touts it is easy to use to automate certain actions on your Mac or the programs that run on your Mac computer. In this tutorial, we are going to show you how to create an AppleScript that will create a new contact.

How you Create a New Contact Today

Address Book, now called Contacts as of OSX Mountain Lion 10.8.x, is an Apple application that allows you to create and manage contacts. Here is how to create a new contact.

  • Open Contacts from the Apple Dock.
    1
  • Click the + sign to add a new contact.
    1
  • Enter the applicable new contact information and click Done.
    1

Introducing Applescript

In order to create an Applescript, you need an editor. Thankfully, Mac OS X comes pre-installed with a scripting editor.

  • Open Spotlight at the top right-­‐hand corner of your screen and type applescript.
    1
  • The following screen editor appears:
    1
  • Let’s write our first script as a newbie. Type beep 7 in the window as shown below:
    1
  • Click Run. Congratulations, you are now a hardcore programmer (lol).

Look Mom, My Computer Speaks
Type the following script: say “You are a strong and powerful programmer” using “victoria”

I had a lot of fun with this one. You can change the voices in System Preferences | Dictation and Speech. Available options are Kathy, Victoria, Vicki, Alex, Fred and Bruce.

Creating a Contact with Applescript

Now that we have had some fun, let’s get down to business and use Applescript to create a contact in OS X. As the author of this post, I can assure you that I am not a programmer but with a little research, I was able to create a script that will ask you the following:

    • First Name
    • Last Name
    • Email Address

Once that is complete, it creates the contact automatically. Let’s step through the creation of the script.

Creating Dialogues and Variables

Open Applescript Editor and start off by asking for a First Name, Last Name and email address and add the variable names.

1. Type the following in the AppleScript Window:

–User Inputs Create a Contact

display dialog “Enter a Name, please:

Please enter Contact’s First Name:” default answer “First Name” set ClientName to text returned of result

display dialog “Please Enter Contact’s Last Name:

Please enter Contact’s Last Name:” default answer “Last Name” set LastClientName to text returned of result

display dialog “Please enter Contact’s email address:

Please enter Contact’s Email:” default answer “xx@aol.com” set lastemail to text returned of result

2. Next, click Compile to make sure you typed your script correctly.
3. Click Run to test. You should get the following :

1

1

1

Let’s move on now and actually take the information we passed and create the contact.

Please type the following:

–Add to Address Book

  tell application "Contacts" 
        set thePerson to make new person with properties 
        {first name:ClientName, last name:LastClientName} 
       	tell thePerson 
         make new email at end of emails of thePerson with
         properties {label:"Work", value:lastemail}
        end tell
        save 
        end tell

In this portion of the script, we are telling the application Contacts to make a new person or contact with the information passed in the variable ClientName, LastClientName and Lastemail. Basically the things we enter in the first three boxes are passed to this area and a contact is created. The full script looks as follows:

–User Inputs to get Client details
display dialog “Enter a Name, please:

Please enter Contact’s First Name:” default answer “First Name”
set ClientName to text returned of result

display dialog “Please Enter Contact’s Last Name:

Please enter Contact’s Last Name:” default answer “Last Name”
set LastClientName to text returned of result

display dialog “Please enter Contact’s email address:

Please enter Contact’s Email:” default answer “xx@aol.com”
set lastemail to text returned of result

–Add to Address Book

  tell application "Contacts" 
        set thePerson to make new person with properties 
        {first name:ClientName, last name:LastClientName} 
       	tell thePerson 
         make new email at end of emails of thePerson with
         properties {label:"Work", value:lastemail}
        end tell
        save 
        end tell

Finally, File | Save and choose the File Format: Application. Click Save. Now double-click the file to add a new contact. Drag and drop this new App to your Mac Dock for easy access. You can make this as complex as you want. For this tutorial, we wanted to show you how to quickly create a contact using AppleScript. Begin experimenting and look out for more Applescript tutorials.

If you found this blog post interesting, please check out, “How to Use Mac Automator to Export Contacts to Excel.”

1

Scrubly sign-up.