Friday, July 21, 2017

#1 2017-06-29 07:38:31 am

phil_zrh
Member
Registered: 2017-06-29
Posts: 14

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

Offline

 

#2 2017-06-29 06:14:09 pm

haolesurferdude
Member
Registered: 2010-02-19
Posts: 91

Re: Search and Replace Text in Powerpoint using AppleScript

Try this:

Applescript:


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

Offline

 

#3 2017-06-30 03:00:51 am

phil_zrh
Member
Registered: 2017-06-29
Posts: 14

Re: Search and Replace Text in Powerpoint using AppleScript

Hi haolesurferdude,

This is brilliant! Thank you very much for that. It works almost as expected. 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

[code] if shapeText = findText then[/code]
would return false for those textboxes.

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

Best,
Phil

Last edited by phil_zrh (2017-06-30 03:03:02 am)

Offline

 

#4 2017-06-30 07:33:52 am

haolesurferdude
Member
Registered: 2010-02-19
Posts: 91

Re: Search and Replace Text in Powerpoint using AppleScript

Try this:

Applescript:


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

Offline

 

#5 2017-06-30 09:35:16 am

mleslie
Member
From:: indoors
Registered: 2005-02-09
Posts: 60

Re: Search and Replace Text in Powerpoint using AppleScript

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:

Applescript:


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

Offline

 

#6 2017-06-30 11:07:17 am

haolesurferdude
Member
Registered: 2010-02-19
Posts: 91

Re: Search and Replace Text in Powerpoint using AppleScript

Good move!

Offline

 

#7 2017-06-30 11:47:55 am

phil_zrh
Member
Registered: 2017-06-29
Posts: 14

Re: Search and Replace Text in Powerpoint using AppleScript

Guys you're literally killing me! Can't tell you how you helped me out. smile smile smile

Will try asap to implement it and let you know!

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)