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



So to specify the actual message on which the rule (or schedule) is running, you need to get item 1 of that list. Same goes if you have selected just one message in a message list. So far so good, but you'll find that if you try

tell application "Microsoft Entourage"

     set theMsg to item 1 of current messages

end tell

you get an error: "Microsoft Entourage got an error: Can't get item 1 of current messages."

That is because current messages is one of the rare properties that requires an explicit get command, or else setting a variable to it in a separate line – which does the same thing as get, namely evaluates the reference (the list of specific messages) from which you can select your item 1.

set theMsg to item 1 of (get current messages)

--> incoming message id 120434

That works, and is what you need to remember to do every time you need item 1 of current messages.

It's exactly the same with selection, another property of the application: you need the explicit get, or a variable, to be able to work with it further. selection, or rather get selection, will return whatever (*almost) is selected in the UI, as one of the following classes: 1) a list, 2) a folder, or 3) text, depending on what is selected. Selected objects such as contacts, groups, events, tasks, notes, messages are always returned as a list, even when only a single object is selected – it is returned as a one-item list. The only exception is for a selected folder or custom view in the Folders List, which is returned as a folder (only one can be selected at a time). Selected text in a message or note (including the note sections and all text fields in contact, event and task windows) is returned as Unicode text.

You always need to check for the class of the selection to make sure you have what you want and can operate on it (you can't even get 'item 1' of the selection if it's a folder, and 'item 1' turns out to be the first character if it's text): it's very easy to miss the fact that you selected a folder rather than a message in it. (This is why using current messages instead of selection is preferable for messages: current messages works even if the folder, rather than the messages, has the focus in the UI.) So usually it's best to set a variable to selection, then check its class, and finally whatever you need from it:

tell application "Microsoft Entourage"

     set theSelection to selection

     if class of theSelection is list then

          set theObject to item 1 of theSelection

          -- will then check class of theObject for contact, etc.

     else

          beep

          display dialog "First make sure you have selected one or more " & ¬

              "contacts in the Address Book." buttons {"Cancel"} with icon 2

     end if

     --rest of script

end tell

(*Even a single calendar event selected in the Calendar returns a single-item list of the selected event. But a few odd things – mostly popup objects like mini-calendars for date selections, buttons in Print dialogs and the like, things for which Entourage has no AppleScript class – baffle the script and return an error if you try the get selection when they're selected. For most "blank" selections, though, such as a place-holder for a new calendar event which hasn't been opened yet, or an open event or contact window in which you've selected nothing at all, it will return an empty list {} or empty text "", as it should.)

One more device that needs to be mentioned is Entourage's own script menu. Entourage has had a script menu since long before the OS ever had one. It is still used by long-time Entourage users, although the system's script menu works perfectly fine for Entourage too. Entourage's script menu has two advantages over the system's script menu:

1) scripts run from it run marginally faster, but that's much less noticeable now in Tiger OS 10.4 where the system script menu no longer is handicapped by slow calls to applications, and

2) there is an excellent method for adding keyboard shortcuts to scripts. Go to the Entourage Help menu and enter "About the script menu" to see how keyboard shortcuts work.

Being able to run scripts manually several times a day via a keyboard shortcut is a great boon. But there is currently also a major disadvantage of the Entourage script menu for some scripts. Most scripts are fine, but those that have script properties (see your AppleScript books) whose values persist between script runs may have issues.



< 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