Remove address fields from Mac address book using Applescript
A Facebook Sync app filled the address fields of my Mac Address Book contacts with their cities. Having tons of people who have u开发者_JAVA百科seless 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! :)
/*
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;
}
}
}
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…).
精彩评论