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



--> {49284, 60516, 58081}

 

to ApplifyMSColor({r, g, b})

     set r to (r ^ 2) as integer

     set g to (g ^ 2) as integer

     set b to (b ^ 2) as integer

     return {r, g, b}

end ApplifyMSColor

It needs as integer because AppleScript's exponentiation operator ^ produces reals (floating point numbers) that look like 4.9284E+4 when 10000.0 or greater, but these values (all of them 65535 or less) will coerce to integer, which is what the color type requires. In the reverse direction, going from Entourage to PowerPoint, you would get the square root of each 65535-based integer and round down to the nearest integer, which is easily done with the integer division operator div:

set r to (49285 ^ 0.5) div 1

-->222

The coercion as integer would work here too now as of OS 10.4, but not before. If the 65535-based number is not a perfect square, its square root will be a real with a non-zero fractional part: 499285 ^ 0.5 results in 222.002252240828, for example. Before OS 10.4 trying to coerce that sort of real 'as integer' would cause an error, so div 1 was the way to do it, and it works perfectly in all versions of <ac OS X. Back to our script.

Moving the shapes is completely straightforward, as the left position property of shape corresponds exactly to the Left Property in VBA. Note that when getting a property (left position) within a compound statement that will then do something with it in the same line:

set left position to (get left position) + 36

as opposed to setting a variable to it in a previous line, you must use the explicit get. And you must also use the explicit get when using the repeat loop form of repeat with someVariable in someList and you want to use (every element of someObject) as the list instead of setting a variable (someList) to that first.

I used these forms here, with the explicit get, because I know how tempting it is to use the parallel forms of the same expressions you use in VBA. But I actually would advise you to get in the habit of setting variables first to ensure you have evaluated the expressions before you try to operate on them: it is very easy to forget about that explicit get and then wonder why the script keeps erroring "can't get [whatever]".

Before we leave this little script, note that if you want to set the left position of every shape on the slide to the same value (i.e., not relative to its old position, which will be different for each shape), in AppleScript you can avoid the repeat loop entirely, and do it all in one fell swoop:

set left position of every shape of oSl to 100

This is a fantastic boon if you have perhaps 50 shapes on the slide – no looping! (The first few pages of the PowerPoint AppleScript Reference discusses this topic.) Using the every element syntax does not always work (as the developer has to be specifically implement it for an object), but it does here. It can't be done for setting the background of the color scheme because, as you will recall, we were not setting a property there but calling the proprietary command set color for.

Working with Text

Change or Add Text

Text isn‘t limited to text boxes. You can click on most shapes in PowerPoint and start typing text. This is because most (though not all) shapes have a Text Frame. To work with text in shapes programmatically, you start with the text frame. We'll start with a simple macro:

Sub AddSomeText()

' work with the currently selected shape

With ActiveWindow.Selection.ShapeRange

' the shape's "text container" is the TextFrame

With .TextFrame

' unless we specify otherwise, .TextRange refers to

' all of the text in the text frame



< 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