Add/Remove Group membership Names to Contacts notes.
This Applescript identifies which Group(s) a Contact is listed in and adds the details to Contacts notes. It also allows for the removal of duplicate note entries.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################# | |
# Title: Add-Remove Group Name(s) or delete duplicates from Contacts Notes | |
############################################################# | |
#Iain Dunn | |
#https://www.logic2design.com | |
#https://twitter.com/Logic2Design | |
#logic2design@icloud.com | |
############################################## | |
# Code | |
############################################## | |
tell application "Contacts" | |
set o to display dialog "Do you want to add Group(s), remove Group(s) or remove duplicate records from Contacts Notes?" buttons {"Add", "Remove", "Duplicates"} default button "Add" | |
if the button returned of o is "Add" then | |
set c to display dialog "Do you want to change all Contacts or just the selected Contact?" buttons {"All", "Selected"} default button "Selected" | |
if the button returned of c is "Selected" then | |
set thePeople to selection | |
else | |
set thePeople to people | |
end if | |
repeat with aPerson in thePeople | |
set oldNotes to note of aPerson | |
set theGroups to {} | |
repeat with aGroup in (groups whose people contains aPerson) | |
set theGroups to theGroups & (name of aGroup) | |
end repeat | |
if theGroups is not {} then | |
set AppleScript's text item delimiters to ", " | |
if oldNotes is not missing value then | |
set note of aPerson to oldNotes & return & "Groups: " & (theGroups as string) | |
else | |
set note of aPerson to "Groups: " & (text items of theGroups as string) | |
end if | |
set AppleScript's text item delimiters to " " | |
end if | |
end repeat | |
save | |
else if the button returned of o is "Remove" then | |
set c to display dialog "Do you want to change all Contacts or just the selected Contact?" buttons {"All", "Selected"} default button "Selected" | |
if the button returned of c is "Selected" then | |
set thePeople to selection | |
else | |
set thePeople to people | |
end if | |
repeat with i in thePeople | |
set fileText to note of i | |
set thisNote to note of i | |
if thisNote is not missing value then set note of i to my deleteLinesFromText(thisNote) | |
end repeat | |
save | |
else | |
set c to display dialog "Do you want to change all Contacts or just the selected Contact?" buttons {"All", "Selected"} default button "Selected" | |
if the button returned of c is "Selected" then | |
set thePeople to selection | |
else | |
set thePeople to people | |
end if | |
repeat with i in thePeople | |
set thisNote to note of i | |
if thisNote is not missing value then set note of i to my removeDuplicateLines(thisNote) | |
end repeat | |
save | |
end if | |
end tell | |
############################################## | |
# Functions | |
############################################## | |
on deleteLinesFromText(theText) | |
-- here's how you can delete all lines of text from fileText that contain the deletePhrase. | |
-- first turn the text into a list so you can repeat over each line of text | |
set textList to paragraphs of theText | |
set deletePhrase to "Group" | |
-- now repeat over the list and replace lines that have the deletePhrase with 'missing values'. | |
repeat with i from 1 to (count textList) | |
if (item i of textList contains deletePhrase) then set item i of textList to missing value | |
end repeat | |
-- Coerce the paragraphs which are left to a single text using return delimiters. | |
set astid to AppleScript's text item delimiters | |
set AppleScript's text item delimiters to return | |
set newText to textList's text as text | |
set AppleScript's text item delimiters to astid | |
return newText | |
end deleteLinesFromText | |
on removeDuplicateLines(t) | |
set l to paragraphs of t | |
set tc to count l | |
if tc = 1 then return t | |
set newList to {} | |
repeat with j from 1 to tc | |
if (item j of l) is not in newList then set end of newList to item j of l | |
end repeat | |
set {oTid, text item delimiters} to {text item delimiters, return} | |
set newList to newList as string | |
set text item delimiters to oTid | |
return newList | |
end removeDuplicateLines |
Comments
Post a Comment