Ms word 为什么不是';t设置变量,除非脚本在脚本调试器中运行';s步骤调试器?

Ms word 为什么不是';t设置变量,除非脚本在脚本调试器中运行';s步骤调试器?,ms-word,applescript,Ms Word,Applescript,当我使用脚本调试器一次一行地遍历每一行时,下面的AppleScript可以正常工作,但是当它获得另存为行时,会报告\u doc变量有一个缺失值 use AppleScript version "2.4" -- Yosemite (10.10) or later use scripting additions tell application "Finder" set _folder to choose folder set _files to files of _folder

当我使用脚本调试器一次一行地遍历每一行时,下面的AppleScript可以正常工作,但是当它获得
另存为
行时,会报告
\u doc
变量有一个
缺失值

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Finder"
    set _folder to choose folder
    set _files to files of _folder
    repeat with _file in _files
        if creator type of _file is "MSWD" then
            tell application "Microsoft Word"
                open _file
                set _doc to document of window 1
                save as _doc file format format text
                close _doc
            end tell
        end if
    end repeat
end tell
我尝试过使用
delay 5
暂停5秒钟,但行为没有改变。为什么会发生这种情况?我能做些什么?

对于“为什么会发生这种情况”的答案似乎是“这可能是一个时间问题”和“因为在Mac上自动从AppleScript和VBA中提取Word时,有许多问题Microsoft尚未解决”。我认为除了通过Smiley机制或word.uservoice.com向微软报告外,你对此无能为力。在uservoice上,最好添加您的投票,而不是现有的请求(如果有)。但是,没有理由相信微软现在甚至会承认或解决相当严重的自动化问题

我没有遇到您甚至无法成功地将文档设置为窗口1的文档的问题。我总是能够使用

set _doc to open _file
在这里,我发现“delay 5”足以解决您报告的问题,但也存在一个问题,即“\u doc”变量在另存为后变得无效。我有一个在窗口中迭代的解决方案,所以我把这个脚本放在一起 A.应尽可能减少延误 B在这里可以处理简单的测试数据,但需要改进,特别是在错误检查方面

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Finder"
    set _folder to choose folder
    set _files to files of _folder
    repeat with _file in _files
        if creator type of _file is "MSWD" then

            tell application "Microsoft Word"
                --activate
                set doc_count to (count of documents)
                open _file

                -- You have to set the maximum no. of repeats
                -- high enough for your system
                set repeats to 50
                repeat until (count of documents) > doc_count or repeats = 0
                    set repeats to repeats - 1
                end repeat
                if (count of documents) > doc_count then
                    set _doc to (document (doc_count + 1))
                    set _windows to the windows
                    repeat with _window in _windows
                        if the full name of the document of _window is the full name of _doc then

                            set _windowIndex to the entry_index of _window
                            exit repeat
                        end if
                    end repeat
                    -- you need to create a new file name for each file.
                    -- this is a temporary kludge
                    set _textfilename to (posix full name of _doc) & ".txt"
                    save as _doc file name _textfilename file format format text
                    -- _doc now invalid, we need to "reconnect"                 
                    set _windows to the windows
                    repeat with _window in _windows
                        if the entry_index of _window is _windowIndex then
                            set _doc to the document of _window
                            exit repeat
                        end if
                    end repeat
                    close _doc saving no
                else
                    -- you can make this more informative, and you might still need to
                    -- try to close something.
                    display dialog "Could not open document: " & POSIX path of _file
                end if
            end tell
        end if
    end repeat
end tell

顺便说一句,当我使用OpenRecentFile对单个文档进行测试时,获取文档的引用从来没有出现过任何问题。但这对你想做的事情没用。

我走了另一条路。因为我在VirtualBox下有Windows,所以我在那里编写了一个VBA脚本来处理转换。