• MacTech Network:
  • Tech Support
  • |
  • MacForge.net
  • |
  • Apple News
  • |
  • Register Domains
  • |
  • SSL Certificates
  • |
  • iPod Deals
  • |
  • Mac Deals
  • |
  • Mac Book Shelf

MAC TECH

  • Home
  • Magazine
    • About MacTech in Print
    • Issue Table of Contents
    • Subscribe
    • Risk Free Sample
    • Back Issues
    • MacTech DVD
  • Archives
    • MacTech Print Archives
    • MacMod
    • MacTutor
    • FrameWorks
    • develop
  • Forums
  • News
    • MacTech News
    • MacTech Blog
    • MacTech Reviews and KoolTools
    • Whitepapers, Screencasts, Videos and Books
    • News Scanner
    • Rumors Scanner
    • Documentation Scanner
    • Submit News or PR
    • MacTech News List
  • Store
  • Apple Expo
    • by Category
    • by Company
    • by Product
  • Job Board
  • Editorial
    • Submit News or PR
    • Writer's Kit
    • Editorial Staff
    • Editorial Calendar
  • Advertising
    • Benefits of MacTech
    • Mechanicals and Submission
    • Dates and Deadlines
    • Submit Apple Expo Entry
  • User
    • Register for Ongoing Raffles
    • Register new user
    • Edit User Settings
    • Logout
  • Contact
    • Customer Service
    • Webmaster Feedback
    • Submit News or PR
    • Suggest an article
  • Connect Tools
    • MacTech Live Podcast
    • RSS Feeds
    • Twitter

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.

Would you like a hard copy
or PDF of this Guide?

You can get a hard copy sent to you
AND download a PDF now ($19.95)
, or

... just download a PDF ($9.95).

Either way, you get a complimentary
MacTech Magazine Subscription

courtesy of the
Microsoft Mac Business Unit


 

  Magazine Reg. Price:  $50.00 value  
  Guide Reg. Price:  $40.00 value  
  You Save:  over $80.00!  

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>
 
MacTech Only Search:
Community Search:

 
 
 

 
 
 
 
 
  • SPREAD THE WORD:
  • Slashdot
  • Digg
  • Del.icio.us
  • Reddit
  • Newsvine
  • Generate a short URL for this page:



MacTech Magazine. www.mactech.com
Toll Free 877-MACTECH, Outside US/Canada: 805-494-9797
MacTech is a registered trademark of Xplain Corporation. Xplain, "The journal of Apple technology", Apple Expo, Explain It, MacDev, MacDev-1, THINK Reference, NetProfessional, Apple Expo, MacTech Central, MacTech Domains, MacNews, MacForge, and the MacTutorMan are trademarks or service marks of Xplain Corporation. Sprocket is a registered trademark of eSprocket Corporation. Other trademarks and copyrights appearing in this printing or software remain the property of their respective holders.
All contents are Copyright 1984-2010 by Xplain Corporation. All rights reserved. Theme designed by Icreon.
 
Nov. 20: Take Control of Syncing Data in Sow Leopard' released
Nov. 19: Cocktail 4.5 (Leopard Edition) released
Nov. 19: macProVideo offers new Cubase tutorials
Nov. 18: S Stardom anounces Safe Capsule, a companion piece for Apple's
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live