Change entourage email address

We are in the process of changing our companies email addresses.

the old way is james_jones@nyc.company.com
new way is james.jones@company.com

I know how to fish the email address out of the default entourage account. How do change tell it to change _ to a . and drop the nyc.

Here is some code that takes a conservative approach to doing what you describe.

Addresses that do not end with “@nyc.company.com” are left untouched.
The only “nyc” that is dropped is the one between the at sign and “.company.com” at the end of the original address.
All remaining underscores are changed to dots. Domain names should not have underscores, but Microsoft software has been known to attempt such nonsense. If this is to be used in such an environment, extra care will have to be taken to change only underscores before the at sign.

to addressIsTargeted(inputAddr)
	return inputAddr ends with "@nyc.company.com"
end addressIsTargeted

to dropNYCFromCompanyDomain(inputAddr)
	-- Include the at sign and the full domain name to make sure we do not drop "nyc" in other parts of the email address.
	set targetString to "@nyc.company.com"
	set targetLength to length of targetString
	set keepPrefixLen to 1
	set keepSuffixLen to 11
	
	-- If we assume addressIsTargeted(inputAddr) would return true, then we could just say 
	-- tell inputAddr to return text 1 through -16 & text -11 through -1
	set targetOffset to offset of targetString in inputAddr
	if targetOffset is greater than 0 then
		-- Make sure target string match is at the end of the input string.
		log {targetOffset, targetLength, length of inputAddr}
		if targetOffset - 1 + targetLength is equal to length of inputAddr then
			-- Use the found offset, keep lengths, and target length to produce and return a new address with the undesirable part removed.
			tell inputAddr to return text 1 through (targetOffset - 1 + keepPrefixLen) & ¬
				text (targetOffset + targetLength - keepSuffixLen) through -1
		else
			-- Return the original address if the target is not at the end.
			return inputAddr
		end if
	else
		-- Return the original address if the target is not found.
		return inputAddr
	end if
end dropNYCFromCompanyDomain

-- A custom version of the code at http://bbs.applescript.net/viewtopic.php?pid=41257#p41257
to changeUnderscoresToDots(inputAddr)
	set {oldTIDs, text item delimiters} to {text item delimiters, {"_"}}
	set inputAddr to text items of inputAddr
	set text item delimiters to {"."}
	tell inputAddr to beginning & ({""} & rest)
	set inputAddr to result
	set text item delimiters to oldTIDs
	return inputAddr
end changeUnderscoresToDots

to fixupAddress(inputAddr)
	if addressIsTargeted(inputAddr) then
		dropNYCFromCompanyDomain(inputAddr)
		changeUnderscoresToDots(result)
		return result
	end if
	return inputAddr
end fixupAddress



set inputAddresses to {"james_jones@nyc.company.com", "fred_t_jones@nyc.company.com", "not_in_nyc@phi.company.com", "has_underscore_but_not_nyc@company.com", "no.underscores.but.in.nyc@nyc.company.com", "some_other@nyc.example.com"}
set newAddresses to {}
repeat with inputAddr in inputAddresses
	fixupAddress(contents of inputAddr)
	set end of newAddresses to result
end repeat

newAddresses

Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)