次の方法で共有


Scripting Word

Now, this is weird. My more recent tasks have involved working with both
the Word testers and the automated testing group to iron out some ways that we
can improve on testing. In particular, now that Word has a more complete
AppleScript implementation than previous versions, we can move from automated
testing based on VBA (Visual Basic for Applications) to automated testing based
on AppleScript.

Up until now, I’ve dabbled in AppleScript, but as someone accustomed to more
conventional programming languages, I’ve found AppleScript to be a wee bit
inscrutable. It’s unfortunate that there really is no standard implementation
of, say, the Text suite such that one way of doing things with, say BBEdit,
might also work with something else, like CodeWarrior’s IDE. Alas, AppleScript
dictionaries don’t quite measure up to VBA’s object browser.

This is particularly difficult for me, because I have some preconceived
notions of how a text editor should be automated. More than simply the fact
that I’ve worked on Word for a few years, I was one of the team members who
implemented the original object model for Word’s implementation of VBA. I did
all of the table-based objects, and the paragraph formatting object (including
some work with styles). I also did a good deal of work on the underlying
plumbing that routes certain methods and properties through Word’s normal
command dispatch mechanism (i.e. the code that runs when you do something like
select “Font” on the “Format” menu).

Anyway, when it comes to AppleScript, I’m definitely in learning mode. So,
I’ve been googling a bit about AppleScript, and what do I run into but John
Welch’s very
excellent piece about
AppleScript and KeyChain, which happens to begin with
a link to my
Anatomy of a Software Bug. And, yes, John, Paul Berkowitz is a genius.
There’s a reason Matt Neuburg devotes almost an entire paragraph of his
acknowledgements in AppleScript: the Definitive Guide to Paul.
(And, no, Paul, I haven’t forgotten the question you asked about typography in
Word and the possibilities for improvements. I’m wrestling with just how much
I can talk about at this stage of the game. As my father would say, when in
doubt, mumble.)

But the genius I want to call out today is Jim Murphy. While the rest of us
were busy enhancing our applications from the user’s point of view, Jim, almost
single-handedly, put together the AppleScript implementations for Word, Excel
and PowerPoint. I’ve spent the past couple weeks sanding off some of the rough
edges (particularly the Style object), but the sandpaper I’m using is very
fine-grained due in no small part to just an incredibly outstanding job that
Jim did on this enormous effort. While I shan’t fail to acknowledge the role
Paul Berkowitz played in helping define how the implementation should behave,
for which AppleScript programmers around the world owe an eternal debt of
gratitude, Jim is the person who made it happen.

If you’re doing anything having to do with trying to script Word 2004, I
want to hear from you. You can add comments here, but you can also get in
touch with me by clicking on the “Contact” link in the “My Links” list in the
left-hand column of the web page that has this article. I’ll also be attending
WWDC this coming week, and you’ll probably find me in some of the scripting
sessions.

I want to hear both the problems and issues you’ve encountered as well as
some of the successes you’ve had. I want to know both the things you’ve found
difficult and the things you’ve found to be easy.

 

Rick

Comments

  • Anonymous
    June 24, 2004
    AppleScript is different because of its English like syntax. But as with anything, you get used to it.

    I'm waiting to get my Office upgrade (the Pro update is delayed), so I can't have a play with the new AppleScript implementation. One thing I'd love to see is some online info about AppleScript in Office. For example, making the application dictionaries available online in a xhtml/PDF/.doc form, and maybe getting Paul to write a few examples for each dictionary to kick start things.

    I went Googling last night and could only find scant mention in MS press releases.

    Cheers.

  • Anonymous
    June 24, 2004
    The comment has been removed

  • Anonymous
    June 25, 2004
    There technically is. But as you've seen, no one really follows it.

    john

  • Anonymous
    June 25, 2004
    True Im a sun solaris engineer and shell script prog, most of what I do baffles the mcse's. What you use most always seems better.

  • Anonymous
    June 25, 2004
    Hi Rick

    I'll wait for a magazine CD to try the Office 2004 demo. I'm using a 56K dialup connection so the 186MB/16 hours is too long. (This was reason for seeking out the documentation.)

    Thanks.

    Dale

  • Anonymous
    June 25, 2004
    The comment has been removed

  • Anonymous
    June 26, 2004
    The comment has been removed

  • Anonymous
    June 26, 2004
    Follow-up: an example of how a more readable Englisg style could have been applied. Instead of that fearsome:

    set myTable to convert to table myRange separator separate by tabs table format table format simple2 with auto fit, apply heading rows, apply borders and apply font without apply first column

    I quoted (note that only 'myTable' and 'myRange' are variables here; everything else is a parameter of enumerated value for the 'convert to table' command), a more user-friendly terminology making use of propositions for parameters and gerunds ('...ing') for enumerations that follow them might be:

    set myTable to convert to table myRange separating by tabs using table format simple2 with auto fit, applying heading rows, applying borders and applying font without applying first column

    That reads somewhat more like AppleScript is meant to. The underlying 4-character raw codes that has refers to would be exactly the same as they are now (Murf chose unique numbers for most of these), but the "surface" text would be more readable. It's no easier, or different, to write code this way than with the existing terms. But it is easier to read and maintain since it makes the AppleScript mostly self-commenting.

  • Anonymous
    June 26, 2004
    > But it is easier to read and maintain since it makes the AppleScript mostly self-commenting.

    Yeah. I think the key to picking good 'English' terminology is good 'guessability'. And the closer the written command is to how you'd describe it verbally, the easier it is for the user to guess how to write it.

    BTW, not seen the Word 2004 dictionary myself, but just wondering if it really needs all the 'applying's, or would "...with auto fit, heading rows, borders and font without first column" be sufficient? Also, just mildly curious why the 'separating by' parameter takes an enumeration rather than a string?

    FWIW, while application terminology should be phrased to suit AppleScript (i.e. according to the SIG rules), seeing how commands look written in other languages might make for an interesting comparison, so here's how Paul's version would read in Python (assuming I've understood it correctly:):

    <code>
    myTable = myRange.convert_to_table(
    separating_by=k.tabs,
    using_table_format=k.simple2,
    auto_fit=True,
    applying_heading_rows=True,
    applying_borders=True,
    applying_font=True,
    applying_first_column=False
    )
    </code>

  • Anonymous
    June 30, 2004
    We don't have 2004 yet in this office (we will soon), but I've been wanting to do the following with Word scripts for a while... but have never figured out how to do any of them... I don't care if it is applescript or something else...

    1. To import an itunes playlist and format it to my preference (I have the playlists in a binder that I can look up)...

    2. To format page breaks in a screenplay... Basically it would look at the paragraph breaks at the bottom of each page. If a certain style of paragraph was broken up (ie dialog or scene description) and more than X number of lines long it would be broken and "(more...)" would be added to the bottom of the page. The top of the next page would say "(continued...)". There are lots of other factors to be considered, but this is the gist of what is going on... looking at page breaks, considering the styles of the paragraphs and either breaking or not breaking and formating properly.

    3. To use a batch of selected address book names in form letters...

    4. To take a batch of images selected in the finder and insert them into specific documents.

    5. To batch copy text an images from a word file into my blog...

    to name a few

  • Anonymous
    July 12, 2004
    I started to play around with scripting today -- I want to convert all the footnotes in a Gutenberg document into real Word footnotes, selecting all text between [ ] brackets and making into a new note. So far am completely stymied as Word tells me it "cannot make a new footnote" no matter how I try requesting things. My script compiles, but doesn't run. But, I live in hope.

  • Anonymous
    July 12, 2004
    It's tricky learning how to deal with the object model, especially things like text ranges, and footnotes all of whose properties appear to be [r/o] (read-only). But those properties may contain properties which are not read-only! Here's how to make a new footnote at the cursor location:

    tell application "Microsoft Word"
    set oDoc to active document
    set f to make new footnote at oDoc
    set content of text object of f to "Here's my first footnote."
    end tell

    It's the 'content' property of text ranges (text object of footnote is a text range) which is what you usually want to set, and it's always settable.

    It's harder to figure out how to specify the location for the footnote if it's not to be at the cursor. AppleScript dictionaries don't tell you to which classes your class under investigation ('footnote' here) is an element. 'document' is obviously one such. But take a look at 'text range' itself. 'text range' can have footnote elements. And every sub-unit of a document - or most of them - has a 'text object' property of its own. So you could make a footnote at the end of the first paragraph of a document, no matter here the cursor might be, like this:

    tell application "Microsoft Word"
    set oDoc to active document
    set t to text object of paragraph 1 of oDoc
    set f to make new footnote at t
    set content of text object of f to "Here's another footnote."
    end tell

    And you'll see a new footnote number at the end of the first paragraph with your new footnote at the bottom of the page. Word is very clever about re-indexing the footnotes. If this new footnote is actually earlier in the document than the other one you made, this new one becomes footnote 1 and the other one becomes footnote 2. If you get the 'entry index' (definitely r/o) property of the footnote you can find that out too.

    'note reference' property is a bit peculiar. It too is a text range, but if you get its content the result is "". I think that's because footnote references are set to AutoNumber by default and these are considered to have no content, just an index. If you set the content of note reference of f to "•" for example, it does replace the number by this character but suddenly the footnote disappears. 'footnote options' lets you set the footnote numbering style, and that includes a 'note number style symbol' type. When I have more time I'll look into that. Usually you'll want numbers, in some style, for footnotes so probably the note reference remains "".

  • Anonymous
    May 29, 2009
    PingBack from http://paidsurveyshub.info/story.php?title=buggin-my-life-away-scripting-word

  • Anonymous
    June 13, 2009
    PingBack from http://wheelbarrowstyle.info/story.php?id=1121

  • Anonymous
    June 15, 2009
    PingBack from http://unemploymentofficeresource.info/story.php?id=6125

  • Anonymous
    June 16, 2009
    PingBack from http://fixmycrediteasily.info/story.php?id=5444

  • Anonymous
    June 18, 2009
    PingBack from http://barstoolsite.info/story.php?id=5652

  • Anonymous
    June 19, 2009
    PingBack from http://edebtsettlementprogram.info/story.php?id=22978

  • Anonymous
    June 19, 2009
    PingBack from http://debtsolutionsnow.info/story.php?id=3523