Applescript 从列表中删除联系人

Applescript to delete contacts from list

我不小心从一个朋友的 iPhone 通讯录中添加了大约 600 个联系人到我的 Mac 通讯录中,几周前我同步到我的 Mac。不幸的是,我没有我的联系人备份,现在他们都与我的 iPhone 和 iCloud 同步了。 我能够从我的朋友 phone 将所有不需要的联系人导出为“.vcf”文件,所以我有大约 600 个文件,例如 "John Doe.vcf",还有一个仅包含联系人姓名的纯文本列表。

理想情况下,我想要一个与 VCF 文件中的数据相匹配的脚本,以确保我删除的是正确的联系人。

想法 #2 是删除与列表中的姓名匹配的联系人,跳过 return 两个或更多结果的搜索(这样我就不会丢失与朋友同名的联系人)

我开始尝试使用以下 applescript 删除单个联系人,但没有成功。请帮忙!

set daName to "John Doe"

tell application "Contacts"
    repeat with onePerson in people
        if (value of name of onePerson) is daName then
            delete onePerson
        end if
    end repeat
end tell

--更新--

这是我用来执行任务的最终代码。我在顶部添加了一个循环,循环遍历剪贴板中复制的名称列表。我还在对话框中添加了重复联系人的姓名,以便我记下它。

提示:

最终代码

set the clipboard to (the clipboard as text)
set the_strings to the clipboard

repeat with this_string in paragraphs of the_strings
    set daName to this_string
    DeleteContact(daName)
end repeat


on DeleteContact(theName)
    tell application "Contacts"
        set myList to every person whose name is theName
        set Nb to count of myList
        if Nb = 0 then return -- no record found
        if Nb > 1 then
            display dialog theName & " (too many found: do nothing)"
        else
            set myperson to item 1 of myList -- only 1 found to be deleted
            delete myperson
            save
        end if
    end tell
end DeleteContact

下面的脚本包含 sub-routine 检查名称和删除。我添加了一些注释以使其更容易适应您的需要:您可以构建一个循环来查找您的卡并调用 DeleteContact sub-routine.

set daName to "John Doe"
DeleteContact(daName)


on DeleteContact(theName)
tell application "Contacts"
    set myList to every person whose name is theName
    set Nb to count of myList
    if Nb = 0 then return -- no record found
    if Nb > 1 then
        display dialog "too many found: do nothing"
    else
        set myperson to item 1 of myList -- only 1 found to be deleted
        delete myperson
        save
    end if
end tell
end DeleteContact

如果多个联系人中存在同名,我会显示一个对话框,但由您决定在此处做什么。

吉奥

这是我的脚本,它可能会解决您的问题,尽管您从未说出问题所在。

tell application "Contacts"
  local results,dlist,dloop
  set results to {}
  set dlist to people whose (first name  contains "Oxnard" or (last name  is equal to "Abercrombe"))
  repeat with dloop in dlist
    delete dloop
  end repeat
  set end of results to "Records deleted :" & count of dlist
  save
return results
end tell

请注意,联系人字典确实定义了 name 属性,但它将基于首选项设置。