How do I pick phonenumbers from Address Book.app?

Hi!

I thought I’d do a quick Hylafax Client using Applescript. It basically works but it needs some improvements in the UI:

How do I implement a panel to pick the fax number(s) from Address Book.app? I was thinking of something like the address picker inside Mail.app.

Any suggestions?

Thanks,
Thorsten

Well, I finally managed to work out something that can pull that info. If a person has two emails, they are in the list for both of them. You can select multiple emails from the list.


set emailSelectList to {}
tell application "Address Book"
	set personIDList to (id of people) -- get a list of all the IDs
	repeat with onePersonID in personIDList
		try
			-- now, get a list of this person's email addresses
			set emailList to value of emails of person id onePersonID
			set personName to name of person id onePersonID
			repeat with oneEmail in emailList
				-- for each address, put the name and address in the list we're building
				copy """ & personName & "" " & "<" & oneEmail & ">" to end of emailSelectList
			end repeat
			
		on error errMsg number errNum
			return errMsg
		end try
	end repeat
end tell

-- you could make this allow only one choice, have a prompt etc
-- look in the Standard Additions dictionary for more info on 'choose from list'
set chosenEmailList to choose from list emailSelectList
return chosenEmailList

Oh, here’s an update. this one actually allows multiple selections, and then puts the chosen address(es) in the To field of the front message in Eudora. If that message already has some recipients, it makes a new message. Modify to fit your heart’s content. :slight_smile:

Oh, it would probably be a good idea to sort this thing in some way. I’d recommend looping over each id, getting the name, and making a new id list in last,first sorted order. Probably a quick-sort routine would work well. Search the archives for a quick-sort routine - I’ve got to get back to work. :slight_smile:


set emailSelectList to {}
tell application "Address Book"
	set personIDList to (id of people) -- get a list of all the IDs
	repeat with onePersonID in personIDList
		try
			-- now, get a list of this person's email addresses
			set emailList to value of emails of person id onePersonID
			set personName to name of person id onePersonID
			repeat with oneEmail in emailList
				-- for each address, put the name and address in the list we're building
				copy """ & personName & "" " & "<" & oneEmail & ">" to end of emailSelectList
			end repeat
			
		on error errMsg number errNum
			return errMsg
		end try
	end repeat
end tell

-- you could make this allow only one choice, have a prompt etc
-- look in the Standard Additions dictionary for more info on 'choose from list'
set chosenEmailList to choose from list emailSelectList with prompt "Choose recipient(s):" with multiple selections allowed

log chosenEmailList


tell application "Eudora"
	activate
	if (field "To:" of message 0) is not equal to "To: " then
		make new message at end of mailbox "Out"
	end if
	set field "To:" of message 0 to my unParseChars(chosenEmailList, ", ")
end tell



on unParseChars(thisList, newDelim)
	-- version 1.2, Daniel A. Shockley, http://www.danshockley.com
	set oldDelims to AppleScript's text item delimiters
	try
		set AppleScript's text item delimiters to the {newDelim as string}
		set the unparsedText to thisList as string
		set AppleScript's text item delimiters to oldDelims
		return unparsedText
	on error errMsg number errNum
		try
			set AppleScript's text item delimiters to oldDelims
		end try
		error "ERROR: unParseChars() handler: " & errMsg number errNum
	end try
	
end unParseChars

OK. Final post by me for now. I looked up a sort handler (from the ScriptBuilders part of macscripter.net), and modified how the email list is built. I get the names as last, first and also had to error-trap for missing first/last anmes, since Address Book doesn’t even SET variables if the value is missing. Annoying, non-standard behavior. Anyway, with that done, I’m only sorting the actual list of names and emails, rather than the whole AB first.


set emailSelectList to {}
tell application "Address Book"
	set personIDList to (id of people) -- get a list of all the IDs
	
	repeat with onePersonID in personIDList
		try
			-- now, get a list of this person's email addresses
			set emailList to value of emails of person id onePersonID
			
			set personLastName to last name of person id onePersonID
			set personFirstName to first name of person id onePersonID
			
			-- need to error-trap, since rather than returning "", empty values
			-- don't even bother to set the variable to ANYTHING
			try
				set personName to """ & personLastName & ", " & personFirstName & """
			on error errMsg
				try
					set personName to """ & personLastName & """
				on error errMsg
					try
						set personName to """ & personFirstName & """
					on error errMsg
						set personName to ""
					end try
				end try
			end try
			
			repeat with oneEmail in emailList
				-- for each address, put the name and address in the list we're building
				copy personName & " " & "<" & oneEmail & ">" to end of emailSelectList
			end repeat
		on error errMsg number errNum
			return errMsg
		end try
	end repeat
end tell

set emailSelectList to shellsort(emailSelectList)

-- you could make this allow only one choice, have a prompt etc
-- look in the Standard Additions dictionary for more info on 'choose from list'
set chosenEmailList to choose from list emailSelectList with prompt "Choose recipient(s):" with multiple selections allowed

--log chosenEmailList

tell application "Eudora"
	activate
	if (field "To:" of message 0) is not equal to "To: " then
		make new message at end of mailbox "Out"
	end if
	set field "To:" of message 0 to my unParseChars(chosenEmailList, ", ")
end tell

on unParseChars(thisList, newDelim)
	-- version 1.2, Daniel A. Shockley, http://www.danshockley.com
	set oldDelims to AppleScript's text item delimiters
	try
		set AppleScript's text item delimiters to the {newDelim as string}
		set the unparsedText to thisList as string
		set AppleScript's text item delimiters to oldDelims
		return unparsedText
	on error errMsg number errNum
		try
			set AppleScript's text item delimiters to oldDelims
		end try
		error "ERROR: unParseChars() handler: " & errMsg number errNum
	end try
end unParseChars

to shellsort(aList)
	(*
	from macscripter.net - ScriptBuilders
ShellSort  1.0  - simplified somewhat
Freeware | 3.1.01 | Script  #480 
Author: Dave Vasilevsky, mdmv@musica.mcgill.ca
*)
	copy {aList, 0, 0, aList's length} to {newList, Swaps, Comps, theLength}
	set Gap to theLength
	repeat while Gap is greater than 1
		set {Gap, Flag} to {Gap div 2, true}
		repeat while Flag is true
			set Flag to false
			repeat with Index1 from 1 to (theLength - Gap)
				set {Index2, Comps} to {Index1 + Gap, Comps + 1}
				--if Comps mod 1000 is 0 then log Comps
				if newList's item Index1 > newList's item Index2 then
					set {Swaps, newList's item Index1, newList's item Index2, Flag} ¬
						to {Swaps + 1, newList's item Index2, newList's item Index1, true}
				end if
			end repeat
		end repeat
	end repeat
	return newList
end shellsort