We use cookies to provide you with a better experience. If you continue to use this site, we'll assume you're happy with this. Alternatively, click here to find out how to manage these cookies

hide cookie message
 

Ask the Script Doctor: Deduping Contacts and more

Ask the Scripting Guy is a semi-regular column in which we solve user problems using AppleScript. If you have a problem you think we might be able to help with, send me an email and I'll consider it for publication.

In previous columns, I answered questions on scripting Mail and working with text. This time, I'm answering some questions I've received on a variety of topics.

Showing the Library

Q: In Lion and Mountain Lion, the Library folder is now hidden by default in my home directory.  I'd like to write a script that I can just double-click anytime I want to open that folder.

A: AppleScript can quickly access a ton of folders on your Mac, without requiring you to specify the actual path. To do this, you use the path to command and specify the folder you want, such as the Library, Preferences, Downloads, and so on. This command also lets you specify whether you want the folder in the user domain (your home folder), the local domain (the computer level), or the system domain (the system level). Here are some examples.

So, for example, to get the Library folder in your home folder: path to library folder from user domain. That's the equivalent of the alias Macintosh HD:Users:yourUserName:Library:. To get the Library folder at the computer level: path to library folder from system domain. That could replace the alias Macintosh HD:Library:. And to get the Library folder at the system level: path to library folder from local domain, for the alias Macintosh HD:System:Library:.

To create a script to open the Library folder, create a new AppleScript Editor document, enter the following script, and save it as an application.

tell application "Finder" activate open (path to library folder from user domain) end tell

Now, anytime you want to get to your Library folder, just double-click the saved application. (You can drag it into your Dock for one-click access.)

Deduping Contacts

Q: I've got a ton of Contacts on my Mac. Many of them have duplicate email addresses and phone numbers.  Is there a way I can use a script to clean these up, so I don't have to edit each one manually?

Before you attempt to script this on your own and risk possible data loss, you may want to check out some of the third-part utilities available in the Mac App Store, such as Contacts Cleaner. These third-party apps offer lots of additional useful features, such as duplicate contact detection, the ability to merge contacts, and more.

That said, if you prefer to write a script to do this, make sure you first make a backup copy of your contacts database. (File > Export > Contacts Archive) Even a little typo in a Contacts script could delete your entire contacts database.

The following script loops through any selected contacts, and cleans up duplicate phone numbers and email addresses. When run, it offers two options: You can choose to clean up duplicate phone numbers and emails only, or you can opt to clean up duplicate phone numbers and emails that have matching labels.

--Encourage the user to back up first, and explain that this may take a while. display alert "Make sure you backed up your Contacts before running this script!" message "Please note that this may take a while if you have alot of contacts." buttons {"Continue", "Cancel"} cancel button "Cancel" as warning --Determine whether the user wants to clear only duplicate numbers and emails, or duplicate numbers and emails with matching labels set clearValuesAndLabels to button returned of (display alert "Clearing Options" message "Do you want to clear:" & return & return & "(Option 1) duplicate numbers and emails only" & return & "(Option 2) duplicate numbers and emails with matching labels" buttons {"Cancel", "Option 1", "Option 2"} cancel button "Cancel") = "Numbers and Emails with Matching Labels" --Get a list of selected contacts tell application "Contacts" set theContactsToProcess to selection --Loop through the contacts repeat with aContact in theContactsToProcess tell aContact --Look for and clean any duplicate phone numbers cleanDuplicateValues(every phone, clearValuesAndLabels) of me --Look for and clean any duplicate email addresses cleanDuplicateValues(every email, clearValuesAndLabels) of me end tell end repeat end tell --Alert the user that the script is done. display alert "Done." --This handler looks for and cleans up a list of duplicate phone numbers or email addresses on cleanDuplicateValues(theElements, clearValuesAndLabels) set theCheckedElements to {} tell application "Contacts" repeat with anElement in theElements tell anElement if clearValuesAndLabels = true then set theCurrentValue to (label & " - " & value) else set theCurrentValue to (value) end if if theCheckedElements contains theCurrentValue then delete save else set end of theCheckedElements to theCurrentValue end if end tell end repeat end tell end cleanDuplicateValues

Getting started with AppleScript

Q: I think AppleScript and Automator are great tools, but I'm having trouble finding clear documentation.  What do you recommend for the "non-geek" who will use these tools just occasionally?

There's no substitute for user-to-user help, and there's no shortage of helpful scripters available. Visit MacScripter.net, a large scripting-specific forum, which is host to thousands of scripters. You'll probably find answers to almost anything by simply searching for a few keywords. If you do get stuck, go ahead and post a question. You'll probably have an answer within a few hours.

It's also a good idea to pick up a solid AppleScript book. At minimum, it can be used for quick reference when you get stuck. A great one for beginners is Apple Training Series: AppleScript 1-2-3 (Peachpit Press) by Sal Soghoian and Bill Cheeseman. Sal's the automation manager at Apple, and Bill's a highly respected AppleScript expert, so this is a must-have for any scripter's collection.

Have AppleScript questions? Send them to scriptguy@macworld.com.

IDG UK Sites

Amazon 3D smartphone release date, price and spec: The hologram phone?

IDG UK Sites

You're never alone with a clone: How the App Store got taken over by copycats

IDG UK Sites

PCs vs consoles: PCs still pwn when it comes to gaming (and everything else)

IDG UK Sites

The art of rebranding: Creative agency The Neighbourhood explains how & why it rebranded