Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Batch append data to the Notes field on Address Book cards Apps
I've been going crazy with Address Book lately -- ever since I started to Sync with Mobile Me. One thing I've been doing is creating Smart Group, but I've run into some pretty buggy behaviors; this was especially true for rules. I eventually discovered that the most reliable thing to do is to use one rule that looks for a special string in the Note field. However, adding that string to each card's Note field can be tedious, and I wanted to do this repeatedly for tons of cards, so I struggled with AppleScript until I had the following solution.
display dialog "Enter value to append to Notes of selected Address Book Cards" default answer "" buttons {"Cancel", "OK"} default button 2
set the append_string to the text returned of the result

tell application "Address Book"
  activate
  set thePeople to selection
  repeat with thePerson in thePeople
    set noteValue to note of thePerson
    
    try
      set theLastCharPos to length of noteValue
    on error
      set noteValue to ""
      set theLastCharPos to length of noteValue
    end try
    
    if theLastCharPos is greater than 0 then
      set theCharacter to character theLastCharPos of noteValue
      if "
" contains theCharacter then
        set newNoteValue to noteValue & append_string
      else if theLastCharPos is greater than 1 then
        set newNoteValue to noteValue & ";" & append_string
      else if theLastCharPos is equal to 1 and " " contains theCharacter then
        set newNoteValue to append_string
      else
        set newNoteValue to noteValue & ";" & append_string
      end if
    else
      set newNoteValue to append_string
    end if
    set note of thePerson to newNoteValue
  end repeat
end tell
Here's how to use this script: Paste this script into Script Editor. In Address Book, select the cards you want to append a note to, then run the script. Enter the string you want to append in the dialog box and click OK. (One feature I did not add is the ability to see if the string had already been added.)

If there's a script out there that does this already, I could not find it. Also, I'm not very familiar with AppleScript, so I know that there are some pretty dumb things I did in this script (improvements are encouraged in replies).

[robg adds: This worked as described in my testing.]
    •    
  • Currently 3.25 / 5
  You rated: 5 / 5 (20 votes cast)
 
[9,468 views]  

Batch append data to the Notes field on Address Book cards | 20 comments | Create New Account
Click here to return to the 'Batch append data to the Notes field on Address Book cards' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Batch append data to the Notes field on Address Book cards
Authored by: agentx on Mar 22, '10 07:48:03AM

A useful hint indeed.

A key things missing from Address Book is "Categories" used in most other programs.
Also coupled with the fact that Smart Groups has had glaring problems with various incarnations.

Still waiting for Mail, Address book and iCal in one simple window, i use BusyCal now for calendar as iCal was just messing me about too much ;-(



[ Reply to This | # ]
Batch append data to the Notes field on Address Book cards
Authored by: robleach on Mar 22, '10 07:57:19AM

I reported a bug to Apple shortly after submitting this hint. I discovered that if you set a rule in the smart group to "Card contains 'search string'", it doesn't always find all the cards with that string. "Note contains 'search string'" does appear to work in all cases. If you have "Card contains 'search string'", I discovered that if you manually edit *any* field of a card that the smart group missed, it will notice the string and add the card to the smart group. "Note contains 'search string'" will always work however, no manual editing required. This is just one of the bugs in Address Book smart groups that I've noticed. I've seen others but haven't been able to figure out the behavior.

Rob



[ Reply to This | # ]
Batch append data to the Notes field on Address Book cards
Authored by: Anonymous on Mar 22, '10 08:01:23AM
Great Hint.
What I did was use Automator to make this an Address Book Service:
- Open Automator, select New Service.
- Input = None, Select Address Book as the only application it will apply to.
- Add Run Applescript to your flow. Paste the script in that window (replace everything)
- Save the Service as a name of your choice. I used "Append Note to Selected Contacts"
Now in Address Book you can just select the contacts and go up to your Service menu to run the script. If you do this often, you can go to Keyboard Preferences in System Preferences and assign the service a keyboard shortcut. Thanks Again

[ Reply to This | # ]
Batch append data to the Notes field on Address Book cards
Authored by: lapfrog69 on Mar 22, '10 06:15:37PM

When I pasted the script onto Script Editor and tried to save (under Snow Leopard), it prompted me
Syntax Error
Expected end of line, etc. but found end of script

It won't save the script.

Anybody has a clue?



[ Reply to This | # ]
Batch append data to the Notes field on Address Book cards
Authored by: robleach on Mar 23, '10 06:19:19AM

I haven't tested it on 10.6, but I suspect it should work. Perhaps you missed the end of the script when you copied it? How many times did you attempt to copy, paste, and run it?



[ Reply to This | # ]
Batch append data to the Notes field on Address Book cards
Authored by: zoff on Mar 23, '10 09:37:23AM

Works fine here in 10.6.2.



[ Reply to This | # ]
Batch append data to the Notes field on Address Book cards
Authored by: davidhunt on Mar 23, '10 03:22:42PM

Hate to show what a newbie I am but automator gets me to the text box, and will add text to the box, but not to address book, in 10.6.2.
Have (Service receives "no input")in (address book).
Run applescript box contains ONLY pasted applescript.
Service does show up in applescript. No error messages. Any ideas?



[ Reply to This | # ]
Batch append data to the Notes field on Address Book cards
Authored by: gowings on Mar 25, '10 02:02:08PM

Same results here, 10.6.2., new iMac. Anyone...anyone...



[ Reply to This | # ]
Batch append data to the Notes field on Address Book cards
Authored by: jbinfrisco on Apr 08, '10 10:52:12AM

Can't seem to get this to work via Automator. New to both but followed instructions to the T. String will not post to contact notes section.



[ Reply to This | # ]
Batch append data to the Notes field on Address Book cards
Authored by: dopey on Apr 24, '10 10:34:16AM

Hi Rob,

I tried your script 10 times and couldn't get it to work. Apple Script Editor shows NO errors. It finds the person, correctly reads the current note and finishes with the set command. Yet no effect in address book. Any pointers? I am at a loss to understand what's going on.

OS X 10.6.3



[ Reply to This | # ]
Batch append data to the Notes field on Address Book cards
Authored by: robleach on Apr 28, '10 01:52:28PM

Well, I did note that it's not "live" updating. Let's try to obvious first: Sometimes I had to switch the cards I was viewing to see the updates. Try viewing different cards and then coming back to the one(s) you're trying to update or quit AB all together and restart. Are you selecting the cards you want updated? You must click on the set of cards you want to update in the name column of address book and run the script while they are selected.

Rob



[ Reply to This | # ]
Batch append data to the Notes field on Address Book cards
Authored by: dopey on Apr 29, '10 09:14:57AM

Hi Rob, I had already tried what you suggested and it does not work. There is simply no effect of the script for me. I have tried another Applescript which set the prefix value and that one showed immediate effects on the selected contacts. It did not append however, only set a value, overriding anything already present.

I am starting to think the notes field behaves differently to other fields. It's also the only field that can be edited without going into edit mode in the address book...



[ Reply to This | # ]
Batch append data to the Notes field on Address Book cards
Authored by: dopey on Apr 29, '10 09:34:40AM

Now I added a save command between your end repeat and end tell statements and that did it:)



[ Reply to This | # ]
Batch append data to the Notes field on Address Book cards
Authored by: robleach on Apr 30, '10 12:40:46PM

Post the code. No idea why the original didn't work on your machine. I believe there were other 10.6 users (I have 10.5) who said it worked for them. And what's this about a prefix? The code only appends.



[ Reply to This | # ]
Batch append data to the Notes field on Address Book cards
Authored by: misterlink on May 23, '10 04:41:22PM
Thanks for the tip! I tried it and added the command "save thePerson" like dopey suggested and it works fine. He suggested putting the save command between the "end repeat" and "end tell" statements which works, but it seemed to me like the command should be just before the "end repeat" statement so it applies to each person in each iteration of the repeat loop. Either method works (which doesn't make sense to me), but oh well. One thing to note is that the "Result" value in the AppleScript Editor displays "missing value". Everything still works, but I'm curious to know why it is giving that error. Here is my code that works:

display dialog "Enter value to append to Notes of selected Address Book Cards" default answer "" buttons {"Cancel", "OK"} default button 2
set the append_string to the text returned of the result

tell application "Address Book"
	activate
	set thePeople to selection
	repeat with thePerson in thePeople
		set noteValue to note of thePerson
		
		try
			set theLastCharPos to length of noteValue
		on error
			set noteValue to ""
			set theLastCharPos to length of noteValue
		end try
		
		if theLastCharPos is greater than 0 then
			set theCharacter to character theLastCharPos of noteValue
			if "" contains theCharacter then
				set newNoteValue to noteValue & append_string
			else if theLastCharPos is greater than 1 then
				set newNoteValue to noteValue & ";" & append_string
			else if theLastCharPos is equal to 1 and " " contains theCharacter then
				set newNoteValue to append_string
			else
				set newNoteValue to noteValue & ";" & append_string
			end if
		else
			set newNoteValue to append_string
		end if
		set note of thePerson to newNoteValue
	save thePerson
	end repeat
end tell
For what it's worth, I found this page trying to figure out a way to delete all notes from selected Address Book entries. I modified the code above to do this, and it works like a charm (but also returns the "missing value" note in the AppleScript editor):

tell application "Address Book"
	activate
	set thePeople to selection
	repeat with thePerson in thePeople
		set note of thePerson to ""
		save thePerson
	end repeat
end tell


[ Reply to This | # ]
Batch append data to the Notes field on Address Book cards
Authored by: tedw on May 23, '10 07:49:01PM
here's an expanded and somewhat cleaner version of the script. This allows adding or deleting individual notes, as well as clearing the entire note list for the selected items.
property delim : ";" -- this is the delimiter used to separate notes.

set theDialogResult to display dialog "Enter note value, and select Add or Delete to add/remove that note from the selected Address Book cards." default answer "" buttons {"Add", "Delete", "Cancel"} default button 1
set {theAction, theString} to {button returned, text returned} of theDialogResult
if theString is "" then
	-- if it's an empty string skip for adds, confirm clear all for deletes
	if theAction is "Add" then return
	if theAction is "Delete" then
		display alert "Delete all?" message "Are you sure that you want to clear all notes from the selected messages?" buttons {"OK", "Cancel"} default button "Cancel" as warning
		if button returned of the result is "Cancel" then return
	end if
end if

tell application "Address Book"
	set thePeople to selection
	repeat with thePerson in thePeople
		set noteValue to note of thePerson
		if noteValue is in {missing value, space} then set noteValue to ""
		set {tid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, delim}
		if theAction is "Add" then
			set noteList to text items of noteValue
			if theString is not in text items of noteValue then
				set end of noteList to theString
			end if
		else if theAction is "Delete" then
			set noteList to {}
			if theString is not "" then
				repeat with thisNote in (text items of noteValue)
					set thisNote to contents of thisNote
					if thisNote is not theString then
						set end of noteList to thisNote
					end if
				end repeat
			end if
		end if
		set newNoteValue to noteList as text
		set AppleScript's text item delimiters to tid
		set note of thePerson to newNoteValue
	end repeat
	save
end tell
The reason you were getting a 'missing value' result in Script Editor is that the 'save' command is a shortcut for 'save addressbook', and is used to save the entire book. It does not return a result (hence the 'missing value' result that you're seeing). I'm actually surprised your version not throwing an error - 'save thePerson' is meaningless in context - but no sense looking a gift horse in the mouth.

[ Reply to This | # ]
Batch append data to the Notes field on Address Book cards
Authored by: misterlink on May 23, '10 08:50:06PM

Thanks for posting - that's handy to have the add and delete functionality combined. I tried the code and it works great, but still gives me the same "missing value" error. Could it just be something with my machine? I wouldn't think so, but you never know...



[ Reply to This | # ]
Batch append data to the Notes field on Address Book cards
Authored by: tedw on May 23, '10 08:54:13PM

as I said, the 'missing value' comes from the 'save' line. in Address Book, the 'save' command returns no value (i.e., it gives 'missing value'), and since it's the last active line in the script that's what the script returns when you run it. don't worry about it. if it really bugs you, add the line "return" at the end of the script, and you'll get a different value.



[ Reply to This | # ]
Batch append data to the Notes field on Address Book cards
Authored by: pastorsoh on Aug 17, '10 03:07:27PM

Thank you very much. Very useful script!

I am wondering if you can help me with my problem. ^^

I need to append data to the Notes field but not just data.
Would it be possible to copy data from a specific field in the address card to append it in the Notes?

For example...
I need to put "spouse" name in Notes since iPhone does not show "spouse" field in its address book.
It will be really helpful if I can see it in the notes instead.

Is it possible at all? Thanks!



[ Reply to This | # ]
Batch append/remove data in the Notes field on Address Book cards
Authored by: reeks on Feb 28, '12 02:21:48PM

I combined two scripts to be able to batch remove strings from the notes but leave the rest of the notes intact. If you remove nothing, it will remove the entire note (as the script in the comments above.

I find this to be useful in combination with smart groups. Use at your own risk of course.

property delim : ";" -- this is the delimiter used to separate notes.

set theDialogResult to display dialog "Enter note value, and select Add or Delete to add/remove that note from the selected Address Book cards." default answer "" buttons {"Add", "Remove", "Cancel"} default button 1
set {theAction, theString} to {button returned, text returned} of theDialogResult
if theString is "" then
-- if it's an empty string skip for adds, confirm clear all for deletes
if theAction is "Add" then return
if theAction is "Remove" then
display alert "Delete all?" message "Are you sure that you want to clear all notes from the selected messages?" buttons {"OK", "Cancel"} default button "Cancel" as warning
if button returned of the result is "Cancel" then return
end if
end if

tell application "Address Book"
set thePeople to selection
repeat with thePerson in thePeople
if theAction is "Add" then
set noteValue to note of thePerson
if noteValue is in {missing value, space} then set noteValue to ""
set {tid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, delim}
set noteList to text items of noteValue
if theString is not in text items of noteValue then
set end of noteList to theString
end if
set newNoteValue to noteList as text
set AppleScript's text item delimiters to tid
set note of thePerson to newNoteValue
else if theAction is "Remove" then
set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, theString}
set n to text items of (get note of thePerson)
set AppleScript's text item delimiters to ASTID
set note of contents of thePerson to n as text
save thePerson
end if
end repeat
save
return
end tell



[ Reply to This | # ]