Parsing text from Entourage using delimiters

I am trying to parse this data from the content of an email. It works as a standalone. But when I compose the script for the application - Entourage it won’t accept the delimiters. So it won’t get the phone number. Also the script looks quite nasty and it keeps giving me a headache! Is it possible to just select the items appearing after the categories. ie. Address, Phone, Date etc?

Membership No: 44
Address: everywhere
Phone: 00991784100102
Date: 01-11-2006

Many thanks -drPETE

-- from email extracts the name, uk phone number and details and adds them to the AB

tell application "Microsoft Entourage"
	
	set MyDisplaySenders to {}
	set unreadlist to messages of folder "Inbox" whose read status = untouched
	repeat with MessID from 1 to (number of items of unreadlist)
		set theMessage to item MessID of unreadlist
		set membersName to the subject of theMessage
		set sSender to sender of theMessage
		set addrNick to address of sSender
		set dispName to display name of sSender
		--set a to the text of content of theMessage as text
		
set a to "Membership No: 44 
Address: everywhere
Phone: 00991784100102
Date: 01-11-2006"

		set text item delimiters to ":"
		set bContent to text items 4 thru 4 of a --Just grab the first 3rd line, using the colons as delimiters
		set text item delimiters to ""
		set bNumber to bContent --grab the just the number 
		set final_text to bNumber as text --Make the string from the grabbed items.
		--set text item delimiters to tdl
		set final_number to (characters 1 thru 16 of final_text) as text	
		set tPhone to final_number
		set ukPhone to ("+44" & characters 1 thru 16 of tPhone) as string
		set tLabel to ("Telephone") as Unicode text
		set tNote to the content of displayed message
			
		tell application "Address Book"	
-- Make new record with members details in the note
			set newPerson to make new person with properties {first name:(word 1 of membersName), last name:(word -1 of membersName), nickname:(word 1 of addrNick & " " & addrNick), note:"Hi " & (word 1 of dispName) & ", " & "your membership details:- " & tNote as Unicode text}
			-- Add the phone number.
			tell newPerson to make new phone at end of phones with properties {label:tLabel, value:ukPhone}
			save addressbook				
		end tell
		
	end repeat
	end tell

Your script is expecting Entourage to have a property or element called text item delimiters, drpete - which it doesn’t.

You can avoid the problem by specifying (especially within a tell statement) that the text item delimiters property belongs to AppleScript:


tell application "Microsoft Entourage"
	set AppleScript's text item delimiters to ":"
end tell

Thanks Kai,

That solves that problem. but now when I re-enable the interrogation of the entourage mail it fails to capture the content of the mail.

I think I have got a long way to go here. Maybe you can point towards some key scripts. I wouldn’t want to drag you through debugging the script I have concocted!

Regards drPETE

This probably won’t do exactly what you want, drpete - especially since I’ve had to make a number of assumptions about your aims. Hopefully, though, it will give you one or two ideas on how you might achieve them.

It adopts an alternative approach to that which I suggested earlier; the text manipulation routine is performed in a separate handler (get_values), well away from any tell statement. You’ll see that, by using this technique, AppleScript’s ownership of the text item delimiters property is implicit.

For those who (like me) don’t normally use Entourage, this should help to demonstrate how the handler is intended to work:


to get_values from txt
	set tid to text item delimiters
	set text item delimiters to ": "
	set lst to rest of txt's text items
	set text item delimiters to tid
	repeat with i in lst
		set i's contents to i's text 1 thru paragraph -2
	end repeat
	lst
end get_values

set txt to "Membership No: 44
Address: everywhere
Phone: 00991784100102
Date: 01-11-2006
"
get_values from txt --> {"44", "everywhere", "00991784100102", "01-11-2006"}


And here’s how it might work in conjunction with an Entourage extraction routine:


to get_values from txt
	set tid to text item delimiters
	set text item delimiters to ": "
	set lst to rest of txt's text items
	set text item delimiters to tid
	repeat with i in lst
		set i's contents to i's text 1 thru paragraph -2
	end repeat
	lst
end get_values

to add_to_Address_Book(nam, eml, dsp, txt)
	set txt to txt's text 1 thru word -1 & return (* clean up any trailing paragraphs *)
	set {num, adr, tel, dat} to get_values from txt
	set dat to date dat
	tell application "Address Book"
		tell (make new person with properties {first name:nam's word 1, last name:nam's word -1, nickname:eml's word 1, job title:"Member " & num, note:"Hi " & dsp's word 1 & ", " & "your membership details:" & return & txt})
			make custom date at end of custom dates with properties {label:"Member from", value:dat}
			make phone with properties {label:"Telephone", value:"+44" & tel}
			make email with properties {label:"Email", value:eml}
			make address with properties {label:"Address", street:adr}
		end tell
		save addressbook
	end tell
end add_to_Address_Book

tell application "Microsoft Entourage" to repeat with msg in (get messages of folder "Inbox" whose read status is untouched)
	set {content:txt, subject:nam, sender:{address:eml, display name:dsp}} to msg
	my add_to_Address_Book(nam, eml, dsp, txt)
end repeat


Thanks Kai, works perfectly!

Learning a lot here beginning to be able to do some bits myself!:slight_smile:

Hi I can make the new casrd in the address book.
I can find a person and extract stuff from the address book.
But, how do I update an article wthin the card?
In this case I want to change the note.

tell application "Address Book"
	set this_Person to selection as reference
	set note to "Updated "
	--remove note
	--make new note at the end of this_Person with properties {"hope this works!"}
	--make email with properties {label:"Email", value:"mail"}
	save addressbook
end tell

Try these, drpete:

To replace any previous notes:


set this_note to "This is a new note."
tell application "Address Book"
	set this_Person to beginning of (get selection)
	set this_Person's note to this_note
	save addressbook
end tell


To delete all notes:


tell application "Address Book"
	set this_Person to beginning of (get selection)
	set this_Person's note to ""
	save addressbook
end tell


To add to any existing notes:


set this_note to "This is an update."
tell application "Address Book"
	set this_Person to beginning of (get selection)
	tell this_Person to if note is missing value then
		set note to this_note
	else
		set note to note & return & this_note
	end if
	save addressbook
end tell


Thanks Kai for spending your time with me on this.

When I place this in to my search routine it stops working.
Why does it do that? What is the selection? Should the note be updated after the owner has been identified?

drPETE

set this_note to "This is a new note."

tell application "Address Book"
	set the_data to {}
	set the_people to every person whose name = search_name
	repeat with the_person in the_people
		tell the_person
			set theName to name
			repeat with this_phone in every phone
				set this_data to (value of this_phone)
				set end of the_data to this_data
				set this_Person to beginning of (get selection)
				set this_Person's note to this_note
				save addressbook
				return the_data
			end repeat
		end tell
	end repeat
end tell

Hi Pete.

The routines I suggested were intended to be used directly within an application tell statement. They won’t generally work inside the nested ‘tell the_person’ block, simply because ‘the_person’ doesn’t understand application-specific terms and expressions like “selection” and “save addressbook”.

(Incidentally, I used ‘selection’ because it was in your earlier script. I therefore assumed that you were possibly selecting certain records in Address Book manually ” and then running the script to do something with them. However, the reference to a possible search now suggests otherwise.)

At this point, I’m uncertain about what to suggest, since I’m not entirely sure about your aim. However, for the sake of an example, the following routine inserts a note and collects the phone numbers for those people whose name matches the search criteria:


set search_name to text returned of (display dialog "Search for who?" default answer "")
set the_data to {}
set this_note to "This is a new note."
tell application "Address Book"
	repeat with the_person in (people whose name is search_name)
		set the_data's end to value of the_person's phones
		set the_person's note to this_note
		save addressbook
	end repeat
end tell
the_data


Of course, this may not be what you’re after at all. If that’s the case, perhaps you could give a few more details of what you’re trying to achieve ” and we’ll try to help you get on track. :slight_smile:

Thanks Kai, that works fine now.

Basically I am using the Address Book as a crude updatable database for my club.

What I like about Applescript is it allows me (with a lot of help from the forum!) to make test systems which work. Then I can pay a php progammer to do it as a web based app.

Many thanks.

drPETE

I’m pretty new to the whole AppleScript world, but I was wondering where I could obtain a list of the properties accepted per each application -
For example, I’m trying to modify the task script given by microsoft, when a message arrives, and I would like to set a reminder date, but I have no clue what the format is and how to set any other properties.

Thanks.

Hi Technix,

I am not clear what you are trying to do.

I have the found this forum really amazing. There are people who seem completely happy solving other peoples problems!!

I think you are best to start a new thread. You have to really explain clearly the step by step activity you want to achieve. Then the forum will find a way to help you. It is also a good idea to experiment with some existing scripts, modify them for your own needs. Then you will be getting close to a solution and be able to make targeted requests.

If you open Script Editor and then open Dictionary for Entourage it gives you a list of the properties specific to that application.

reagards drPETE

VFX/editor pinewood UK

Hi, Technix;

drpete is right here - we’ll help, we need more info about what you want to do, and you should start a new thread with your question. Be careful with the title you assign, too - Just writing “Help”, will not be as effective as “Setting a reminder from an email date in Entourage”, with a copy of the script you want to modify.

Adam