Script to find/replace formatting

T

teacherpoet

Version: 2008
Operating System: Mac OS X 10.4 (Tiger)
Processor: intel

I need an applescript that will let me find and replace specific formatting within one or more documents. For example, in some I want to find all single underlining and replace that with italics.

Presumably such a script could then get modified easily so that you could substitute plain text for boldface, one font for another, etc. That would make it useful for a wide variety of purposes.

I hope this is the right place for such a query. If not, let me know.
 
C

CyberTaz

You really don't need a *script* for this... Word's Find & Replace feature
will handle just about anything you throw at it. When you open the F&R
dialog click the small button marked with a triangle in the lower left
corner to expand the dialog.Select the formatting you want to Find, select
the formatting you want to Replace it with & let it rip.

However, If your formatting were based on Style in the first place it would
be even easier - you'd just have to change the Style's definition.

HTH |:>)
Bob Jones
[MVP] Office:Mac
 
T

teacherpoet

Good to know that I can do this via F&R without a script. However, I can't actually seem to achieve this. I followed your instructions, expanded the F&R screen. And then . . . ?

I tried the following:
1. Clicked on No Formatting (bottom left) to gray that out.
2. Clicked on Format>Font right next to it.
3. Clicked into the Find field.
4. Selected the font of my document and Underline>Words only. Got a message underneath that field indicating my choice. Clicked Okay.
5. Clicked into the Replace field.
6. Clicked on No Formatting (bottom left) to gray that out.
7. Selected the font of my document and Italic only. Got a message underneath that field indicating my choice. Clicked Okay.
8. Tried Find Next, Replace, and Replace All with no result.

I tried substituting Cmd+U and Cmd+I, and also underline and italic, in that field, with no result.

So "Select the formatting you want to Find, select the formatting you want to Replace it with & let it rip" doesn't exactly do the trick.

These underlinings are not style-based. They go back to the day when I used that to indicate italics to proofreaders, when I submitted printouts as hard copy, before editors accepted digital files.
 
D

Daiya Mitchell

Fun question. Just by the way/for the archives, there is a built-in
Automator action to save and repeat a Find and Replace in Word 2008, but
it only lets you act on text, not formatting (automator actions not
included in Home and Student version).

CyberTaz is right that this is only worth messing with if you need to be
running the same script over and over again (and then some more), but I
had already written this, so I'm posting it. Also, the F&R dialog will
let you act on all open documents, no clue how to script that.

You know AppleScript, right? I don't, I'm just experimenting. Download
the Word 2004 AppleScript Reference from here. There's a sample script
on page 156 under "Class: Replacement" which can be tweaked to add new
formatting, not just clear found formatting.
http://www.microsoft.com/mac/developers/default.mspx

*HOWEVER*, Word documents, internally, are divided into ranges. That
script only works on the main range of the document. It does not run on
headers/footers or footnotes/endnotes, text boxes, etc. Making a Find
and Replace run on all ranges is discussed here.
http://tinyurl.com/5dvoqt

Free feel to post results/sample scripts back to the archives here for
the edification of all, if you do decide you need a script.
 
D

Daiya Mitchell

If you are not changing the font, there is no need to select anything
there. Try selecting only the Underline and Italic formats, while
ignoring the font.

Also, did you apply Words Only Underline to the document in the first
place? The default cmd-U uses the straight line underline, and if these
docs have been converted, that's probably the one that is applied. Try
the regular underline.
 
C

CyberTaz

Well, in addition to a slight modification to your procedure I suspect there
could be either or both of 2 other factors involved: Since these are
evidently "older" docs it's possible that the font may be a little different
(even though the name may be the same) and/or that the underlining may be
coded somewhat differently than you think. Both should be overcome if you
try it this way [based on your earlier post of wanting to replace single
underlining with italic];

1- When you go to F&R you're already in the Find What field so empty it of
any content & just click the No Formatting button to get a "clean" start.
Then click Format> Font, select the type of underlining (single or Words
Only) from the Underline Style list but make no other changes, OK. NOTE:
there seems to be a bit of a bug when you select Words Only, so if that
phrase doesn't appear *on* the list button when you select it, select it a
2nd time, then OK.)

2- Click in the Replace With field. Clear any content & click No Formatting,
then Format> Font, select Italic from the Font Style list, select None from
the Underline Style list, then OK.

How does that work?

You only need specify a font if you've used more than one font in the doc &
only want to replace TNR [whatever] with Arial [whatever/some other] for
example, but leave any instances of Garamond [whatever] untouched or if you
want to replace the font, itself, with a different font.

HTH |:>)
Bob Jones
[MVP] Office:Mac
 
T

teacherpoet

Thanks to both of you, CyberTaz and Daiya.

FYI, my test file has been a new one in which I underlined a few words.

Your basic suggestions worked. Here's what I did:

1. Open F&R, click No Formatting.
2. Place cursor in Find field, click Format>Font, choose straight line underline, click Okay.
3. Place cursor in Replace field, click Format>Font, choose Italic and Underline>None, click Okay.
4. Click Replace All.

Worked like a charm. I then tested it on one of the older documents I need to massage, and it worked fine there too.

However, I don't (yet) know Applescript -- just a newbie at that. Obviously best to script this, rather than go through all those steps for each affected document. And in some of these documents the underlining also appears in the headers and notes, so I'd want to have it work on all ranges.

I've looked at the script toward which you pointed me . . . and I wouldn't know where to start, alas. Also, it won't compile in Script Editor as is, even after I remove line breaks, etc.

Scripting may be for geeks, but it ain't for sissies.
 
T

teacherpoet

P.S. I see that I can run the basic F&R operation on All Open Documents, so that's probably the way I'll go -- find a cluster of them, open them, and do this all at once. But a script would make it much easier; as I found such a document, I'd run it and save the revised version.
 
T

teacherpoet

P.P.S. Okay -- I got that Replace Everywhere script to compile. Had to ferret out various line breaks that snuck in somehow.

But still can't figure out what I would change to get this to substitute italic for underline throughout a document.
 
D

Daiya Mitchell

P.P.S. Okay -- I got that Replace Everywhere script to compile. Had to ferret out various line breaks that snuck in somehow.

They come from copying. I think they get copied from the web in the
wrong format.
But still can't figure out what I would change to get this to substitute italic for underline throughout a document.

Did you download the Word 2004 Reference and look at the sample script
on p156? that's the one that helps with formatting. I'm fairly sure that
it would replace or be incorporated into last section of the Replace
Everywhere script that begins "on ReplaceInRange(findText, replaceText,
theRange)", but I didn't get very far with Replace Everywhere page,
because I didn't quite understand it.

This is as far as I got revising the sample script from the Word 2004
reference--this example replaces italic with underline. But as is, it
only runs on the main text range of the active document, no notes or
header/footers:

tell application "Microsoft Word"
set myFind to find object of text object of active document
clear formatting myFind
set italic 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 false
set underline of font object of replacement of myFind to true
set content of replacement of myFind to ""
execute find myFind replace replace all
end tell
 
C

CyberTaz

<snip>
And in some of these documents the underlining also appears in the headers and
notes, so I'd want to have it work on all ranges.
<snip>

I believe that if you select either Current Document All or All Open
Documents that it will work on H/F as well as body content.

Regards |:>)
Bob Jones
[MVP] Office:Mac
 
T

teacherpoet

The script you sent compiles fine, but doesn't change anything by itself when I run it.

I've looked at the script on p. 156 of the Dictionary. This seems exactly relevant to my situation. The problem: I don't understand where I would drop this into the Replace Everywhere script. If I understand correctly, I'd have to drop it into several spots to affect not just the main body of the text but also headers and footers. And I'd have to create a new TextRange section to handle notes.

Would I replace something already in the script with this material, and then tweak it to effect a substitution of italic for bold? Would I simply add it to the existing script at strategic points, then tweak it?

This set of instructions in the Dictionary reminds me of the road signage of New Jersey -- intended not for someone arriving for the first time but for those who already live there.
 
D

Daiya Mitchell

I just noticed that the Replace Everywhere script doesn't handle
Footnotes. Argh.

And I can't figure out how to incorporate my script into the last part
of Replace Everywhere anyhow. Let me know if you sort it--I can holler
for help tomorrow, but it may take a few days.
 
C

CyberTaz

Current Document All & All Open Documents catches footnotes, endnotes, etc.
as well:)... But I don't know how to translate that into AppleScript.

Regards |:>)
Bob Jones
[MVP] Office:Mac
 
D

Daiya Mitchell

The script you sent compiles fine, but doesn't change anything by itself when I run it.

Well, it isn't tweaked for your situation, it's actually reversed from
what you want. On my machine, it changes text in italics to have
underlines, no problem. Does your doc have text in italics? Test it on a
dummy doc. Mine is just a simple variation on the p156 script.
I've looked at the script on p. 156 of the Dictionary. This seems exactly relevant to my situation. The problem: I don't understand where I would drop this into the Replace Everywhere script. If I understand correctly, I'd have to drop it into several spots to affect not just the main body of the text but also headers and footers. And I'd have to create a new TextRange section to handle notes.

Keep in mind that I'm very novice at this, but as I understand it (and
I'm fairly confident about this bit): You don't have to drop it into
several spots. See the section at the end, where it starts "on
ReplaceInRange"? The ReplaceInRange bit is the code that specifies what
the actual replacement will be. Looking above in the script, everytime
you see "my ReplaceInRange", that just calls the script at the end and
runs it. And yes, it needs another section to go through the Footnotes
and call "my ReplaceInRange" for the notes.
Would I replace something already in the script with this material, and then tweak it to effect a substitution of italic for bold? Would I simply add it to the existing script at strategic points, then tweak it?

That's the tricky part. The ReplaceInRange subsection at the end needs
to be edited to do a formatting change, as it's currently set up for a
text change. The findText, replaceText variables are totally confusing
me, and I'm not sure what needs to be edited to make a tweaked
ReplaceInRange subsection interact smoothly with the ReplaceEverywhere
section. Trial and error thus far has not been fruitful, in my attempts
to integrate the formatting F&R script I sent with the ReplaceInRange
subsection.
This set of instructions in the Dictionary reminds me of the road signage of New Jersey -- intended not for someone arriving for the first time but for those who already live there.

Agreed. It's in the subsection of the site entitled Developers, you may
have noticed. I'll send out a cry for help to some more expert people,
but it will probably take a few days, if not more.
 
T

teacherpoet

If you can figure out how to make this Replace Everywhere script work (including footnotes/endnotes), or get some expert help with it, I'd much appreciate that. And then I think you'd have something worth posting for others to use -- just as I now use your excellent Set Window Size, View, and Zoom script, for which you have my deep gratitude.

If you added some instructions to this on how and where in the script to substitute various formatting options (bold, etc.), it would prove handy for numerous purposes, and not just for me. I can't be the only person who needs to perform such actions.

I'm not afraid of Applescript. I have tweaked some simple scripts myself -- including your Set Window Size, View, and Zoom. But those were comparatively simple scripts, and the tweaks were fairly obvious.

For example, I got Set Window Size, View, and Zoom (in one version) to give me a view of my document that fills 2/3 of my MacBook Pro screen, with text zoomed to 215 percent, in Print Layout, with the left edge flush left against the edge of the screen two toolbars in place at the top, and enough space at the bottom to let the Dock show. And a second version thereof that completely fills the screen of the MBP. Sweet. But this just required some trial-and-error play with the window size and zoom percentage.

One doesn't really need to "know Applescript" in order to achieve something like that. But a script like Replace Everywhere requires know-how to revise and custom-tailor, and short of reading the Dictionary straight through and/or taking an online course in scripting I consider it well past my present ability. As Dirty Harry says, "Man's got to know his limitations." Over time I hope to learn much more,

By the way, I didn't see the Automator F&R to which you referred a few posts back -- not in the cluster that came with Office 2008, anyhow. (And I have the Special Media Edition.)
 
T

teacherpoet

I fiddled with your script and got it to change underline to italic in the main body of the document. But it doesn't do headers/footers, and it doesn't do endnotes/footnotes.

It's a start, for sure.
 
D

Daiya Mitchell

I'll see what can be done. Glad the Window Size instructions are useful.
Once we sort this, I'll see what can be done about putting similar
samples/tutorial online.
But a script like Replace Everywhere requires know-how to revise and custom-tailor, and short of reading the Dictionary straight through and/or taking an online course in scripting I consider it well past my present ability. As Dirty Harry says, "Man's got to know his limitations." Over time I hope to learn much more,

I'm basically experimenting to try to learn more, which is why I leaped
on your question to mess about with it, but as I've been told by several
experts, trial and error is a bad way to learn AppleScript, and I really
need to start from the basics with a good instructional manual.
By the way, I didn't see the Automator F&R to which you referred a few posts back -- not in the cluster that came with Office 2008, anyhow. (And I have the Special Media Edition.)

Where did you look? In Word itself, there are just a few Sample
Automator Workflows. But if you open in Automator and select MS Word in
it, you'll see about 30 different actions you can tweak and assemble
into workflows. The F&R is there. However, in messing about, I noticed
that the Automator Action fails to search in all ranges, and only
operates in the main document. Making it fairly useless, and subject to
the same problem we have here.
 
D

Daiya Mitchell

No, I was just using it to make sure I was fiddling right and figure out
how the language worked. Setting specific formats to find and replace is
actually a pretty straightforward tweak.

I suspect you've already spent more time messing with the scripting than
it would take you to set up the F&R in the Word dialog over and over
again. :)
 
T

teacherpoet

"I suspect you've already spent more time messing with the scripting than
it would take you to set up the F&R in the Word dialog over and over
again."

Possibly true. But then I'll have to remember forever exactly how to do that in the Word F&R dialog. If we get it scripted, then every time I look at my script menu I'll see that I have this. And It's a one-step operation, whereas the F&R dialog is about 14 steps each time . . . okay for solving a single occurrence of a problem, but not for multiple occasions.

No rush on this. Whenever you and your accomplices can find the time.
 

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