This is a very quick edit of one of my old scripts. So in all likelihood can be improved upon
It uses Addressbook plugins.
You will need to create one script for each tag, and place them in the Addressbook Plugins Folder.
This one is for setting ‘Private’.
Change:
property TagName : “Private”
To the tag need for the each script.
change
property tagList : {“Work”, “Newsletter”, “Etc”}
to the list of the other tags, excluding the one in TagName
The script will use the tagList to replace the current tag if it exists, and also it should keep any other notes you have entered in the Note area.
The property infoArea : “address”, determine which type label the script will work on.
In this case an address type label.
You can change this to "phone’ and then further define it in the on action title for p with e.
There is a beep when completed. You can remove this.
the serach and replace is case sensitive.
property tagList : {"Work", "Newsletter", "Etc"}
property TagName : "Private"
property infoArea : "address"
using terms from application "Address Book"
on action property
return infoArea
end action property
on action title for p with e
--if label of e contains "home" then
return TagName
--end if
end action title
on should enable action for p with e
return true
end should enable action
on perform action for p with e
my setNote(e)
--beep
--set note of the_card to the_note & return & "Private" as string
end perform action
end using terms from
on setNote(e)
set replace_text to TagName
tell application "Address Book"
set Tpip to item 1 of (get selection)
set tagNote to note of Tpip
if tagNote is missing value then
set note of Tpip to TagName
else
repeat with i from 1 to number of items in tagList
set replacedTxt to item i of tagList
if tagNote contains replacedTxt then
set notes to my replaceText(tagNote, replacedTxt, replace_text)
set note of Tpip to notes
exit repeat
else
set note of Tpip to tagNote & space & TagName
end if
end repeat
end if
save addressbook
---
end tell
beep
end setNote
on replaceText(completeTxt, replacedTxt, replace_text)
set completeTxt to completeTxt & " " -- adds a space to words on the end of the line or they will not be matched
set completeTxt to do shell script "echo " & quoted form of completeTxt & "|sed -e 's/" & quoted form of replacedTxt & "/" & quoted form of replace_text & "\\ /g'"
return completeTxt
end replaceText
Here is a second script for the “newsletter” just to show the simple changes needed for each Tag.
property tagList : {"Work", "Private", "Etc"}
property TagName : "Newsletter"
property infoArea : "address"
using terms from application "Address Book"
on action property
return infoArea
end action property
on action title for p with e
--if label of e contains "home" then
return TagName
--end if
end action title
on should enable action for p with e
return true
end should enable action
on perform action for p with e
my setNote(e)
--beep
--set note of the_card to the_note & return & "Private" as string
end perform action
end using terms from
on setNote(e)
set replace_text to TagName
tell application "Address Book"
set Tpip to item 1 of (get selection)
set tagNote to note of Tpip
if tagNote is missing value then
set note of Tpip to TagName
else
repeat with i from 1 to number of items in tagList
set replacedTxt to item i of tagList
if tagNote contains replacedTxt then
set notes to my replaceText(tagNote, replacedTxt, replace_text)
set note of Tpip to notes
exit repeat
else
set note of Tpip to tagNote & space & TagName
end if
end repeat
end if
save addressbook
---
end tell
beep
end setNote
on replaceText(completeTxt, replacedTxt, replace_text)
set completeTxt to completeTxt & " " -- adds a space to words on the end of the line or they will not be matched
set completeTxt to do shell script "echo " & quoted form of completeTxt & "|sed -e 's/" & quoted form of replacedTxt & "/" & quoted form of replace_text & "\\ /g'"
return completeTxt
end replaceText
Place all the script into the Addressbook Plugins folder.
You will need to quit and relaunch AB.
Getting the note and indeed the search and replace can be improved. and i would do it if I had time…