Describes different types of things I find interesting.

Sunday, August 23, 2009

Mac PowerPoint Scripting

Scripting in PowerPoint can help automate most repetitive tasks. The example script here will automatically go through all your slides and add a footer that gives the title of the next slide coming up. This helps during a presentation because you can see what's coming and transition well to the next slide.

A future blog post will show a PowerPoint script I use to add a progress bar showing the remaining slides.

Warning: This example is specific to Mac and uses AppleScript. It will not work on Windows. Windows PowerPoint scripting is done in VBA, not AppleScript.

For those in a hurry (or who have coded before), here is all the code:

tell application "Microsoft PowerPoint"

set oPres to active presentation

set mySlide to 1

tell active presentation to set currentSlide to slide mySlide

set titleTypes to {placeholder type title placeholder, placeholder type center title placeholder, placeholder type vertical title placeholder}

set footerTypes to {placeholder type footer placeholder}

repeat with currentSlide in (get every slide of oPres)

if mySlide > 1 then

-- Get the title of the current slide

repeat with i from 1 to count shapes of currentSlide

set currentShape to shape i of currentSlide

set aType to placeholder type of currentShape

if aType is not missing value and aType is in titleTypes then set myTitle to currentShape

end repeat

-- Get the footer of the previous slide

repeat with i from 1 to count shapes of prevSlide

set currentShape to shape i of prevSlide

set aType to placeholder type of currentShape

if aType is in footerTypes then set myFooter to currentShape

end repeat

-- Set the previous slide footer text to current's title

set prevSlide to mySlide - 1

set fText to "Coming up: " & content of text range of text frame of myTitle

set header footer text of footer of headers and footers of slide prevSlide of active presentation to fText

end if

set prevSlide to currentSlide

set mySlide to mySlide + 1

end repeat

set mySlide to mySlide - 1

set header footer text of footer of headers and footers of slide mySlide of active presentation to "End of presentation"

return currentSlide

end tell


To use this code open Script Editor.app

  1. Choose File->New
  2. Paste all the code in the window
  3. Save the file as: Documents/Microsoft User Data/PowerPoint Script Menu Items/addNextSlideFooter.scptd
  4. Open a PowerPoint presentation
  5. Ensure that footers are being shown (if they aren't just add a footer with any text in it. This script will REPLACE the footer text)
  6. Choose the automator menu (squiggly option to the right of Help)
  7. Choose the name of your script (addNextSlideFooter.scptd)
This has worked for me on PowerPoint 2008 on Mac OSX. This will NOT work on Windows! If you write the same script for Windows in VBA let me know and I'll link it here.

Same code with more details (in red)


tell application "Microsoft PowerPoint" -- This just tells the script we're using PowerPoint

-- set X to Y just sets a variable. Similar to x = y in other languages.

set oPres to active presentation

set mySlide to 1

tell active presentation to set currentSlide to slide mySlide

-- We need to create a list of types that could possibly hold the title so we can get the title later.

-- Luckily, if you're using a PowerPoint template there are some specific title placeholders.

-- If you aren't using a template (or any of these placeholders) for the title, this whole script won't work.

set titleTypes to {placeholder type title placeholder, placeholder type center title placeholder, placeholder type vertical title placeholder}

set footerTypes to {placeholder type footer placeholder}



-- Loop over all slides in the presentation, setting currentSlide to the current slide in each loop.

repeat with currentSlide in (get every slide of oPres)


-- We set the title of the previous slide, so if we're on slide 1, skip it!

if mySlide > 1 then

-- Get the title of the current slide by searching all the shapes for one of

-- the correct type.

repeat with i from 1 to count shapes of currentSlide

set currentShape to shape i of currentSlide

set aType to placeholder type of currentShape


-- If there is a title, store it in the variable myTitle

if aType is not missing value and aType is in titleTypes then set myTitle to currentShape

end repeat

-- Get the footer of the previous slide (same this as the title, but for the previous slide)

repeat with i from 1 to count shapes of prevSlide

set currentShape to shape i of prevSlide

set aType to placeholder type of currentShape

if aType is in footerTypes then set myFooter to currentShape

end repeat

-- Set the previous slide footer text to current's title

set prevSlide to mySlide - 1

set fText to "Coming up: " & content of text range of text frame of myTitle

set header footer text of footer of headers and footers of slide prevSlide of active presentation to fText

end if

set prevSlide to currentSlide -- Update some variables

set mySlide to mySlide + 1

end repeat


-- Make sure the last slide says "End of presentation" in the footer.

set mySlide to mySlide - 1

set header footer text of footer of headers and footers of slide mySlide of active presentation to "End of presentation"

return currentSlide

end tell -- DONE!


No comments:

Post a Comment

Followers

About Me

I am currently an instructor at George Mason University. I teach Computer Science and Software Engineering. My research focus is computer vision. In addition to my research in computer vision, I am interested in software engineering processes to help teams succeed in building software. I also enjoy building things from wooden furniture to robotics.