Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@tjluoma tjluoma commented May 28, 2014

Thanks @RobTrew! I like that even better, and it incorporates another suggestion from https://twitter.com/ComplexPoint/status/471715614965657601

@tjluoma

This comment has been minimized.

Copy link
Owner Author

@tjluoma 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

This comment has been minimized.

Copy link

@RobTrew 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

This comment has been minimized.

Copy link

@RobTrew 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
You can’t perform that action at this time.