NOOB - Tag contacts in Address Book with Quicksilver

Hi,

Okay, I’m a total noob so apologies to begin with (I’m sure I’m not aware of a ton of protocol so forgive me).

Here’s what I’m trying to do. I’d love to be able to tag multiple contacts in address book with Quicksilver.

I’ve been tagging itunes with a great script from Chris Brown and Jesse Newland called tagselected. Then I discovered a post here by Scott Paulus that tagged contacts in Address Book.

In typical noob fashion I attempted to make a franken-script out of the two of them & what do you know - it didn’t work!?! Of course, right? So I’m asking for any help.

What I’d love to do is:
Select multiple contacts in Address book.
In Quicksilver input text to become the tag(s)
Use an Applescript action to add the tag(s) to the end of the note field (ideally with a carriage return before them)

Here’s my feeble Franken-Script

using terms from application "Quicksilver"
	on process text theString
		tell application "Address Book"
			set thePeople to selection
			
			
			
			set tagString to my theString
			
			repeat with aPerson in thePeople
				set currentNote to the note of aPerson
				if currentNote is missing value then set currentNote to ""
				set newNote to currentNote & tagString
				set note of aPerson to newNote
			end repeat
			
		end tell
	end process text
end using terms from

Here’s the original TagSelected iTunes script that was one half of the monster:

(*
"tuneTag tag script"
written by Chris Brown:
	chrisbro@gmail.com
	http://christopholis.com
and Jesse Newland:
	jnewland@gmail.com
	http://jnewland.com

v1.2, may 2006

TuneTag
By Chris Brown “ chrisbro@gmail.com
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. More information can be found at http://creativecommons.org/licenses/by-nc-sa/2.5/ .
*)

global iTunes_is_running

using terms from application "Quicksilver"
	on process text theString
		tell application "System Events"
			set running_apps to (get name of processes)
		end tell
		if running_apps contains "iTunes" then
			set iTunes_is_running to true
		end if
		if iTunes_is_running then
			tell application "iTunes"
				if theString is "" then
					activate
					display dialog "No text entered. Please enter one or more tags and try again." buttons {"OK"} default button 1
				else
					set sel to selection
					set selected to ""
					if sel is not {} then
						repeat with theNewT in sel
							set tagList to my split(theString, " ")
							repeat with singleTag in tagList --> add each tag from quicksilver
								set cur_comment to theNewT's comment
								set currentTagList to my split(cur_comment, " ")
								set singleTag to singleTag as text
								set checkTag to "*" & singleTag
								set checkTag to checkTag as text
								ignoring case
									if currentTagList does not contain checkTag then
										if cur_comment is not equal to "" then
											set theNewT's comment to cur_comment & " *" & singleTag
										else
											set theNewT's comment to cur_comment & "*" & singleTag
										end if
									end if
								end ignoring
							end repeat
						end repeat
					else
						activate
						display dialog "No songs are currently selected. No tags saved." buttons {"OK"} default button 1
					end if
				end if
			end tell
		else
			activate
			display dialog "iTunes is not running. Please start iTunes and try again." buttons {"OK"} default button 1
		end if
	end process text
end using terms from

on split(someText, delimiter)
	set AppleScript's text item delimiters to delimiter
	set someText to someText's text items
	set AppleScript's text item delimiters to {""}
	return someText
end split

And here’s the tag Address book script I grabbed from this forum:

tell application "Address Book"
	set thePeople to selection
	display dialog "What tag would you like to append?" buttons {"Cancel", "Apply"} default button "Apply" default answer "[tag]"
	set dialogInfo to result
	
	set selectedButton to button returned of dialogInfo
	set tagString to text returned of dialogInfo
	
	if selectedButton is "Apply" then
		repeat with aPerson in thePeople
			set currentNote to the note of aPerson
			if currentNote is missing value then set currentNote to ""
			set newNote to currentNote & tagString
			set note of aPerson to newNote
		end repeat
	end if
end tell

Perfect world - I love the way the itunes script checks for each tag and splits them up.

Thanks in advance for any help!!!

Anthony

Okay given a little more rope I’ve managed to make this script work -

using terms from application "Quicksilver"
	on process text theString
		tell application "Address Book"
			set thePeople to selection
			
			
			
			set tagString to theString
			
			repeat with aPerson in thePeople
				set currentNote to the note of aPerson
				if currentNote is missing value then set currentNote to ""
				set newNote to currentNote & return & "[tag]" & tagString
				set note of aPerson to newNote
			end repeat
			
		end tell
	end process text
end using terms from

Now, of course, I get greedy and want to add the splitting of tags & the check tags step from the earlier script.

So here is my current script which, of course, returns nothing. Any thoughts? What am I missing?

using terms from application "Quicksilver"
	on process text theString
		tell application "Address Book"
			set thePeople to selection
			
			
			
			set tagString to my split(theString, " ")
			repeat with singleTag in tagString --> add each tag from quicksilver
				
				repeat with aPerson in thePeople
					set currentNote to the note of aPerson
					set currentTagList to my split(currentNote, " ")
					set singleTag to singleTag as text
					set checkTag to "[tag]" & singleTag
					set checkTag to checkTag as text
					ignoring case
						if currentTagList does not contain checkTag then
							if currentNote is missing value then set currentNote to ""
							set newNote to currentNote & return & "[tag]" & singleTag
						else
							set note of aPerson to currentNote & return & "[tag]" & singleTag
						end if
					end ignoring
				end repeat
			end repeat
		end tell
	end process text
end using terms from

on split(someText, delimiter)
	set AppleScript's text item delimiters to delimiter
	set someText to someText's text items
	set AppleScript's text item delimiters to {""}
	return someText
end split

Okay, more progress but not there yet - somebody chime in!

I’ve managed to get this to work - sort of. The script below will separate tags and input them into a contact that already has info in the note field. Here are the problems:
1)if the note field is blank - no love - nada script doesn’t output anything (so if I select 2 contacts one will return the info - blank one nothing)
2)if I rerun the same tag(s) on the same contact the script’s “checktag” function doesn’t catch the existing tag and doubles the same tag.

Any ideas?

Also is there a simple way to have the script indicate it has completed (a sound or display text - I know I know nothing about applescript and I’m sure this is easy)

Here’s the progress so far:

using terms from application "Quicksilver"
	on process text theString
		tell application "Address Book"
			set thePeople to selection
			set tagString to my split(theString, " ")
			repeat with singleTag in tagString --> add each tag from quicksilver
				
				repeat with aPerson in thePeople
					set currentNote to the note of aPerson
					set currentTagList to my split(currentNote, " ")
					set singleTag to singleTag as text
					set checkTag to "[tag]" & singleTag
					set checkTag to checkTag as text
					ignoring case
						if currentTagList does not contain checkTag then
							if currentNote is not equal to "" then
								set newNote to currentNote & return & "[tag]" & singleTag
								set note of aPerson to newNote
							else
								set newNote to currentNote & return & "[tag]" & singleTag
								set note of aPerson to newNote
							end if
						end if
					end ignoring
				end repeat
			end repeat
		end tell
	end process text
end using terms from

on split(someText, delimiter)
	set AppleScript's text item delimiters to delimiter
	set someText to someText's text items
	set AppleScript's text item delimiters to {""}
	return someText
end split

HELP!!! I’ve hit a wall. Any hints would be amazing!

Even a way to check the steps of the script. I’m skeptical that the whole split thing is working.

Thanks,

Anthony