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



          activate

          set theIndex to slide index of slide of view ¬

              of active window

          set selectedSlide to slide theIndex of active presentation

          do Visual Basic "n = ActiveWindow.Selection.ShapeRange.ZOrderPosition

     'write n as string to text file, replacing any text there

     FileNumber = FreeFile

     Open \"" & textFilePath & "\" For Output As #FileNumber

     Print #FileNumber, CStr(n)

     Close #FileNumber

     "

          set n to (read alias textFilePath before return) as integer

          set selectedShape to shape n of selectedSlide

          return selectedShape

     end tell

end GetSelectedShape

The last line of the top-level script getting the text is just a test for you to try now: first select a text box or shape with some text in it before running this script. You'll see that the result of the script, in Script Editor, is the text from the shape you selected.

In AppleScript we first get hold of the selected slide, exactly as in the previous example, via the view. (In this case, since you have selected a shape, it's guaranteed that the slide pane is active, so you're fine.) With do Visual Basic (in PPT 2004, for now: it will not work in 2008) we get the ZOrderPosition of the selected shape. The ZOrderPosition (or z order position in AppleScript) of a shape is the index number of its "recentness" – the reverse of its order of creation. (Every time you make a new shape, whether in the UI or by macro or script, it now has z order position 1 and pushes the others back to higher numbers.) This index number n is the same as the index of the shape on the slide – and we know which is the selected slide, so we can get shape n of the selected slide and that's the selected shape we're looking for.

The problem is that do Visual Basic cannot return this result n to the script. So instead, we cast the integer n to string (CStr(n)) and write it out to a text file. The very first line of the handler has previously prepared a file path in the user Temporary Items folder on your Mac and set a variable textFilePath to it. (Under OS X, "Temporary Items" is in fact on the root of your hard drive, in /var/tmp, not in your own user folder so don‘t write anything too sensitive there if there are others with access to your computer. Or tell the Finder to delete the file at the end of the script. However, all we are writing here is the number "3" or "4" – that's it – so it's not necessary to take any precautions in this case.) We insert that textFilePath variable, carefully surrounded by escaped quotes, into the do Visual Basic command and use the standard VB FreeFile, Print # and Close # methods to write the string number to the file and close it.

We can then immediately retrieve the string from the file via AppleScript's read Standard Addition command (up to the line ending added by Print #), coerce it back to an integer n, get shape n of the selected slide, and now we have the selected shape. Voilˆ!

This workaround is just a means for you to test out the rest of a macro-to-script conversion now, in Office 2004, of course; the do Visual Basic command will no longer work in Office 2008. When Office 2008 arrives, you hopefully should be able to replace the call to the handler (set selectedShape to my GetSelectedShape() ) with a straightforward statement getting the selected shape - or so we all fervently hope. The handler gets you over that first hurdle, and you now can convert your macros that depend on selected shapes.

(As mentioned already, if PowerPoint 2008 does not implement the selection object, or some alternate way to get selected shape(s) in AppleScript, then you will not be able to convert any macros involving selection or selected shapes in 2008, and would have to remain with PowerPoint 2004 (in Rosetta, if on an Intel computer), or use PowerPoint Windows in Parallels or VMWare, if these PowerPoint macros are necessary to you.)

Change every shape on a slide, change every slide in a presentation

Here is a macro that sets the background color of every slide in a presentation to a pale aquamarine (the background color of the first color scheme of the second row when you Format/Slide Color Scheme in the UI), and then moves each shape on each slide half an inch to the right, looping through each slide in the presentation and each shape on each slide:

Dim oPres As Presentation

Dim oSl As Slide



< 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