Frequently, you might run into Visual Basic for Applications (VBA) code like this:
Dim oSh as Object Set oSh = ActivePresentation.Slides(4).Shapes(8) With oSh .SomeProperty = "this" .AnotherProperty = "that" End With
In AppleScript, you don't need to (and can't) declare (Dim) anything as typed. Also, there are no collection objects in AppleScript, so instead of
ActivePresentation.Slides(4).Shapes(8), you refer to
shape 8 of slide 4 of active
presentation. Lastly, you can use a
tell
block exactly as VBA uses a
With
block, as follows:
tell application "Microsoft PowerPoint" set theShape to shape 8 of slide 4 of active presentation tell theShape set some property to "this" set another property to "that" end tell end tell
Furthermore, if you're setting 18 or so properties of
theShape, you don't even need to set each one separately on its own line (although you can if you want to — it's sometimes easier to keep track of them that way). You can set them all in one long line using lists, as follows:
tell theShape
set {some property, another property} to {"this", "that"}
end tell You must use the
tell
block if you want to set a list of properties in one line. You cannot do so using the alternative
of
syntax.
There is no name property of slide in AppleScript. But because it is so useful in VBA as a way of keeping track of and referring to slides, you will frequently find in the macros you want to convert that the author has named each slide and shape of interest for later reference. Many macros will start out by presuming that you have selected a slide or a shape, and then will proceed to name it for future reference.
With ActiveWindow.Selection.SlideRange .Name = "MyName" End With With ActiveWindow.Selection.ShapeRange .Name = "MyName" End With
The VBA author will have been careful to specify that you must select only one slide or shape for the Name property, either using the SlideRange or ShapeRange property as above, or by
.Selection.Slides(n).
You cannot use this coding format in AppleScript. In AppleScript for Microsoft PowerPoint 2004 for Mac, there is no selection object. An alternative option for getting the selected slide is to first click anywhere in the Slide pane to make it the active pane.
tell application "Microsoft PowerPoint" set theIndex to slide index of slide of view ¬ of active window set selectedSlide to slide theIndex of active presentation set slideID to slide ID of selectedSlide end tell
The slide of the view of the active window will be your selected slide as long as you are not in slide sorter view, which does not have any single slide displayed as a slide property, and as long as you first click somewhere in the Slide pane to make it the active pane if you are in normal view. Or, switch to slide view, where it always works. You'll need to dispense with naming the slide (because you can't) and use the slide ID instead.
If you are not going to be moving slide positions around, then just calling
selectedSlide
will find it later in the script. But because that is a reference to the slide index and the index may well change, it is safer to get the slide ID now (as in the third line above) and then use it to find your slide later.
set theSlide to item 1 of (get every slide of active presentation ¬ whose slide ID is slideID)
Getting the selected shape on a slide (unless it's the only shape) appears to be a challenge. The do Visual Basic command doesn't at first appear to work since it cannot return a result to AppleScript.
There is a workaround to get past that first line so that the macro can be converted to AppleScript. This option uses a handler (subroutine) called
GetSelectedShape()
and you are going to need it frequently. Just keep
GetSelectedShape()
handy in a script library, and paste it into the bottom of every script that needs to work with a selected shape.
You call it by entering the following line whenever you need it to take the place of any VBA line that gets the selected shape or assigns a variable to
ActiveWindow.Selection.ShapeRange.
set selectedShape to my GetSelectedShape()
Here it is in action:
tell application "Microsoft PowerPoint" set selectedShape to my GetSelectedShape() --calls the handler below -- test only: set theText to (content of text range of text frame of selectedShape) -- enter rest of script after trying the test and removing the line above end tell to GetSelectedShape() set textFilePath to (path to temporary items as string) & "Shared Text File" tell application "Microsoft PowerPoint" 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. 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 Macintosh Script Editor, is the text from the shape that you selected.
In AppleScript, you first get ahold of the selected slide, exactly as in the previous example, using 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 the do Visual Basic command, you get the ZOrderPosition of the selected shape. The ZOrderPosition property (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, 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 shape index on the slide, and you know which is the selected slide, so you can get
shape n
of the selected slide and that's the selected shape you're looking for.
The problem is that the do Visual Basic command cannot return this result
n
to the script. So instead, you 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 your Temporary Items folder on your Macintosh
and set a variable
textFilePath
to it. You insert that
textFilePath
variable, carefully surrounded by escaped quotes, into the do Visual Basic command, and use the standard Visual Basic FreeFile, Print #, and Close # methods to write the string number to the file and close it.
Note Under Mac OS X, the Temporary Items folder is 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 you are writing here is the number
3
or
4, so it's not necessary to take any precautions in this case.
You can then immediately retrieve the string from the file using 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 selectedSlide, and then you have the selected shape.
This workaround works for converting your macros in Microsoft Office 2004, but the do Visual Basic command will not work in Office 2008. The handler gets you over that first hurdle, and you now can convert your macros that depend on selected shapes.


