Script to find/replace formatting

T

teacherpoet

Any forward motion on this find & replace applescript? Still hoping to get it to work.
 
S

Shawn Larson [MSFT]

I¹ve included an example below. This example finds and replaces styles,
fonts, and paragraph formatting. You can certainly modify this code and do
replacements for specific formatting and you do not need to include all the
formats I¹ve included with the example.

Shawn Larson
Mac Word Test
Microsoft MacBU
--
This posting is provided ³AS IS² with no warranties, and confers no rights.
tell application "Microsoft Word"
activate
-- Reference the find object of the text object
set myFind to find object of text object of active document

-- set find and replacement styles/formatting info here
set findStyle to style heading2
set replStyle to style heading1
set findFont to "Times New Roman"
set replFont to "Arial"
set findPara to align paragraph left
set replPara to align paragraph right

(* Replace Styles *)
-- Add formatting info to find object
clear formatting myFind
set style of myFind to findStyle

-- Add formatting info to replacement object
clear formatting replacement of myFind
set style of replacement of myFind to replStyle

-- Find and replace all instances
execute find myFind replace replace all

(* Replace Font Formatting *)
-- Add formatting info to find object
clear formatting myFind
set name of font object of myFind to findFont

-- Add formatting info to replacement object
clear formatting replacement of myFind
set name of font object of replacement of myFind to replFont

-- Find and replace all instances
execute find myFind replace replace all

(* Replace Paragraph Formatting *)
-- Add formatting info to find object
clear formatting myFind
set alignment of paragraph format of myFind to findPara

-- Add formatting info to replacement object
clear formatting replacement of myFind
set alignment of paragraph format of replacement of myFind to replPara

-- Find and replace all instances
execute find myFind replace replace all
end tell
 
T

teacherpoet

Thanks much for this, Shawn. Because I'm an Applescript newbie, however, I don't know how to go about paring this down to achieve only what I want to achieve, and using the correct terms in the correct places to specify what I want to find and what I want to replace.

My objective is to find all instances of simple underlining and replace those with italicizing of the previously underlined text.

This includes finding underlined text in the footnotes/endnotes and the headers/footers.

Daiya Mitchell pointed me toward a Replace Everywhere script that she found as a sample script from the Word 2004 reference (as you'll see from one of the posts above). I tweaked this successfully so that it would replace underlining with italicizing in the main text. But I don't know how to get it to do the same thing with text in the footnotes/endnotes and the headers/footers.

Here's that script:

tell application "Microsoft Word"
set myFind to find object of text object of active document
clear formatting myFind
set underline of font object of myFind to true
set content of myFind to ""
clear formatting replacement of myFind
set underline of font object of replacement of myFind to false
set italic of font object of replacement of myFind to true
set content of replacement of myFind to ""
execute find myFind replace replace all
end tell

This works fine -- it just doesn't do the whole job.
 
S

Shawn Larson [MSFT]

Here¹s a sample script for you, using your Œformatting requirements¹ for
finding and replacing. It is not the most elegant solution, but I wanted to
get a working example posted now, and then later clean it up. This script
will search the main document, all headers and footers, footnotes, endnotes,
and shapes. Enjoy!

Shawn Larson
Mac Word Test
Microsoft MacBU

tell application "Microsoft Word"
activate

(* Active Document *)
set myFind to find object of text object of active document
set content of myFind to ""
clear formatting myFind
set underline of font object of myFind to true
set content of myFind to ""
clear formatting replacement of myFind
set italic of font object of replacement of myFind to true
set underline of font object of replacement of myFind to false

(*Headers & Footers *)
-- Create list of all sections in active document
set lstSections to every section of active document
repeat with currSection in lstSections
-- Create list of all headers and footers in active document
set lstHeaderFooters to {get header currSection index header footer
primary} & {get header currSection index header footer first page} & {get
header currSection index header footer even pages} & {get footer currSection
index header footer primary} & {get footer currSection index header footer
first page} & {get footer currSection index header footer even pages}
-- Loop through all headers and footers
repeat with currHeaderFooter in lstHeaderFooters
set myFind to find object of text object of currHeaderFooter
set content of myFind to ""
clear formatting myFind
set underline of font object of myFind to true
clear formatting replacement of myFind
set content of myFind to ""
set italic of font object of replacement of myFind to true
set underline of font object of replacement of myFind to false
execute find myFind replace replace all

-- Create list of all shapes in header and footer
set lstShapes to (every shape of currHeaderFooter)
-- Loop through all shapes in header and footer
repeat with currShape in lstShapes
if has text of (text frame of currShape) then
set myFind to find object of text range of text frame of
currShape
set content of myFind to ""
clear formatting myFind
set underline of font object of myFind to true
clear formatting replacement of myFind
set content of myFind to ""
set italic of font object of replacement of myFind to
true
set underline of font object of replacement of myFind to
false
execute find myFind replace replace all
end if
end repeat
end repeat
end repeat

(* Footnotes *)
-- Create list of all footnotes in active document
set lstFtNotes to every footnote of active document
-- Loop through all footnotes
repeat with currFtnote in lstFtNotes
set myFind to find object of text object of currFtnote
set content of myFind to ""
clear formatting myFind
set underline of font object of myFind to true
clear formatting replacement of myFind
set content of myFind to ""
set italic of font object of replacement of myFind to true
set underline of font object of replacement of myFind to false
execute find myFind replace replace all
end repeat

(* Endnotes *)
-- Create list of all endnotes in active document
set lstEndNotes to every endnote of active document
-- Loop through all endnotes
repeat with currEndnote in lstEndNotes
set myFind to find object of text object of currEndnote
set content of myFind to ""
clear formatting myFind
set underline of font object of myFind to true
clear formatting replacement of myFind
set content of myFind to ""
set italic of font object of replacement of myFind to true
set underline of font object of replacement of myFind to false
execute find myFind replace replace all
end repeat

(* Shapes *)
-- Create list of all shapes in main document
set lstShapes to (every shape of active document)
-- Loop through all shapes in main document
repeat with currShape in lstShapes
set theTextFrame to (text frame of currShape)
if has text of (text frame of currShape) then
set myFind to find object of text range of text frame of
currShape
set content of myFind to ""
clear formatting myFind
set underline of font object of myFind to true
clear formatting replacement of myFind
set content of myFind to ""
set italic of font object of replacement of myFind to true
set underline of font object of replacement of myFind to false
execute find myFind replace replace all
end if
end repeat
end tell
 
T

teacherpoet

That almost did the trick, Shawn. It handles the headers/footers and footnotes/endnotes fine. Curiously, however, it leaves the main body of the text untouched.

I substituted the script I posted earlier -- the one that successfully handles the main text -- for the section of your script that deals with the main document. Voila! The system works!

I'll append the revised script to this post. I can't comment on its elegance or lack thereof, and of course I invite you to streamline it to your heart's content. But it does the trick as is. Many thanks.

BTW, I run my tests of this on a test file I created that includes underlined text in main body, header, and footnotes. So I know when it works and when it doesn't.

Also, now that I see where you position the search terms, I can easily tweak this for any other replacement I might desire. As I'm not the only person who needs such a script, I encourage you to post this somewhere as a downloadable .zip file to benefit the Word '08 community.

Here's the revised Underline to Italic script:

tell application "Microsoft Word"
activate

(* Active Document *)
set myFind to find object of text object of active document
clear formatting myFind
set underline of font object of myFind to true
set content of myFind to ""
clear formatting replacement of myFind
set underline of font object of replacement of myFind to false
set italic of font object of replacement of myFind to true
set content of replacement of myFind to ""
execute find myFind replace replace all

(*Headers & Footers *)
-- Create list of all sections in active document
set lstSections to every section of active document
repeat with currSection in lstSections
-- Create list of all headers and footers in active document
set lstHeaderFooters to {get header currSection index header footer primary} & {get header currSection index header footer first page} & {get header currSection index header footer even pages} & {get footer currSection index header footer primary} & {get footer currSection index header footer first page} & {get footer currSection index header footer even pages}
-- Loop through all headers and footers
repeat with currHeaderFooter in lstHeaderFooters
set myFind to find object of text object of currHeaderFooter
set content of myFind to ""
clear formatting myFind
set underline of font object of myFind to true
clear formatting replacement of myFind
set content of myFind to ""
set italic of font object of replacement of myFind to true
set underline of font object of replacement of myFind to false
execute find myFind replace replace all

-- Create list of all shapes in header and footer
set lstShapes to (every shape of currHeaderFooter)
-- Loop through all shapes in header and footer
repeat with currShape in lstShapes
if has text of (text frame of currShape) then
set myFind to find object of text range of text frame of currShape
set content of myFind to ""
clear formatting myFind
set underline of font object of myFind to true
clear formatting replacement of myFind
set content of myFind to ""
set italic of font object of replacement of myFind to true
set underline of font object of replacement of myFind to false
execute find myFind replace replace all
end if
end repeat
end repeat
end repeat

(* Footnotes *)
-- Create list of all footnotes in active document
set lstFtNotes to every footnote of active document
-- Loop through all footnotes
repeat with currFtnote in lstFtNotes
set myFind to find object of text object of currFtnote
set content of myFind to ""
clear formatting myFind
set underline of font object of myFind to true
clear formatting replacement of myFind
set content of myFind to ""
set italic of font object of replacement of myFind to true
set underline of font object of replacement of myFind to f
 
T

teacherpoet

Oops! Somehow the end of that script got cut off in the previous post. Here it is, in full:

tell application "Microsoft Word"
activate

(* Active Document *)
set myFind to find object of text object of active document
clear formatting myFind
set underline of font object of myFind to true
set content of myFind to ""
clear formatting replacement of myFind
set underline of font object of replacement of myFind to false
set italic of font object of replacement of myFind to true
set content of replacement of myFind to ""
execute find myFind replace replace all

(*Headers & Footers *)
-- Create list of all sections in active document
set lstSections to every section of active document
repeat with currSection in lstSections
-- Create list of all headers and footers in active document
set lstHeaderFooters to {get header currSection index header footer primary} & {get header currSection index header footer first page} & {get header currSection index header footer even pages} & {get footer currSection index header footer primary} & {get footer currSection index header footer first page} & {get footer currSection index header footer even pages}
-- Loop through all headers and footers
repeat with currHeaderFooter in lstHeaderFooters
set myFind to find object of text object of currHeaderFooter
set content of myFind to ""
clear formatting myFind
set underline of font object of myFind to true
clear formatting replacement of myFind
set content of myFind to ""
set italic of font object of replacement of myFind to true
set underline of font object of replacement of myFind to false
execute find myFind replace replace all

-- Create list of all shapes in header and footer
set lstShapes to (every shape of currHeaderFooter)
-- Loop through all shapes in header and footer
repeat with currShape in lstShapes
if has text of (text frame of currShape) then
set myFind to find object of text range of text frame of currShape
set content of myFind to ""
clear formatting myFind
set underline of font object of myFind to true
clear formatting replacement of myFind
set content of myFind to ""
set italic of font object of replacement of myFind to true
set underline of font object of replacement of myFind to false
execute find myFind replace replace all
end if
end repeat
end repeat
end repeat

(* Footnotes *)
-- Create list of all footnotes in active document
set lstFtNotes to every footnote of active document
-- Loop through all footnotes
repeat with currFtnote in lstFtNotes
set myFind to find object of text object of currFtnote
set content of myFind to ""
clear formatting myFind
set underline of font object of myFind to true
clear formatting replacement of myFind
set content of myFind to ""
set italic of font object of replacement of myFind to true
set underline of font object of replacement of myFind to false
execute find myFind replace replace all
end repeat

(* Endnotes *)
-- Create list of all endnotes in active document
set lstEndNotes to every endnote of active document
-- Loop through all endnotes
repeat with currEndnote in lstEndNotes
set myFind to find object of text object of currEndnote
set content of myFind to ""
clear formatting myFind
set underline of font object of myFind to true
clear formatting replacement of myFind
set content of myFind to ""
set italic of font object of replacement of myFind to true
set underline of font object of replacement of myFind to false
execute find myFind replace replace all
end repeat

(* Shapes *)
-- Create list of all shapes in main document
set lstShapes to (every shape of active document)
-- Loop through all shapes in main document
repeat with currShape in lstShapes
set theTextFrame to (text frame of currShape)
if has text of (text frame of
 
T

teacherpoet

Hmm . . . this keeps cutting off the tail end of my posts. The only part of your script that I changed was the first section, so you have that; I'll leave it to Mac BU to figure out how to post this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top