用于删除同一联系人卡片中重复字段的Applescript

时间:2013-09-14 18:56:50

标签: macos applescript addressbook

我注意到我的OS X 10.8.5联系人中有很多(数百个)重复的字段名称和值。 “字段名称和值”可能不是正确的单词,但我的意思是,我有很多看似相同字段的联系卡:

enter image description here

问题不仅限于社交个人资料。此外,电子邮件地址手机,(可能还有更多)会遭受重复。

我昨天得到了AppleScript的介绍,虽然我确实得到了它(可以运行它们,可以使用现有的脚本来玩,知道save :)),但这仍然超出了我的能力:

如果这些AppleScript具有相同的类型(?)相同的标签(?)相同的值(?)。(但保留一个实例。)

我可以想象地址(包括街道,邮政编码等)可能会要求太多。但是,非复合“字段”已经显着地清理了我的地址簿。

道歉,肯定没有正确的技术术语。

1 个答案:

答案 0 :(得分:1)

这是一个例子。仅测试了我制作重复字段的两个联系人。并将它们放在一组进行测试

这可以让您了解构建代码的一种方法。

这个例子就是这样,因为我不想花太多时间在上面。但是在我的测试中工作。

该示例将删除联系人中的重复社交个人资料和电话号码字段。

在社交个人资料中,重复删除操作。这是因为第一次删除会强制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