

Moving from Microsoft Office VBA to AppleScript:
MacTech's Guide to Making the Transition
Introduction
|
Table of Contents
Page Prev and Page Next buttons at bottom of the page.
|
April, 2007
Page 130
repeat with toRecip in (every recipient of theMsg ¬
whose recipient type is to recipient)
set {dName, eAddress} to {display name, address} ¬
of (address of toRecip)
if dName "" then
set nameAddress to "\"" & dName & "\" <" & eAddress & ">"
else
set nameAddress to eAddress
end if
if {eAddress} is not in entryAddresses then
make new group entry at theGroup ¬
with properties {content:nameAddress} -- note content syntax!
set end of entryAddresses to eAddress
end if
end repeat
end repeat
beep 2
open theGroup
end tell
The script first asks you to name the new group; note that default answer:"" is the way to include a blank text box in display dialog: check for more options in the display dialog entry in the Standard Additions dictionary. The variable groupName is set to text returned of result (what you type in the text box). Then you are asked whether or not you wish to allow the group to list names and email addresses, corresponding to the similar checkbox you will find in the UI of every group in the address book when you open its window, and sets the variable showOnlyName to true or false depending on the answer.
Then we make the new group, assigning it the variable theGroup, specifying its name and show only name in messages properties with the variables we previously assigned for those values. We get the selected messages – you can run the script on several messages at once if you wish, or just one (as we know already, current messages is always a list, even if just a single-item list) – and then process each item of the selectedMessages list (each message) in the repeat loop.
Before starting the outer repeat loop, we initialize an empty list entryAddresses, to which we will add entries as we make them. By setting it only once, outside the outer repeat loop, we insure that duplicate contacts will be avoided for all the messages being processed.
Within the outer repeat loop (for each message) we now proceed to the inner repeat loop for each to recipient of the message. (If you want to include cc recipients as well, simply remove the whose recipient type is to recipient clause.)
We now delve into the peculiar structure of the recipient class once again, whereby its address property is in fact a record containing both a different address property (a string this time, namely the actual email address) and a display name property which might be "" (blank) if not included in the message, and assign the variables eAddress and dName to them respectively. We then need to check whether the display name is in fact present or missing, since if it is not blank (dName "") we include it followed by a space and the email address in < > brackets when assigning the nameAddress variable; if the display name is blank (else) we include only the email address on its own.
Note the extra quotes we put around dName if present: that is required in Entourage if the display name happens to contain any punctuation (such as a period after an initial) – which would otherwise error further down the script when creating the group entry – and it does no harm if there is no punctuation: in fact, in that case, you will not even see the quotes around the display name in the UI since they are removed automatically if not needed.
< Previous Page Next Page>

- SPREAD THE WORD:
- Slashdot
- Digg
- Del.icio.us
- Newsvine