Hi all. I’m really new to Applescript having only really used languages like Autohotkey on Windows before. Im trying to write a script to change the styles of certain words in a document in Word 2011. I have tried using something similar to this http://macscripter.net/viewtopic.php?id=36451, but I haven’t managed to get it to work.
The kind of thing I am trying to do at the moment is change the words in a document like this:
Title1
Text text text
Title2
Text text text
Title3
Text text text
In the example above I would want to change Title1, Title2 and Title3 to the same style (Heading 1), whilst leaving the text as it is.
The code I have so far is below which is basically taken from the link above and doesn’t work:
tell application "Microsoft Word"
set myFind to find object of selection
tell myFind
clear formatting
set style of format to style heading1
execute find find text "Title1" replace with "Title1" replace replace all
end tell
end tell
If anyone could give me a hand that would be fantastic, or if there is a good resource for learning how to Applescript Word 2011 on Mac that would also be great. Thanks in advance. Tom.
Hi,
as far as I know it is not possible to use find patterns like “word begins with” with the find and replace function.
This script checks each selected paragraph if it starts with “Title” and then sets the style
tell application "Microsoft Word"
set theParagraphs to paragraphs of selection
repeat with aParagraph in theParagraphs
if (get content of text object of aParagraph) starts with "Title" then
set style of text object of aParagraph to style heading1
end if
end repeat
end tell
Hi, thanks that works for the example but maybe I was too vague. I meant apply heading1 to the different titles if they were completely different i.e.
Summary
Text text text
Report
Text text text
Conclusion
Text text text
That is really useful though (actually given me another idea for a script) and a big thanks. I’ll try to adapt it but if you or anyone else has a suggestion? Thanks again.
you must find a unique way to identify the title paragraphs
for example there is always an empty paragraph before a title paragraph
or you use a “whitelist”
property titleList : {"Summary", "Report", "Conclusion"}
tell application "Microsoft Word"
set theParagraphs to paragraphs of selection
repeat with aParagraph in theParagraphs
if (get content of text object of aParagraph) is in titleList then
set style of text object of aParagraph to style heading1
end if
end repeat
end tell
Ok, I’ll investigate that. The titles will always be the same, although not in the same location. I basically have a lot of similar documents that need formatting all the time.
The second script doesn’t seem to work, although the first one does work. I think I understand how it should work, but do I need to add something else to it? Thank you for all your help by the way, it is much appreciated.