Apple Mail - Add to Palm, not AddressBook

As the subject implies, I’d like to find a means to have an “Add to Palm Desktop” menu item instead of (or in addition to) “Add to AddressBook”. Ideally, the command would take the current eMail message’s sender name and eMail address, switch over to Palm Desktop, check for an existing entry and create a new Palm contact with the name & eMail address (or, if the entry already exists, report that it does exist and ask if I want to open it).

Suggestions appreciated. I’ve not done much AppleScripting but given enough hints and tips I might be able to craft it.

Thanks in advance!

Barry

Model: Intel iMac
AppleScript: 1.10.6 (framework#)
Browser: Safari 417.9.3
Operating System: Mac OS X (10.4)

Barry:

On my machine, every record in my Palm that has an email address keeps that address in the [custom one] portion of the address. To examine yours, try something like this:

tell application "Palm Desktop"
	get properties of address id 26-->Yields TONS of info on what is there for that address id
	get custom one of address id 26-->Yields Email data for my contacts
end tell

To get someone’s email address, this works:

tell application "Palm Desktop"
	get field text of address id 324's custom one
end tell

As you can see, Palm loves to refer to contacts by their address id numbers. To search the database for all email addresses, this should work:

tell application "Palm Desktop"
	set e_Mails to the id of every address whose custom one's field text = "casdvm@cs.com"
end tell

-->{324, 325}

That gives the id number of the address, so you can check on the rest of the data. Remember that it will return a list, regardless of how many records are found.

I hope this helps get you started.

Barry:

I’ve been playing around with this the last couple of days, and found something interesting. This little script will extract the sender’s information from a single selected email:

tell application "Mail"
	set theSelection to selection
	if ((count of theSelection) = 1) then
		tell item 1 of theSelection
			set theSender to sender
		end tell
	end if
end tell

The interesting part, is that sometimes, the sender information includes a name and email address, like this:

“Chad James Hoover chadh@xxxxxx.com

And sometimes, just an email comes through:

mishael_barton@xxxxxxxxx.com

The only issue for you is how to process the data once it is recieved, and it all depends on the format. It appears that whenever the < and > characters show up, you get a name with the email address, otherwise you just get the email.

Craig,

That’s an exciting find. As long as data ends up in a variable, I can parse through it to determine what’s there and move over to Palm to do the search and, optionally, the addition of a new record.

I’ve been reading through that “Absolute Starters” guide to AppleScript and have been following along with the script editor open. This shouldn’t be that difficult as I used to write some pretty complex HyperCard “apps” for doing labor estimations for the sewing industry. Even wrote an internal eMail system for my company back in '87 (HC’s birth, right?).

Thanks again for your help.

Regards,
Barry

Barry:

You are welcome. In fact, this whole idea of yours has stimulated me to the point that I have been trying different things out to see what will and won’t work. The best part is that once a clean email address is obtained, AppleScript can search the entire Palm database for it extremely fast. Once that address has been added to the correct field of the Palm OS, it updates the remaining fields automatically.

The only difficulty I have had so far really comes down to the Mail issue identified above. I am struggling to figure out how to efficiently set up a handler to process the Mail sender string, and preserve the sender’s name as well as the email address, if both are there. I thought that would be a neat way to make a new contact in the Palm, or search through the Palm contacts for that particular name, and just add the email to the contact. Just extracting the email, and then making a new contact is simple, but what if someone changes their email address? Or an already present contact gets an email that did not have one previously?

I really don’t want to make this monstrosity of a script, but the possibilities are certainly interesting.

I think the difference is because the sender either has or has not included his real name in the configuration of his eMail account. I ran the script on an eMail I received from a list-server and, sure enough, just the eMail address appears in the result.

So we’re really not going to know whether the sender’s name will be there. In addition, you don’t even know that the sender’s name matches what you might already have in Palm. So how do we approach this?

I started to outline a script but, as you’ve already noted, this could get really complex. How about we simplify this?

Let’s assume that Apple’s Addressbook won’t accidentally duplicate contacts and is smart enough to only add new eMail addresses Mail encounters to existing contacts. So Addressbook is the primary repository of addresses.

Now let’s look at Palm. If you already know the structure of both Addressbook and Palm Addresses, could a script be crafted that would examine every record in Addressbook, search for that record in Palm
IF it finds a match (probably based upon name), then it compares the two records AND synchronizes Palm & Addressbook as needed
ELSE (if no match, we have a new record)
Add the new record to Palm (name and eMail)
END IF

You’ll note I’ve used the word “synchronize” rather than “update Palm” simply because I’m assuming that Palm will be the main PIM and data we enter about physical address, etc. will probably be done there; so it would be good to update Addressbook records with Palm data. In this manner, a new record manually entered into Palm would find its way into Addressbook during the next “sync” (and I’m using “sync” to mean a software-only sync between the two databases, nothing to do with the actual Palm device which is beyond the scope of this issue).

One “problem” I see with this approach is the “add to Addressbook” event that only adds a record with an eMail address (like a list-serve eMail). In that case, the script would have to branch to an alternative search for the eMail address -AND- no name. Hmmm. May have to give this a bit more thought. However, if the record exists in Addressbook as an eMail address with no name (which happens as I just tested and verified), it probably should in Palm. We all have to police our databases for duplicates and erroneous records and then fix the problems. The next time we sync, what happens? Do the records already in Palm (but expunged from Addressbook get put back into Addressbook? Need some kind of configuration alternatives here. Perhaps part of the script is to copy the eMail address of “blank name” records into the last name field? Then no records will end up as “email address only”.

Okay, enough for me tonight. eMailing pseudocode is not something to do when you’re tired.

Later!

Barry

Barry:

I am with you on your thoughts. For my purposes, Address book is a great repository for email addresses, and I would like to sync that with my Palm database, where I keep all other details about my contacts (phone, address, other notes, etc.). I also foresee the same problems as you: How does one construct a sync program that honors deletions? Here is the problem that I see: If the sync is based on the Palm, and a few new emails have come into Address Book, the script would delete those new ones, since they do not exist on the Palm, and the Palm would never see them. If the sync is based on Address Book, you could never delete anything from Palm, because the script would see that it is gone, and add it anew.

As I have been pondering this, I am convinced that the Palm sync must keep some sort of temp file for both the Desktop and Handheld, so that when something is altered or deleted on one, the same happens on the other. The only way we could construct a similar sync script is to go ahead and set it up to sync based on the Palm, but make the intitial run compare the Address Book list current to the Address Book last. What I mean, is that every time the sync script runs, it saves a list of Address Book contacts at the end. The next time it runs, it compares that list, and immediately adds the NEW contacts to the Palm, then the two sync, based on the Palm database.

Does that make sense?

Okay; so we agree that AddressBook may be where new eMail addresses get entered first but not necessarily as we might do it in Palm and then want to sync the two db’s. As you’ve written, if you delete the record from AddressBook but it’s been sync’d already with Palm, how do you handle it?

How about if the sync script adds a little something to the Notes field in Addressbook? Perhaps the word “sync’d.” as the first word. This would tell the script (on subsequent runs) that this record has been sync’d with Palm. So the next sync run after the record was deleted from Palm would tell the script (upon encountering this same record in AddressBook but not Palm that it had to have been manually deleted from Palm and, therefore, should be deleted from AddressBook.

If the record (in AddressBook) does [i]not[i/] have the word “sync’d.” as the first word of the Notes field, then the script assumes we do want this record to be added to the Palm db.

Now, what about if we change certain fields in Palm? Shouldn’t they be written back to the AddressBook?

As Professor Kingsfield used to say on The Paper Chase: “There are no answers; only more questions.”

Barry

I believe that the Professor was right. Well, anyway, back to your original question. I believe that I have a script that can cleanly extract the email address from any Mail message, regardless of the sender. It would be very little trouble to go to the next step and add it to the Palm database.


tell application "Mail"
    set theSelection to selection
    if ((count of theSelection) = 1) then
        tell item 1 of theSelection
            set theSender to sender
        end tell
    end if
end tell

set this_Sender to ExtractSender(theSender)
this_Sender
on ExtractSender(ts)
    if ts does not contain "<" then
        set ext_Sender to {{"No", "Name"}, ts}
    else
        set tid to AppleScript's text item delimiters
        set AppleScript's text item delimiters to "<"
        set sender_DAT to {ts's text item 1, ts's text item 2}
        set AppleScript's text item delimiters to tid
        set ext_Sender to {(every word of sender_DAT's item 1), (characters 1 thru -2 of (sender_DAT's item 2) as string)}
    end if
    return ext_Sender
end ExtractSender

-->{{"Craig", "A", "Smith", "DVM"}, "tacomacathospital@wamail.net"}
-->{{"No", "Name"}, "casdvm@wamail.net"}
-->{{"Barry", "Levine"}, "barryjaylevine@yahoo.com"}

As you can see, however (please see the list of responses at the end of the script for examples), one never knows just what sort of name is going to come along with the email message, so automatically plugging it into the Palm will be difficult, at best. In fact, even Address Book struggles with my professional name:

{first name:"DVM", middle name:"Craig A", last name:"Smith"}

So, here we are with more questions. Do you want to pursue the synchronisation script, or take this little script to the next step of adding the selected contact to the Palm database? For my own personal use, I will probably spend some time on the synchronisation issue, if for no other reason than the interesting challenges it presents. I am happy to help you continue on your original course as well, since I believe it has usefulness as well, and I really like scripting the Palm Desktop.

Craig,

It looks like the synchronization path may be easier simply because the data are already in the correct fields; if the eMail had a name as well as an address, it’s been parsed and entered into Addressbook. It is possible the name hasn’t been parsed correctly but, no matter which db is the recipient, we’re going to get that result and that will require human intervention. We’ve both seen AddressBook records that have both first and last name crammed into just one of the fields because of poor data entry on the part of the eMail’s sender, etc.

If my suggestion about adding something to the record (the word “sync’d.”?), which would indicate an already sync’d status, resolves the knowing which record to delete issue, then I’ll respectfully suggest you comtinue on the larger “sync both db’s” project. I’ve seen an AppleScript that does this between AddressBook and Entourage (used it once some time ago) and I went looking for something like that (but between AddressBook and Palm) when I transferred my data over to my new Intel iMac. (Entourage’s performance under Rosetta is terrible but Palm Desktop runs quite well!)

Okay, it’s your choice. It’s not like I’m paying you the big bucks and I call the shots, eh? But I think more folks will have a use for sync’ing the db’s if the logic about deleted records is resolved.

Thanks,
Barry

Barry:

I know it has been awhile since I last visited this thread, I hope you have not been holding your breath…

I have discovered some new and interesting information about finding and setting email addresses in the Palm Desktop that may very well affect this synchronization issue. Earlier, I was sure that nearly all the email addresses were located in the custom one field of each contact. I have now noted that some of mine are also located in one of four phone fields for each contact. In fact, if you go to your Palm Desktop, and punch up the information on a contact, you will see a window with 5 other windows inside. In a clockwise direction, beginning in the upper left, they are entitled (Name), Phones, Home Address, Other information, and Word address.

If a contact’s email address is listed in one of the phone fields, it will show up in the Phones window, and when you click that window, you have the option of doing a drop-down thing to change the labels.

If a contact’s email address is listed in the custom one field, it will be located in the Other Information window. When you click that window, you are presented with a little icon to the left of the email address, and clicking it activates Mail, and composes an email to that address! I only discovered that tonight!

Anyway, I was curious if you are interested in a ‘clean-up’ script that would cycle through all your Palm contacts, and move the email from one of the phone fields to the custom one field. I have a crude script (first draft) posted here of a synchronization script that will first make a list of all Palm database email addresses (in the custom one field), and then cycle through your Address book, only making new Palm contacts if the Address book email is not in the list. Of course, as you can imagine, that could leave you with a bunch of duplicate contacts wherein their emails were in one of the phone fields, and therefore not found when obtaining the list of emails in the custom one field. It then adds a line at the end of the Address Book note field that says “synced - (today’s date)”

The only other issue I have located so far is that some of my Address Book contacts have more than one email address. I have not spent much time trying to solve that dilemma, since I have so few. I am pondering it, however.

Personally, now that I have learned this nifty stuff, I plan on writing the ‘clean-up’ script next, before I start syncing with my own Address Book.

Here is the first draft of the sync script:


tell application "Palm Desktop" to set every_Palm_Email to field text of custom one of every address whose field text of custom one is not "" --List of every email address in Palm database

tell application "Address Book"
	set un_Synced to every person whose note does not contain "synced" --List of Address Book person not already synced to Palm
	repeat with some_One in un_Synced
		if every_Palm_Email does not contain (value of email of some_One) then
			my AddToPalm((some_One's last name), (some_One's first name), (value of email of some_One))
			if some_One's note is missing value then
				set some_One's note to space
			end if
			set some_One's note to (some_One's note & (return & "synced - " & (short date string of (current date))))
		end if
	end repeat
end tell
--------------------
on AddToPalm(ln, fn, em)
	tell application "Palm Desktop" to make new address with properties {first name:fn, last name:ln, custom one:{field text:(em's item 1)}}
end AddToPalm

I will only be in town until Saturday morning, then I am off camping for a week with my kids. I do not know how much time I will have this week to work on it, but any comments or questions you have are welcome and probably stimulating.

OK, I have officially decided that the OS X version of the Palm Desktop is amazing. Within that Other Information window on ANY contact, you can easily set up any of the custom fields to be the nifty automatic E-mail address fields. I think I am going to set up the first two, to cover the possibility of two addresses per Address Book contact. Once you have set it up for one contact, the entire database is affected, so that is convenient. Please be advised that that is how I am going to set up my ‘clean-up script’ to work. If two addresses are found for one contact, they will be placed in the custom one and custom two fields.