Addressbook tagging

Hi,

I need a script that makes it possible to tag contacts in my addressbook.

What I want is to right-click a contact.
Pop-up shows with option TAG.
Option TAG should have different options like

  • Private
  • Work
  • Newsletter
  • Etc.

When you click an option this word will be added to the contacts note field.
So when you click Private, the word private will be added to note field of the contact you clicked.

I don’t know where to begin…but hope it’s possible.
Best,
Joost

You can’t create a right-click menu with applescript, but you can run some commands for the currently selected contact in address book. Try something like this script. Select a contact and run it. You can add the script to the script menu or even create a keyboard shortcut for it with one of the many programs available (I use Butler).

set myChoices to {"Private", "Work", "Newsletter"}

tell application "Address Book"
	set selectedContact to item 1 of (get selection)
end tell

choose from list myChoices with title "Choose From The List" with prompt "PickOne" default items (item 1 of myChoices) OK button name "Select" cancel button name "Quit"
tell result
	if it is false then error number -128 -- cancel
	set choice to first item
end tell

tell application "Address Book"
	set note of selectedContact to ((get note of selectedContact) & return & choice)
end tell

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…