• 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 23



Dim oDoc As Document

Set oDoc = Documents.Open FileName:="Mac HD:Folder:Filename"

With oDoc

.HyphenationZone = InchesToPoints(0.25)

.HyphenateCaps = False

.AutoHyphenation = True

End With

The process of setting a variable to the result of a command is usually straightforward in AppleScript too. However, the command open from the Standard Suite does not return a result, so you cannot set a variable to the command. The commands in the Standard Suite are derived from a model supplied by Apple, who strongly advise all applications to use the Standard Suite as far as possible to maintain a consistency to the language.

It is possible for applications to add their own parameters to commands of the Standard Suite, and Word adds a great many, in fact 10 of them, all derived from VBA's Open. But they cannot alter the behavior that open does not return a result. They would have done far better to do what Excel does – leave the plain open command in place without parameters, and also give Word its own 'open document' command with all the parameters, that would return a result.

Here is a workaround that seems likely to be successful:

tell application "Microsoft Word"

     open " Mac HD:Folder:Filename.doc"

     set theDoc to active document

     tell theDoc

          set hyphenation zone to (inches to points inches 0.25)

          set hyphenate caps to true

          set auto hyphenation to true

     end tell

end tell

We open the document and then set our variable to active document – the document in the front. The open command always opens the file in the front of the application, so this works if the document was not already open. If the document already is open, however, it does not come to the front, and you will be acting on the wrong document. So here's the full workaround you need:

tell application "Microsoft Word"

     try

          set theDoc to document "Filename.doc"

     on error

          open "Mac HD:Folder:Filename.doc"

          set theDoc to active document

     end try

     tell theDoc

          set hyphenation zone to (inches to points inches 0.25)

          set hyphenate caps to true

          set auto hyphenation to true

     end tell

end tell

It still makes me nervous though – theoretically there might some problem along the way. But it works, and is what we have for now. When Office 2008 arrives, do check in the Microsoft Word Suite to see if there is an 'open document' or similarly named command that returns a result, and also check open in the Standard Suite just in case, as well.

Working with Collections

As explained in Chapter 2, AppleScript does not have Collection Objects as such. You simply get the plural (a list) of any class. You will note that the dictionary always gives the plural if there is one: very, very rarely, there is no plural, and then you can‘t get a list. This is only the case for those rare classes where there is only one such object, such as application or selection. In all other cases, the class is an element of another class (and is so denoted by that "Contained by" reference in the dictionary), either of the application itself or of another class.



< 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