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.
|
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>
- SPREAD THE WORD:
- Slashdot
- Digg
- Del.icio.us
- Newsvine