views:

597

answers:

2

Hello,

A Facebook Sync app filled the address fields of my Mac Address Book contacts with their cities. Having tons of people who have useless addresses makes it difficult to search by people on Google Maps app (ending up scrolling through many many people- I only want to see those who have proper addresses entered).

I want to clear all the home address fields in my address book using applescript. I wrote something small but couldn't get it to work, probably needs some help from somebody who knows applescript :)

tell application "Address Book"
repeat with this_person in every person
        repeat with this_address in every address of this_person
            if label of this_address is "home" then
                remove this_address from addresses of this_person
            end if
        end repeat
     end repeat
 end tell

I tried to deduct the logic of multiple addresses/phones from other scripts but could only find adding them, not removing them.

Thanks! :)

A: 

Your logic is sound, and would probably work if you replaced remove with delete, but you can shrink it even further; all that you actually need is the following simple 1.5-liner:

tell application "Address Book" to ¬
    delete (addresses of people whose label is "home")

I figured this out by looking at the "Remove Emails for Label" script from Trevor's AppleScript Scripts, where he uses delete to get rid of specific email addresses (it seems like remove is for removing whole address cards, not pieces of them), and shrunk it with a little experimentation (which is how I find that AppleScript programming always proceeds…).

Antal S-Z
Thanks! This didn't give out any errors but didn't seem to produce any changes either :( I restarted AB and I still see the addresses.But I'll tinker a bit more based on this, thanks :)
cemregr
That's odd—it works on my system (in fact, it works well enough that I'm glad I made a backup of the address book first :) ). What version of OS X are you running? I'm running 10.5.8; maybe a mismatch there is the problem?
Antal S-Z
I think I may have figured out the problem, half a year later: you probably need to put a `save addressbook` within the `tell` block to force it to commit the changes.
Antal S-Z
+1  A: 
/*

 This program will remove the fb://profile links from your 
 contact cards. I would suggest creating an addressbook archive 
 first before running this against your actual contacts. The easiest
 way to use this is to search your contact cards for "profile" select
 all and export as a single vCard. Then compile and run this program with
 that vCard as input and specify an output file, say fbRemoved.vcf

 I found that AddressBook behaves oddly when I try to do a mass import
 selecting use new for all cards. To get around this I just deleted all 
 cards I had selected in the "profile" search then imported fbRemoved.vcf

 Written by: Alexander Millar
 Date: 30 Feb 2010

*/

#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <fstream>
#include <string>

using namespace std;

bool contains_fb_link(string str) {
  size_t found;
  found = str.find("fb\\://profile/");
  if(found!=string::npos) 
    { 
      return true;
  }
    return false;
}

int main( int argc, char *argv[] ) {
  istream *infile;
  ostream *outfile = &cout;
  string str;

  switch ( argc ) {
  case 3:
    outfile = new ofstream( argv[2] );
    if ( outfile->fail() ) {
      cerr << "Error! Could not open output file \"" << argv[2] << "\"" << endl;
      exit(-1);
    }
  case 2:
    infile = new ifstream( argv[1] );
    if ( infile->fail() ) {
      cerr << "Error! Could not open input filee\"" << argv[1] << "\"" << endl;
      exit(-1);
    }
    break;
  default:
    cerr << "Usage: " << argv[0] << " input-file [output-file]" << endl;
  }

  for ( ;; ) {
    getline(*infile,str);
    if( infile->eof() ) break ;

    if(contains_fb_link(str))
    {
      getline(*infile,str);
      if( infile->eof() ) break ;
    }
    else
    {
       *outfile << str << endl;
    }
  }
}
Welcome to StackOverfow! I didn't test your program but +1 for effort. Also, there's no February 30th.
Potatoswatter