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


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: 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 | # ]