Create a slide
The Add method for Slides in Visual Basic for Applications (VBA) requires two arguments: Layout and Index.
ActivePresentation.Slides.Add Index:=1, Layout:=ppLayoutText
In AppleScript, there are not many properties of slide that are read/write (and some, like color scheme, require a special proprietary command to change them, so they can't be set at inception when making a new slide). The layout property can be set, but the slide index property (the AppleScript term for Index) cannot — it's read-only and can't be set, not even at inception as read-only properties sometimes can.
There is, however, an AppleScript way of specifying where an element is inserted. Elements are made at an object, and usually that's all you need to do. But if you try the following, it errors:
tell application "Microsoft PowerPoint"
set newSlide to make new slide at active presentation ¬
with properties {layout:slide layout text slide}
end tell If you look at the make command in the Standard Suite of any application, you'll see it has two required parameters: new (technically it's this new argument that specifies the class of the new element) and at. The description for the at parameter says,
location reference : the location at which to insert the element. Strictly speaking, that really requires a precise location, not just at the parent object. Since in some cases it doesn't really matter where, and in other cases there's no choice where new elements go — they go at the end, after existing elements — most applications have implemented a convenient coercion that lets you omit the
at end
of [object]
statement.
But with the slides of a presentation, you really want to be able to insert them anywhere, not just at the end, and you can. In AppleScript, the ways to indicate an insertion location are:
at beginning of,
at
end of, at before [some element], and
at after
[some element].
tell application "Microsoft PowerPoint"
set newSlideA to make new slide at beginning of active presentation ¬
with properties {layout:slide layout text slide}
set newSlideB to make new slide at end of active presentation ¬
with properties {layout:slide layout two objects over text}
set newSlideC to make new slide at before slide 2 ¬
of active presentation with properties ¬
{layout:slide layout media clip and text}
end tellThe last command inserts
newSlideC
as the new
slide 2
before the existing
slide 2. (You could also say
at
after slide 1
to insert it at the same location.) Don't forget that you can't drop the at preceding
before, even though it sounds incorrect in English. After the slide is created, you can always get its slide index property to find out where it is located.
Where did those layout types come from? In the dictionary for slide (Microsoft PowerPoint Suite), you will see all of the enumerated constants for the layout property strung together, separated by slashes. It's easy to see which one must be the equivalent for VBA's ppLayoutText constant: slide layout text slide.
Note The developers had to be extremely careful not to cause terminology conflicts. The keywords you see in dictionaries and scripts mask the real "raw codes" that the compiler compiles to. If the same keywords were used for different properties and enumerations, scripts would compile incorrectly and fail. The simplest way to avoid conflicts was to give enumerations a unique name starting with the class and property — slide layout — and only then follow with the enumerated constant. Because
text
is used in so many constants of this enumeration — it's the third word in no less than six of them — they've added the word
slide
to make sure this one is unique and won't be confused with any of the others. You will be able to find the AppleScript version of the VBA constant without difficulty. Just precede it by slide layout and look for what is virtually the same name.
Occasionally you will see macros where the author has used the actual Long (number) rather than the descriptive
pp
constant. For example, for ppLayoutText, the author might have used the number
2
instead. Unfortunately, in this case that won't work (though in several enumerations in Microsoft Office
it does). You would need to look up ppSlideLayout in the Microsoft Visual Basic Editor
Object Browser to find its (ppLayoutText) name from the Long (2) first, and then use its obvious AppleScript equivalent from the enumeration list.
Once you've made your slides, you will want to keep track of which one is which, since the canonical reference to slides is by index (slide 1,
slide 2,
slide 3,and so on), which keeps changing if you insert a new one at the beginning or somewhere in the middle. For example, what you knew as
slide 3
is no longer
slide 3, but
slide 5.
In VBA, all slides have a Name property (by default
Slide1,
Slide2, and so on, but you can rename them). There is no name property in AppleScript. However, all slides also have a unique ID, or SlideID property in VBA, which you can get as soon as you've made the slide. (They seem to increment from the moment you first launched PowerPoint.) This property does exist in AppleScript, as the slide ID property. In VBA, you can find the slide again using the SlideID, via the FindBySlideID method of the Slides Collection object. Such a command does not exist in AppleScript.
But here's where the
whose
filter of AppleScript comes to the fore. The following looks like it should work, but the result is
{}
(an empty list = nothing).
get first slide of active presentation whose slide ID is 1257
The formulation
first element whose...
is inconsistent in the Office
applications: sometimes it works, sometimes it gets you the same result as
every element whose...
instead. An option here is to:
get every slide of active presentation whose slide ID is 1257
This gets you the right result in list braces.
--> {slide 1 of active presentation}There can only ever be one slide with that ID, so
every
gets you the single-item list. Here's the solution:
item 1 of (get every slide of active presentation whose slide ID is 1257) --> slide 1 of active presentation
Insert a slide
There is no AppleScript equivalent of the InsertFromFile method that allows you to insert an existing file of saved slide(s) into a presentation. Here's a chance to try out do Visual Basic. You can still convert your macro, and when you get to a line using InsertFromFile, such as the following:
ActivePresentation.Slides.InsertFromFile _ "Macintosh HD:Users:Shared:Sales.ppt", 2, 3, 6
You can use the following:
do Visual Basic "ActivePresentation.Slides.InsertFromFile _ \"Macintosh HD:Users:Shared:Sales.ppt\", 2, 3, 6"
Note the escape backslashes, \, before the internal quotes, and the fact that you do not need to close the outer AppleScript quotes for each line of the quoted VBA. Remember that this line will fail in Office 2008.


