Re: Vacation Mail script
Re: Vacation Mail script
- Subject: Re: Vacation Mail script
- From: Andreas Amann <email@hidden>
- Date: Fri, 26 Mar 2004 20:09:34 -0800
Unfortunately, the name property of the Address Book contact is read
only; to write a name, you have to parse it into first, middle, last,
title, and suffix. That is not a trivial job because a name can have
anywhere from one to all five of those. (Plus the missing-name case
has to be accounted for.) For example, if the name has two spaces in
it, is that first, middle, and last or title, first and last or first,
last, and suffix?
Agreed, this is rather painful - I have come up with two procedures
which seem to do a pretty decent job in getting most names right (you
would call the "processNameString" one first to get the name into a
standardized format and the call the other procedure to actually
populate the AB). Of course, you might want to think about whether this
much effort is needed for a vacation script but it might prove useful
in other contexts...
--
on processNameString(theName)
-- try to change most name formats into a single standard one...
-- this procedure is partially derived from code in "Add Recipients to
AB" found at
http://homepage.mac.com/cricket/
set theResult to {}
set theIntermediateResult to {}
ignoring case
if theName contains "(E-Mail" then -- remove stupid Outlook addition
to name...
set theOffset1 to offset of "(E-Mail" in theName
set theOffset2 to offset of ")" in (characters theOffset1 thru -1 of
theName) as text
set theName to (characters 1 thru (theOffset1 - 1) of theName) &
((characters (theOffset1 + theOffset2) thru -1 of theName) as text) as
text
end if
end ignoring
set AppleScript's text item delimiters to " "
set everyTextItem to every text item of theName
set AppleScript's text item delimiters to ""
if ((count of everyTextItem) is greater than 1) and (last character of
item 1 of everyTextItem as string is equal to ",") then
set theCount to count of everyTextItem
set theIntermediateResult to {item theCount of everyTextItem}
repeat with theCounter from theCount - 1 to 1 by -1
set theIntermediateResult to theIntermediateResult & {item
theCounter of everyTextItem}
end repeat
else
set theIntermediateResult to everyTextItem
end if
repeat with eachItem in theIntermediateResult
set thisItem to ""
set everyCharacter to every character of eachItem
repeat with eachCharacter in everyCharacter
if (eachCharacter as string is not equal to "\"") and (eachCharacter
as string is not equal to "(") and (eachCharacter as string is not
equal to ")") and (eachCharacter as string is not equal to ",") then
set thisItem to thisItem & eachCharacter as string
end if
end repeat
set theResult to theResult & {thisItem}
end repeat
set AppleScript's text item delimiters to " "
set theResult to theResult as string
set AppleScript's text item delimiters to ""
-- remove another Outlook nicety ;-)
if theResult starts with "'" and theResult ends with "'" then set
theResult to characters 2 thru -2 of theResult as text
return theResult
end processNameString
on addName(thePerson, theName)
set AppleScript's text item delimiters to " "
set theNameParts to text items of theName
set AppleScript's text item delimiters to ""
if (count of theNameParts) = 1 then
tell application "Address Book" to set first name of thePerson to
(item 1 of theNameParts)
else if (count of theNameParts) = 2 then
tell application "Address Book"
set first name of thePerson to (item 1 of theNameParts)
set last name of thePerson to (item 2 of theNameParts)
end tell
else if (count of theNameParts) = 3 and ((item 3 of theNameParts) ends
with ".") then
tell application "Address Book"
set first name of thePerson to (item 1 of theNameParts)
set last name of thePerson to (item 2 of theNameParts)
set suffix of thePerson to (item 3 of theNameParts)
end tell
else if (count of theNameParts) = 3 and ((item 1 of theNameParts) ends
with ".") then
tell application "Address Book"
set title of thePerson to (item 1 of theNameParts)
set first name of thePerson to (item 2 of theNameParts)
set last name of thePerson to (item 3 of theNameParts)
end tell
else if (count of theNameParts) = 3 then
tell application "Address Book"
set first name of thePerson to (item 1 of theNameParts)
set middle name of thePerson to (item 2 of theNameParts)
set last name of thePerson to (item 3 of theNameParts)
end tell
else
if (item 1 of theNameParts) ends with "." then
tell application "Address Book" to set title of thePerson to (item 1
of theNameParts)
set theNameParts to items 2 thru -1 of theNameParts
end if
if (item -1 of theNameParts) ends with "." then
tell application "Address Book" to set suffix of thePerson to (item
-1 of theNameParts)
set theNameParts to items 1 thru -2 of theNameParts
end if
tell application "Address Book" to set first name of thePerson to
(item 1 of theNameParts)
set theNameParts to items 2 thru -1 of theNameParts
if ((count of theNameParts) > 1) then
tell application "Address Book"
set middle name of thePerson to (item 1 of theNameParts)
set AppleScript's text item delimiters to " "
set last name of thePerson to (items 2 thru -1 of theNameParts as
text)
set AppleScript's text item delimiters to ""
end tell
else
tell application "Address Book" to set last name of thePerson to
(item 1 of theNameParts)
end if
end if
end addName
--
Furthermore, in order not to have duplicate addresses in the group
and have the group grow beyond control you probably might want to
check whether the address is already present:
No need to; the rule checks to see whether it is in the group before
calling the script. If it is in the group, it doesn't run the script,
nor do any of the other actions in the rule.
Good point - I was forgetting about the context;-)
Andreas
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.