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

Go from lowly lizard to wicked Wyvern in...
Do you like questing, and do you like dragons? If not then boy is this not the announcement for you, as Loongcheer Game has unveiled Quest Dragon: Idle Mobile Game. Yes, it is amazing Square Enix hasn’t sued them for copyright infringement, but... | Read more »
Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »
Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »
Embark into the frozen tundra of certain...
Chucklefish, developers of hit action-adventure sandbox game Starbound and owner of one of the cutest logos in gaming, has released their roguelike deck-builder Wildfrost. Created alongside developers Gaziter and Deadpan Games, Wildfrost will... | Read more »

Price Scanner via MacPrices.net

13-inch M2 MacBook Airs in stock today at App...
Apple has 13″ M2 MacBook Airs available for only $849 today in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty is included,... Read more
New today at Apple: Series 9 Watches availabl...
Apple is now offering Certified Refurbished Apple Watch Series 9 models on their online store for up to $80 off MSRP, starting at $339. Each Watch includes Apple’s standard one-year warranty, a new... Read more
The latest Apple iPhone deals from wireless c...
We’ve updated our iPhone Price Tracker with the latest carrier deals on Apple’s iPhone 15 family of smartphones as well as previous models including the iPhone 14, 13, 12, 11, and SE. Use our price... Read more
Boost Mobile will sell you an iPhone 11 for $...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering an iPhone 11 for $149.99 when purchased with their $40 Unlimited service plan (12GB of premium data). No trade-in is required... Read more
Free iPhone 15 plus Unlimited service for $60...
Boost Infinite, part of MVNO Boost Mobile using AT&T and T-Mobile’s networks, is offering a free 128GB iPhone 15 for $60 per month including their Unlimited service plan (30GB of premium data).... Read more
$300 off any new iPhone with service at Red P...
Red Pocket Mobile has new Apple iPhones on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide MVNO using all the major wireless carrier... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, available for $759 for 8-Core CPU/7-Core GPU/256GB models and $929 for 8-Core CPU/8-Core GPU/512GB models. Apple’s one-year warranty is... Read more
Updated Apple MacBook Price Trackers
Our Apple award-winning MacBook Price Trackers are continually updated with the latest information on prices, bundles, and availability for 16″ and 14″ MacBook Pros along with 13″ and 15″ MacBook... Read more
Every model of Apple’s 13-inch M3 MacBook Air...
Best Buy has Apple 13″ MacBook Airs with M3 CPUs in stock and on sale today for $100 off MSRP. Prices start at $999. Their prices are the lowest currently available for new 13″ M3 MacBook Airs among... Read more
Sunday Sale: Apple iPad Magic Keyboards for 1...
Walmart has Apple Magic Keyboards for 12.9″ iPad Pros, in Black, on sale for $150 off MSRP on their online store. Sale price for online orders only, in-store price may vary. Order online and choose... Read more

Jobs Board

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
DMR Technician - *Apple* /iOS Systems - Haml...
…relevant point-of-need technology self-help aids are available as appropriate. ** Apple Systems Administration** **:** Develops solutions for supporting, deploying, Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple 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.