使用Applescript从Mac地址簿中删除地址字段

时间:2009-12-27 09:15:37

标签: macos applescript addressbook

Facebook Sync应用程序填写了我的Mac地址簿联系人的地址字段。有大量无用地址的人很难在谷歌地图应用上搜索人员(最后滚动浏览许多人 - 我只想看到那些输入了正确地址的人)。

我想使用applescript清除地址簿中的所有家庭住址字段。我写了一些小东西,但无法让它工作,可能需要一些知道applecript的人的帮助:)

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

我试图从其他脚本中删除多个地址/电话的逻辑,但只能找到添加它们,而不是删除它们。

谢谢! :)

2 个答案:

答案 0 :(得分:1)

/*

 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;
    }
  }
}

答案 1 :(得分:0)

您的逻辑是合理的,如果您将remove替换为delete,可能会有效,但您可以进一步缩小它;您实际需要的只是以下简单的1.5-liner:

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

我通过查看Trevor's AppleScript Scripts中的“删除标签的电子邮件”脚本来解决这个问题,他使用delete来删除特定的电子邮件地址(似乎remove是{{1}}删除整个地址卡,而不是它们的碎片),并通过一些实验来缩小它(这就是我发现AppleScript编程总是进行的方式......)。