TweetFollow Us on Twitter

Introduction to Scripting Microsoft PowerPoint

Volume Number: 23 (2007)
Issue Number: 03
Column Tag: Scripting

AppleScript Essentials

Introduction to Scripting Microsoft PowerPoint

by Benjamin S. Waldie

In recent months, we have been discussing ways to automate the Office applications using AppleScript. We have discussed Word and Excel scripting, and this month, we are going to focus on scripting PowerPoint.

In Office X, PowerPoint's AppleScript dictionary contained a single command -- do Visual Basic. While no direct AppleScript-ability was present, this command at least provided a way to initiate Visual Basic macrocode from AppleScript in order to automate some tasks. Of course, to do this, one needed to be fluent in Visual Basic.

With the release of Office 2004, Microsoft introduced re-worked AppleScript support in the Office applications. Word and Excel both had their AppleScript dictionaries substantially re-written and expanded, and PowerPoint introduced extensive AppleScript support. Sure, you can still use the do Visual Basic command to automate PowerPoint, if you wish. But, this isn't necessary anymore, as AppleScript code can now be written to perform repetitive tasks directly. Furthermore, Microsoft has announced that Visual Basic support will be removed from the Office applications when Office 2008 is released, thus rendering any do Visual Basic code useless moving forward.

In this month's column, we will explore the AppleScript support in PowerPoint 11, released with Office 2004. In future versions of PowerPoint, much of the terminology we will discuss is likely to remain functional, although it is always good practice to test code for terminology changes when performing any application upgrades in a scripted workflow. Let's get started.

Working with Presentations

Making a Presentation

In PowerPoint, the base class in which you will work is a presentation. To create a new presentation, use the make command, followed by the presentation class, as demonstrated here.

tell application "Microsoft PowerPoint"
   make new presentation
end tell
--> presentation "Presentation1" of application "Microsoft PowerPoint"

The result of the make command is a reference to the newly created presentation. This may be placed into a variable, if desired, for future reference throughout your code.

Referencing the Front most Presentation

It's important to understand how to reference the front most presentation in PowerPoint. Like documents in most applications, presentations can be referenced by index. However, unlike many other applications, a PowerPoint presentation's index does not refer to its front to back ordering. Rather, it refers to the order in which the presentation was opened or created, in reference to the other currently opened presentations. So, it is never safe to assume that presentation 1 is the front most presentation. To ensure reference to the front most presentation, refer to the active presentation property of the application class instead, as demonstrated by the example code below.

tell application "Microsoft PowerPoint"
   active presentation
end tell
--> active presentation of application "Microsoft PowerPoint"

Note that the code above results in an ambiguous reference to the active presentation of the application, and not a specific presentation. If another presentation is brought to the front, then this reference will begin pointing to that presentation. Keep this in mind if you ever find that your code is not targeting the anticipated presentation, and verify the presentation ordering.

Opening a Presentation

To open a presentation file on disk, use the open command. For example:

set thePath to choose file with prompt "Please select a presentation:"
tell application "Microsoft PowerPoint"
   open thePath
end tell

When using the open command, please note that a result is not returned. Therefore, if your code will begin processing the newly opened presentation, you will need to form a reference to that presentation. While you could reference the active presentation property of the application, this is not always the safest method. To ensure an accurate reference to the newly opened presentation, locate the presentation whose file path is equal to the path from which the presentation was just opened. A presentation's path can be found by referencing its full name property. The following code demonstrates how to open a presentation, and then build a reference to the opened presentation by matching the opened path to the presentation's full name property.

set thePath to choose file with prompt "Please select a presentation:"
tell application "Microsoft PowerPoint"
   open thePath
   set theOpenedPresentation to first presentation whose full name = (thePath as string)
end tell
--> presentation 1 of application "Microsoft PowerPoint"

Saving a Presentation

To save a presentation that has been saved previously, use the save command, as follows:

tell application "Microsoft PowerPoint"
   save active presentation
end tell

This will cause the presentation to be saved in its original format back to its original path. You can also save a presentation into a new path, or in a different format. To do this, make use of the save command's optional parameters in and as. The following code demonstrates how to save a presentation to the desktop in presentation format.

set theOutputPath to (path to desktop folder as string) & "My Preso.ppt"
tell application "Microsoft PowerPoint"
   save active presentation in theOutputPath as save as presentation
end tell

Other supported save formats include presentation template, HTML, and PowerPoint show. You are encouraged to explore saving presentations in other formats further on your own.

Closing a Presentation

To close a presentation, simply use the close command, followed by a reference to the presentation you wish to close.

tell application "Microsoft PowerPoint"
   close active presentation
end tell

Although the close command has an optional saving parameter, which is supposed to allow you to specify a yes/no/ask constant value indicating whether the presentation should be saved when closed, PowerPoint seems to ignore it. To ensure that a presentation is saved before being closed, be sure to use the save command to save the presentation, and then issue the close command. For example:

set theOutputPath to (path to desktop folder as string) & "My Preso.ppt"
tell application "Microsoft PowerPoint"
   tell active presentation
      save in theOutputPath
      close
   end tell
end tell

Working with Slides

In PowerPoint, content is contained within the slides of a presentation, and much of the AppleScript code you will be writing will involve the manipulation of slide content. First, we'll discuss creating slides, and then we will explore ways of manipulating slide content.

Making a New Slide

To create a new slide within a presentation, use the make command. In doing so, you may also with to specify properties for the newly created slide, such as a layout style. This can be done by using the make command's with properties parameter. The following example code demonstrates how to create a new text slide in the front most presentation. As you will see, the result of the make command will be a reference to the newly created slide.

tell application "Microsoft PowerPoint"
   tell active presentation
      make new slide at end with properties {layout:slide layout text slide}
   end tell
end tell
--> slide 2 of active presentation of application "Microsoft PowerPoint"

Manipulating Slide Text

There are numerous ways of manipulating text content within slides. You can change the text itself, and you can also change attributes of the text, such as font, style, color, and so forth. We'll discuss a few different ways to manipulate slide text. You are encouraged to explore these and others further on your own.

The first thing to understand when working with text content on slides is that the text is not directly contained within the slide itself. Rather, it is contained within shapes that reside on the slide. PowerPoint's shape class possesses a text frame property, which itself is a class possessing numerous properties. One property of the text frame class is text range, which references yet another class, called text range. Text range has numerous properties, one of which is content. To change the text content of a shape on a slide, this is the property you will want to modify. It sounds a bit complicated, but it's really not, as demonstrated by the code below. This code will set the content of the first text shape on slide 2 of our presentation to the text "TEST HEADING".

tell application "Microsoft PowerPoint"
   tell slide 2 of active presentation
      set content of text range of text frame of shape 1 to "TEST HEADING"
   end tell
end tell

Font and style attributes are applied via the font property of a text range, which, again, references a class itself. Attributes such as bold, underline, italic, and more, are all applied using the font class. The following example code demonstrates how to adjust font attributes in this manner. This code will first set the content of the second shape on slide 2 of our presentation to the text "Test Content". It will then change the font, point size, and color of the text. See figure 1 for an example of the result of this code.

tell application "Microsoft PowerPoint"
   tell slide 2 of active presentation
      set content of text range of text frame of shape 2 to "Test Content"
      tell font of text range of text frame of shape 2
         set font name to "Futura"
         set font size to 24
         set font color to {255, 0, 0}
      end tell
   end tell
end tell


Figure 1. Styled Slide Text

Adding a Picture to a Slide

Adding a picture to a slide becomes slightly more complicated. To do this, you must first create a picture class on the target slide, while setting certain attributes for the picture, including its path, top, and left position. The following example code demonstrates how this is done. This code will first prompt the user to locate a picture file. It will then create a picture class at the specified top and left position on the target slide. The picture will then be scaled, relative to its original image size. An example of a slide containing an image placed using this code can be found in figure 2.


Figure 2. Placed Picture Content

set thePicturePath to (choose file with prompt "Please select a picture:") as string
tell application "Microsoft PowerPoint"
   tell slide 2 of active presentation
      set thePicture to make new picture at end with properties ¬
      {top:200, left position:400, lock aspect ratio:true, file name:thePicturePath}
      tell thePicture
         scale height factor 0.1 scale scale from top left with relative to original size
         scale width factor 0.1 scale scale from top left with relative to original size
      end tell
   end tell
end tell

Applying a Background to a Slide

Using AppleScript, it is possible to change the background of a slide. First, to ensure that background of the master slide is not modified, you'll probably want to disassociate the target slide's background from the master. Setting the slide's follow master background property to false does this.

To change the color of a slide's background, adjust the fore color property of the slide background's fill format to the desired RGB value. The following example code demonstrates how this is done. This code will also first disassociate the slide's background from the master.

tell application "Microsoft PowerPoint"
   tell slide 2 of active presentation
     set follow master background to false
     set fore color of fill format of background to {0, 0, 255}
   end tell
end tell

Other background attributes are also modifiable via AppleScript, including pattern, texture, and more. The code below shows how to apply a blue tissue paper texture as the texture of a slide's background. Figure 3 shows an example of the result of this code.

tell application "Microsoft PowerPoint"
   tell slide 2 of active presentation
      set follow master background to false
      preset textured background texture texture blue tissue paper
   end tell
end tell


Figure 3. An Applied Slide Background Texture

Working with Slideshows

Applying Slide Transitions

Preparing presentations for slideshow mode is another task that AppleScript can perform quite easily. Slide show settings and transition settings are both accessible to AppleScript. The following code demonstrates how to loop through the slides of a presentation, applying a dissolve entry transition to each slide.

tell application "Microsoft PowerPoint"
   tell active presentation
      set theSlideCount to count slides
      repeat with a from 1 to theSlideCount
         set entry effect of slide show transition of slide a to entry effect dissolve
      end repeat
   end tell
end tell

Running a Slideshow

Once your slides are complete, you may want your script to run the slideshow. To do this, you will probably first want to bring PowerPoint to the front. Use the activate command to do this. Next, use the run slide show command, targeting the slide show settings of the presentation you want to run, as shown here.

tell application "Microsoft PowerPoint"
   activate
   run slide show slide show settings of active presentation
end tell

Exiting a Slideshow

Exiting a slideshow is bit different than you might expect. You don't exit the presentation. Rather, you exit the slide show view of the slide show window of the presentation. For example:

tell application "Microsoft PowerPoint"
   exit slide show slide show view of slide show window of active presentation
end tell

In Closing

While we have truly only scratched the surface of what's possible by AppleScripting PowerPoint, the techniques discussed in this month's column should give you a good starting point. For more information about scripting PowerPoint, be sure to browse PowerPoint's AppleScript dictionary. Also, don't miss the PowerPoint AppleScript Reference Guide, available for free download from Microsoft's Mactopia website at http://www.microsoft.com/mac/resources/resources.aspx?pid=asforoffice.

Until next time, keep scripting!


Ben Waldie is the author of the best selling books "AppleScripting the Finder" and the "Mac OS X Technology Guide to Automator", available from http://www.spiderworks.com, as well as an AppleScript Training CD, available from http://www.vtc.com. Ben is also president of Automated Workflows, LLC, a company specializing in AppleScript and workflow automation consulting. For years, Ben has developed professional AppleScript-based solutions for businesses including Adobe, Apple, NASA, PC World, and TV Guide. For more information about Ben, please visit http://www.automatedworkflows.com, or email Ben at ben@automatedworkflows.com.
 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Fresh From the Land Down Under – The Tou...
After a two week hiatus, we are back with another episode of The TouchArcade Show. Eli is fresh off his trip to Australia, which according to him is very similar to America but more upside down. Also kangaroos all over. Other topics this week... | Read more »
TouchArcade Game of the Week: ‘Dungeon T...
I’m a little conflicted on this week’s pick. Pretty much everyone knows the legend of Dungeon Raid, the match-3 RPG hybrid that took the world by storm way back in 2011. Everyone at the time was obsessed with it, but for whatever reason the... | Read more »
SwitchArcade Round-Up: Reviews Featuring...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for July 19th, 2024. In today’s article, we finish up the week with the unusual appearance of a review. I’ve spent my time with Hot Lap Racing, and I’m ready to give my verdict. After... | Read more »
Draknek Interview: Alan Hazelden on Thin...
Ever since I played my first release from Draknek & Friends years ago, I knew I wanted to sit down with Alan Hazelden and chat about the team, puzzle games, and much more. | Read more »
The Latest ‘Marvel Snap’ OTA Update Buff...
I don’t know about all of you, my fellow Marvel Snap (Free) players, but these days when I see a balance update I find myself clenching my… teeth and bracing for the impact to my decks. They’ve been pretty spicy of late, after all. How will the... | Read more »
‘Honkai Star Rail’ Version 2.4 “Finest D...
HoYoverse just announced the Honkai Star Rail (Free) version 2.4 “Finest Duel Under the Pristine Blue" update alongside a surprising collaboration. Honkai Star Rail 2.4 follows the 2.3 “Farewell, Penacony" update. Read about that here. | Read more »
‘Vampire Survivors+’ on Apple Arcade Wil...
Earlier this month, Apple revealed that poncle’s excellent Vampire Survivors+ () would be heading to Apple Arcade as a new App Store Great. I reached out to poncle to check in on the DLC for Vampire Survivors+ because only the first two DLCs were... | Read more »
Homerun Clash 2: Legends Derby opens for...
Since launching in 2018, Homerun Clash has performed admirably for HAEGIN, racking up 12 million players all eager to prove they could be the next baseball champions. Well, the title will soon be up for grabs again, as Homerun Clash 2: Legends... | Read more »
‘Neverness to Everness’ Is a Free To Pla...
Perfect World Games and Hotta Studio (Tower of Fantasy) announced a new free to play open world RPG in the form of Neverness to Everness a few days ago (via Gematsu). Neverness to Everness has an urban setting, and the two reveal trailers for it... | Read more »
Meditative Puzzler ‘Ouros’ Coming to iOS...
Ouros is a mediative puzzle game from developer Michael Kamm that launched on PC just a couple of months back, and today it has been revealed that the title is now heading to iOS and Android devices next month. Which is good news I say because this... | Read more »

Price Scanner via MacPrices.net

Amazon is still selling 16-inch MacBook Pros...
Prime Day in July is over, but Amazon is still selling 16-inch Apple MacBook Pros for $500-$600 off MSRP. Shipping is free. These are the lowest prices available this weekend for new 16″ Apple... Read more
Walmart continues to sell clearance 13-inch M...
Walmart continues to offer clearance, but new, Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for $699, $300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBooks... Read more
Apple is offering steep discounts, up to $600...
Apple has standard-configuration 16″ M3 Max MacBook Pros available, Certified Refurbished, starting at $2969 and ranging up to $600 off MSRP. Each model features a new outer case, shipping is free,... Read more
Save up to $480 with these 14-inch M3 Pro/M3...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more
Amazon has clearance 9th-generation WiFi iPad...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80-$100 off MSRP, starting only $249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Apple is offering a $50 discount on 2nd-gener...
Apple has Certified Refurbished White and Midnight HomePods available for $249, Certified Refurbished. That’s $50 off MSRP and the lowest price currently available for a full-size Apple HomePod today... Read more
The latest MacBook Pro sale at Amazon: 16-inc...
Amazon is offering instant discounts on 16″ M3 Pro and 16″ M3 Max MacBook Pros ranging up to $400 off MSRP as part of their early July 4th sale. Shipping is free. These are the lowest prices... Read more
14-inch M3 Pro MacBook Pros with 36GB of RAM...
B&H Photo has 14″ M3 Pro MacBook Pros with 36GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 Pro MacBook Pro (... Read more
14-inch M3 MacBook Pros with 16GB of RAM on s...
B&H Photo has 14″ M3 MacBook Pros with 16GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $150-$200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 MacBook Pro (... Read more
Amazon is offering $170-$200 discounts on new...
Amazon is offering a $170-$200 discount on every configuration and color of Apple’s M3-powered 15″ MacBook Airs. Prices start at $1129 for models with 8GB of RAM and 256GB of storage: – 15″ M3... Read more

Jobs Board

*Apple* Systems Engineer - Chenega Corporati...
…LLC,** a **Chenega Professional Services** ' company, is looking for a ** Apple Systems Engineer** to support the Information Technology Operations and Maintenance Read more
Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
*Apple* / Mac Administrator - JAMF Pro - Ame...
Amentum is seeking an ** Apple / Mac Administrator - JAMF Pro** to provide support with the Apple Ecosystem to include hardware and software to join our team and Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.