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.
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
On 4/6/08 11:06 AM, in article ee97a...@webcrossing.caR9absDaxw,
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.
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.
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.
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
On 4/6/08 12:45 PM, in article ee97...@webcrossing.caR9absDaxw,
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.
But still can't figure out what I would change to get this to substitute italic for underline throughout a document.
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
> 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 |:>)
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.
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.
Regards |:>)
Bob Jones
[MVP] Office:Mac
On 4/6/08 4:38 PM, in article uTzGpYCm...@TK2MSFTNGP03.phx.gbl, "Daiya
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.
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.)
It's a start, for sure.
> 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.
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.
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.
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
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)