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 119
on error
return
end try
set newMsg to window 1
set theAttachments to every attachment of newMsg
repeat with theAttachment in theAttachments
-- only one, don't multiply vCards
if name of theAttachment = (myname & ".vcf") then return
end repeat
make new attachment at newMsg with properties {file:theVcard}
end tell
The first part of the script gets the path to MUD, the path to the Microsoft User Data folder, where we are going to store our vCard. Although you don't realize it when you make vCards in the UI, Entourage needs it as a file on your hard disk in order to be able to make an attachment to a message. We could make it in the "Entourage Temp" subfolder there, where Entourage makes vCards dragged to a message, but instead we'll make it right in the MUD folder to have it easily available in the UI too.
Entourage requires (and won‘t let you delete) a "Me" contact, so the line that gets it will never error. Now there is a very handy property of contact called vcard data, which is exactly what it says: all the data, in text form, that makes up a vCard. We also need the name of your Me contact too, to become the name of the vCard.
The next section of the script, after the first Entourage block, creates the vCard file. First we compose the full path of the file-to-be: being the path to MUD plus your name plus the .vcf extension. All the next group of commands come from the Standard Additions whose dictionary you can find in Script Editor. Among the many interesting commands, find the File Read/Write Suite. In order to write to a file you need to open for access file [pathname] with write permission.
That creates a new file if it doesn't exist yet, or accesses it if it does. The command set eof f to 0 erases it (we are about to update the vCard, which might have out-of-date info from the last time). We then write the vcard data to the file and close it. (Always put a write command inside a try/error block, and try to close access the file if there's any sort of error. It's very bad to leave a file with open access.)
On most Macs, Apple's Address Book is the default "owner" of .vcf (vCard) files. So the file would appear with Address Book's vCard icon. We therefore tell the Finder to set the file's creator type to "OPIM" – Entourage's 4-character signature (creator type) – and its file type to "vCrd" to get the correct Entourage icon: vCard, not database or message folder or anything else.
Finally, back in Entourage, we check that the front window is a draft window and check for existing attachments: after all, if the script is running from a schedule every 1 minute we don't want it multiplying the vCard by adding a new copy every minute! So if we find that there already is an attachment with (your name & ".vcf) we quit the script (return) without doing anything. Otherwise, we proceed to make a new attachment at the draft window, just as we did in the very first example, using the alias object as the file property for the attachment. Whenever you send off the message, it will take your vCard with it.
Note that by a reverse process you could make new contacts in your Entourage address book by running a script (possibly from a mail rule filtered on messages with attachments whose names contain ".vcf") to save .vcf attachments to your hard disk (say, a subfolder for vCards in the MUD folder, or somewhere temporary and delete the files at the end of the script), then use the read command to read as Unicode text the file into an r variable and, in Entourage, make new contact with properties {vcard data:r}.
However, this script might backfire if the vCard was sent via Apple's Mail and Address Book or some other applications which make vCards differently: Entourage uses Unicode text – which is why writing the vcard data worked well and why you need to read it back as Unicode text. But this is probably incorrect of Entourage: other applications such as Apple's apps write vCards as plain text (string). In addition, Apple's Address Book now follows a different protocol, so it cannot even be adjusted for Entourage. If you read one of Apple's vCards as Unicode text, the resulting Entourage contact appears to be completely blank. It may be different again coming from other sources, but this will probably do as a workaround, given that Entourage is the only application with Unicode vCards. A short test that checks for the presence of a byte-order mark (BOM) of hex-characters FEFF – ASCII characters 254 and 255 – as the first two characters of the text should suffice. (If the text begins with the BOM, it's Unicode UTF-16 as made by Entourage vCards; if not, it's a plain text vCard not compatible with Entourage.)
set r to read theVcard -- an .vcf alias (file) on your computer
if (ASCII number (character 1 of r2)) = 254 and ¬
< Previous Page Next Page>
- SPREAD THE WORD:
- Slashdot
- Digg
- Del.icio.us
- Newsvine