Save the document and get its file path
Imagine that you save a document in Microsoft Word, Excel, or PowerPoint. Then you send it as an e-mail attachment to someone. Simple. But if you need to send messages with attachments several times a day, always to the same recipients, this gets tedious. Instead, you can write a script so that Microsoft Entourage does it for you in a flash.
If you want to write different text in the message each time, the script can be written to include just the attachment, the recipients' names, the subject, and even a salutation. But if the message text is also the same every time, you can simply have the message prepared and sent off in the background without ever switching away from the application that you're working in.
The first step in both cases is to check that the document is saved, or save it if it's not, and to get its file path. This step needs to be done in the application that you're working from, and will differ slightly depending on whether it's Word, Excel, or PowerPoint.
In Word:
tell application "Microsoft Word" try set frontDoc to active document save frontDoc on error -- if you cancel out of Save dialog for an unsaved doc, avoid error return end try set filePath to full name of frontDoc end tell
In Excel:
tell application "Microsoft Excel" try set frontDoc to active workbook on error -- avoid error if no workbook open return end try set containerPath to path of frontDoc if containerPath = "" then -- unsaved activate set docName to name of frontDoc set filePath to (choose file name default name docName) ¬ as Unicode text if filePath does not contain "." then set filePath ¬ to filePath & ".xls" save frontDoc in filePath else -- already saved as file, re-save save frontDoc set filePath to full name of frontDoc end if end tell
In PowerPoint:
tell application "Microsoft PowerPoint"
try
set frontDoc to active presentation
on error -- avoid error if no presentation open
return
end try
set containerPath to path of frontDoc
if containerPath = "" then -- unsaved
activate
set docName to name of frontDoc
set filePath to (choose file name default name docName) ¬
as Unicode text
if filePath does not contain "." then set filePath ¬
to filePath & ".ppt"
save frontDoc in filePath
else -- already saved as file, can't re-save (bug)
beep
display dialog "Due to a bug in PowerPoint, you need to ¬
make sure you " & "have saved the presentation first." ¬
& return & return & "If you have done so, ¬
click \"Proceed\"." buttons {"Cancel", "Proceed"} ¬
with icon 2
set filePath to full name of frontDoc
end if
end tellNote PowerPoint
currently requires the
display dialog
code in the above example, in place of the
else
section you see in the Excel
code.
Create the message
Now that you have the
filePath
of the front document (as text), you can create your e-mail message. If you want to open a new message in Entourage
with preset recipients' names and e-mail addresses, a CC line, a salutation, a subject, and your default signature, the script would continue as follows:
tell application "Microsoft Entourage"
activate
set mySigType to default signature type of default mail account
if mySigType is other then set mySig to default signature choice ¬
of default mail account
set newMsg to make new draft window with properties ¬
{subject:"Today's File", to recipients:¬
"Ellen Adams <ellenadams@northwindtraders.com>, " & ¬
"Jun Cao <juncao@northwindtraders.com>", CC recipients:¬
"Toni Poe <tonipoe@northwindtraders.com>", content:¬
"Hi, Everyone" & return & return, signature type:¬
mySigType, attachment:alias filePath}
if mySigType is other then set other signature choice ¬
of newMsg to mySig
end tellNote that you
activate Entourage
to bring it to the front, and that you have to extract the default signature of the default account so that you can use it when you make a new draft window. Checking the dictionary for any type of mail account (POP account, IMAP account, Hotmail account, or Exchange account), you will notice that the default signature type might be random, none, or other. The other type is the regular sort of signature. In that case, you also have to extract the other signature choice, which is the actual signature. But if your account uses a random or none type, then there is no other signature choice property, so you can't specify it later when making the new draft window or the script will error.
The need to use short lines with the continuation character (¬) in this topic obscures the fact that the to recipients property (and cc recipients and bcc recipients properties as well) is just a continuous, single string, with the various recipients separated by commas. That is, when you write your own scripts, you wouldn't bother with the continuation character (¬) and would let Macintosh Script Editor
wrap the whole make new draft window command, with the to recipients property specified as follows:
to recipients:"Ellen Adams <ellenadams@northwindtraders.com>, Jun Cao <juncao@northwindtraders.com>"
You do not need to include the display names if you don't want to, for any recipients, and in that case you don't need the
< >
angled brackets around the e-mail address. So the line above could be written as follows:
to recipients:"ellenadams@northwindtraders.com, juncao@northwindtraders.com"
If you look at the draft window class entry in the dictionary, you will find that there is no attachment property. Yet the code works. That is because the developers made a coercion to make things simpler: a putative property for use only at inception, in the with properties parameter of the make new draft window (or draft news window) or outgoing message commands. It doesn't work anywhere else. The element attachment is not a property, but is actually an element of the draft window and message classes, since there can be any number of them, from 0 to many.
If you want to get the attachments of an incoming or sent message, you can't get the attachment — you'll get an error. Instead you have to get attachments or
every attachment of message, and then get the file property of each one if you want to track them down.
Getting back to draft window (new message window), you could add the attachment after the fact as follows:
set newMsg to make new draft window with properties {subject:"Today's File"}
make new attachment at newMsg with properties {file:alias filePath}But if you have a lot of attachments to make, this would get tedious. Using the with properties parameter of the make new draft window command, you can attach a whole raft of attachments, using a list of aliases in
{ }
list braces as the attachment property.
This code omits a large number of draft window properties: account, bounds, encoding, priority, and much else that you didn't need to set. They all have default values that do not need to be specified, but should you want to change any of them, you can do so.


