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

Tokkun Studio unveils alpha trailer for...
We are back on the MMORPG news train, and this time it comes from the sort of international developers Tokkun Studio. They are based in France and Japan, so it counts. Anyway, semantics aside, they have released an alpha trailer for the upcoming... | Read more »
Win a host of exclusive in-game Honor of...
To celebrate its latest Jujutsu Kaisen crossover event, Honor of Kings is offering a bounty of login and achievement rewards kicking off the holiday season early. [Read more] | Read more »
Miraibo GO comes out swinging hard as it...
Having just launched what feels like yesterday, Dreamcube Studio is wasting no time adding events to their open-world survival Miraibo GO. Abyssal Souls arrives relatively in time for the spooky season and brings with it horrifying new partners to... | Read more »
Ditch the heavy binders and high price t...
As fun as the real-world equivalent and the very old Game Boy version are, the Pokemon Trading Card games have historically been received poorly on mobile. It is a very strange and confusing trend, but one that The Pokemon Company is determined to... | Read more »
Peace amongst mobile gamers is now shatt...
Some of the crazy folk tales from gaming have undoubtedly come from the EVE universe. Stories of spying, betrayal, and epic battles have entered history, and now the franchise expands as CCP Games launches EVE Galaxy Conquest, a free-to-play 4x... | Read more »
Lord of Nazarick, the turn-based RPG bas...
Crunchyroll and A PLUS JAPAN have just confirmed that Lord of Nazarick, their turn-based RPG based on the popular OVERLORD anime, is now available for iOS and Android. Starting today at 2PM CET, fans can download the game from Google Play and the... | Read more »
Digital Extremes' recent Devstream...
If you are anything like me you are impatiently waiting for Warframe: 1999 whilst simultaneously cursing the fact Excalibur Prime is permanently Vault locked. To keep us fed during our wait, Digital Extremes hosted a Double Devstream to dish out a... | Read more »
The Frozen Canvas adds a splash of colou...
It is time to grab your gloves and layer up, as Torchlight: Infinite is diving into the frozen tundra in its sixth season. The Frozen Canvas is a colourful new update that brings a stylish flair to the Netherrealm and puts creativity in the... | Read more »
Back When AOL WAS the Internet – The Tou...
In Episode 606 of The TouchArcade Show we kick things off talking about my plans for this weekend, which has resulted in this week’s show being a bit shorter than normal. We also go over some more updates on our Patreon situation, which has been... | Read more »
Creative Assembly's latest mobile p...
The Total War series has been slowly trickling onto mobile, which is a fantastic thing because most, if not all, of them are incredibly great fun. Creative Assembly's latest to get the Feral Interactive treatment into portable form is Total War:... | Read more »

Price Scanner via MacPrices.net

Early Black Friday Deal: Apple’s newly upgrad...
Amazon has Apple 13″ MacBook Airs with M2 CPUs and 16GB of RAM on early Black Friday sale for $200 off MSRP, only $799. Their prices are the lowest currently available for these newly upgraded 13″ M2... Read more
13-inch 8GB M2 MacBook Airs for $749, $250 of...
Best Buy has Apple 13″ MacBook Airs with M2 CPUs and 8GB of RAM in stock and on sale on their online store for $250 off MSRP. Prices start at $749. Their prices are the lowest currently available for... Read more
Amazon is offering an early Black Friday $100...
Amazon is offering early Black Friday discounts on Apple’s new 2024 WiFi iPad minis ranging up to $100 off MSRP, each with free shipping. These are the lowest prices available for new minis anywhere... Read more
Price Drop! Clearance 14-inch M3 MacBook Pros...
Best Buy is offering a $500 discount on clearance 14″ M3 MacBook Pros on their online store this week with prices available starting at only $1099. Prices valid for online orders only, in-store... Read more
Apple AirPods Pro with USB-C on early Black F...
A couple of Apple retailers are offering $70 (28%) discounts on Apple’s AirPods Pro with USB-C (and hearing aid capabilities) this weekend. These are early AirPods Black Friday discounts if you’re... Read more
Price drop! 13-inch M3 MacBook Airs now avail...
With yesterday’s across-the-board MacBook Air upgrade to 16GB of RAM standard, Apple has dropped prices on clearance 13″ 8GB M3 MacBook Airs, Certified Refurbished, to a new low starting at only $829... Read more
Price drop! Apple 15-inch M3 MacBook Airs now...
With yesterday’s release of 15-inch M3 MacBook Airs with 16GB of RAM standard, Apple has dropped prices on clearance Certified Refurbished 15″ 8GB M3 MacBook Airs to a new low starting at only $999.... Read more
Apple has clearance 15-inch M2 MacBook Airs a...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs now available starting at $929 and ranging up to $410 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at... Read more
Apple drops prices on 13-inch M2 MacBook Airs...
Apple has dropped prices on 13″ M2 MacBook Airs to a new low of only $749 in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, now available for $679 for 8-Core CPU/7-Core GPU/256GB models. Apple’s one-year warranty is included, shipping is free, and each... Read more

Jobs Board

Seasonal Cashier - *Apple* Blossom Mall - J...
Seasonal Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Seasonal Fine Jewelry Commission Associate -...
…Fine Jewelry Commission Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) Read more
Seasonal Operations Associate - *Apple* Blo...
Seasonal Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Read more
Hair Stylist - *Apple* Blossom Mall - JCPen...
Hair Stylist - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom 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.