这是一个例子。仅在我制造重复字段的两个联系人上进行了测试。并将它们放在一个组中进行测试
这应该让您了解构建代码的一种方法。
这个例子就是这样,因为我不想花太多时间在上面。但在我的测试中工作。
该示例将删除联系人中重复的社交资料和电话号码字段。
在社交资料中重复删除操作。这是因为第一次删除会强制 contacts.app 将条目替换为社交媒体网站的 URL。
从这里开始,其他领域的建设应该相对容易。
每组字段,即电话、社交资料、电子邮件都应该有自己的运行处理程序,以使代码更易于管理。就像我在这里所做的那样。
您还应该注意,对大量联系人执行此操作可能需要很长时间。
在玩任何这些之前,您还应该备份通讯录联系人。使用风险自负
set targetGroup to "BD"
tell application "Contacts" to set thePeople to people of group targetGroup -- TEST GROUP OF PEOPLE
---- tell application "Contacts" to set thePeople to people -- ALL PEOPLE
(* Run the Handler for Social profiles*)
my deleteSocialEntries(thePeople)
(* Run the Handler for phones *)
my deleteSocialPHONE(thePeople)
(* Handlers*)
on deleteSocialPHONE(thePeople)
tell application "Contacts"
(* iterate over the people - get each person in the contacts*)
repeat with i from 1 to number of items in thePeople
set this_person to item i of thePeople
(*get all the phones of the person*)
set theFields to (properties of phones of this_person)
set keepers to {}
(* iterate over the phones and get a phone's value*)
repeat with i from 1 to number of items in theFields
set this_item to item i of theFields
set thisValue to value of this_item
if thisValue is not in keepers then
(* Not in the list of keepers so add it*)
copy thisValue to end of keepers
else
(* Was already in the list of keepers try and delete it*)
try
set thisID to id of this_item
set thisD to (every phone of this_person whose id is thisID)
delete (item 1 of thisD)
save
end try
end if
end repeat
end repeat
end tell
end deleteSocialPHONE
on deleteSocialEntries(thePeople)
set social to {"Twitter", "Linkedin", "Facebook"}
tell application "Contacts"
(* iterate over the people - get each person in the contacts*)
repeat with i from 1 to number of items in thePeople
set this_person to item i of thePeople
(* iterate over the social media types for this person *)
repeat with i from 1 to number of items in social
set this_soc to item i of social
(*get all the *type* of social profiles of the person*)
set theFields to (properties of every social profile of this_person whose service name is this_soc)
set keepers to {}
(* iterate over the ocial profile and get a user name value*)
repeat with i from 1 to number of items in theFields
set this_item to item i of theFields
set thisValue to user name of this_item
if thisValue is not in keepers then
copy thisValue to end of keepers
else
(* Was already in the list of keepers try and delete it*)
set thisID to id of this_item
set thisD to (every social profile of this_person whose id is thisID)
delete (url of item 1 of thisD)
delete (user name of item 1 of thisD)
delete (url of item 1 of thisD)
delete (user name of item 1 of thisD)
save
end if
end repeat
end repeat
end repeat
end tell
end deleteSocialEntries