Search and Replace Text in Powerpoint using AppleScript

Hi all,

my name is Phil and I’m rather new to doing AppleScript. I’m currently facing the following problem:

We want to create personalized powerpoint presentations for client send out. AppleScript should first ask the user about the name and company name of the client. Apple script shall call up the Master Presentation and should look for the placeholders “%company%” and “%name%” and should replace them with the user input. I’ve found quite a few pieces of code for word and excel but can’t figure out on how to get it running on ppt.

I guess its not that hard but I couldn’t find anything suitable within the last hours…

Thx a million for helping.

Best,
Phil

So far I have compiled the following:
set persname to the text returned of (display dialog “Name?” default answer “”)
set company to the text returned of (display dialog “Company?” default answer “”)
set filepath to POSIX path of “Path…:Company Presentation MASTER.pptx”
try
set command to "open " & quoted form of filepath
do shell script command
end try

Try this:


property theName : "%name%" as text
property theCompany : "%company%" as text

set theFile to choose file with prompt "Select Master PPT File"
set persname to the text returned of (display dialog "Enter Person Name?" with title "Master PPT Query" default answer "")
set company to the text returned of (display dialog "Enter Company Name?" with title "Master PPT Query" default answer "")

tell application "Microsoft PowerPoint" to open theFile

pptTextReplace(theCompany, company)
pptTextReplace(theName, persname)

on pptTextReplace(findText, replaceText)
	tell application "Microsoft PowerPoint"
		activate
		repeat with slideNumber from 1 to count of slides of active presentation
			tell slide slideNumber of active presentation
				repeat with shapeNumber from 1 to count of shapes
					set shapeText to content of text range of text frame of shape shapeNumber
					if shapeText = findText then
						set content of text range of text frame of shape shapeNumber to replaceText
					end if
				end repeat
			end tell
		end repeat
	end tell
end pptTextReplace


Hi haolesurferdude,

This is brilliant! Thank you very much for that. It works almost as expected. :slight_smile:
Remaining problem is that the search terms are mainly within a context (sentences) of a text box.

In this case, the code wouldn’t execute as

 if shapeText = findText then

would return false for those textboxes.

Is there any way of digging one level deeper within the text box?

Best,
Phil

Try this:


property theName : "%name%" as text
property theCompany : "%company%" as text

set theFile to choose file with prompt "Select Master PPT File"
set persname to the text returned of (display dialog "Enter Person Name?" with title "Master PPT Query" default answer "")
set company to the text returned of (display dialog "Enter Company Name?" with title "Master PPT Query" default answer "")

tell application "Microsoft PowerPoint" to open theFile

pptFindNReplace(theCompany, company)
pptFindNReplace(theName, persname)

on pptFindNReplace(findText, replaceText)
	tell application "Microsoft PowerPoint"
		activate
		repeat with slideNumber from 1 to count of slides of active presentation
			tell slide slideNumber of active presentation
				repeat with shapeNumber from 1 to count of shapes
					set shapeText to content of text range of text frame of shape shapeNumber
					set newText to my replaceText(shapeText, findText, replaceText)
					if newText ≠ shapeText then
						set content of text range of text frame of shape shapeNumber to newText
					end if
				end repeat
			end tell
		end repeat
	end tell
end pptFindNReplace

on replaceText(theText as text, findString as text, replaceString as text)
	(* Takes a text string and replaces the findString with the replaceString. Can be just 1 character in either or both findString and replaceString. *)
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, findString}
	set textItems to text items of theText
	set AppleScript's text item delimiters to replaceString
	set theResult to textItems as text
	set AppleScript's text item delimiters to oTIDs
	return theResult
end replaceText


This looks super useful! I ran it against some of the templates I typically use, trying it out as a more global PPT text find/replace, and ran into an issue. Sometimes “shapeText” comes up empty and causes the script to halt.

I added a test for this missing value condition in your subroutine to resolve this:


on pptFindNReplace(findText, replaceText)
	tell application "Microsoft PowerPoint"
		activate
		repeat with slideNumber from 1 to count of slides of active presentation
			tell slide slideNumber of active presentation
				repeat with shapeNumber from 1 to count of shapes
					set shapeText to content of text range of text frame of shape shapeNumber
					if shapeText is not missing value then
						set newText to my replaceText(shapeText, findText, replaceText)
						if newText ≠ shapeText then
							set content of text range of text frame of shape shapeNumber to newText
						end if
					end if
				end repeat
			end tell
		end repeat
	end tell
end pptFindNReplace

Good move!

Guys you’re literally killing me! Can’t tell you how you helped me out. :slight_smile: :slight_smile: :slight_smile:

Will try asap to implement it and let you know!

Resurrecting this old thread, as it helped me get partway to where I need to go.

I need to search-and-replace text in hundreds of PowerPoint presentations. The above, with the replaceText routine, works well to change the replace substring into the text string that’s then passed to the contents. The issue in my files is that the parts of the string that are not part of the replace string have different formatting on them. Using “set content” etc. retains the correct text, but strips out that formatting.

Using find-replace in the GUI works perfectly and doesn’t disturb the formatted text. However, we don’t want to do that manually over hundreds of files. There may also be more than one change to make to each of these files, so that becomes very time-consuming.

Anybody got any brilliant ideas for how to changing part of the contents while leaving the rest as-is? I may have to resort to scripting XML changes, but I’m hoping there’s a better way.

Thanks for any insight you might have!