Skip to content

Instantly share code, notes, and snippets.

@tjluoma
Last active January 6, 2023 21:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjluoma/ca9bcbe0d905a759442f to your computer and use it in GitHub Desktop.
Save tjluoma/ca9bcbe0d905a759442f to your computer and use it in GitHub Desktop.
I use this with a Keyboard Maestro macro that runs whenever MSWord “deactivates” (in Keyboard Maestro terms). Updated with comments from http://apple.stackexchange.com/a/132171/9226
on hasDocument()
tell application "Microsoft Word"
every document is not missing value
end tell
end hasDocument
tell application "Microsoft Word"
if it is running and my hasDocument() then
save active document
end if
end tell
@tjluoma
Copy link
Author

tjluoma commented May 28, 2014

Hrm for some reason I can’t see RobTrew’s comment anymore.

Fortunately I saved it. He suggested:

tell application "Microsoft Word"
    if running then
        if documents is not missing value then
            tell active document
                if (not saved and path contains ":") then save
            end tell
        end if
    end if
end tell

@RobTrew
Copy link

RobTrew commented May 28, 2014

And in case you wanted to log the saves, you could try a pattern something like this:

SaveMSDoc()

on SaveMSDoc()
    set strReturn to "Nothing to save ..."
    tell application "Microsoft Word"
        if running then
            set lstDocs to documents
            if lstDocs is not missing value then
                set oDoc to active document
                if oDoc is missing value then set oDoc to item 1 of lstDocs
                tell oDoc
                    set strPath to path
                    if (not saved and strPath contains ":") then
                        save
                        set strReturn to ((current date) as string) & tab & "saved " & strPath
                    end if
                end tell
            end if
        end if
    end tell
    return strReturn
end SaveMSDoc

@RobTrew
Copy link

RobTrew commented May 28, 2014

Hi TJ - I did delete the first version :-) it didn't take account of the fact that .active_document can return missing value even when the document list is not empty ...

罗斌洲

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment