为什么不设置变量,除非脚本在 Script Debugger 的步进调试器中是 运行?

Why isn't a variable being set unless the script is run within Script Debugger's step debugger?

我有以下 AppleScript,当我使用 Script Debugger 逐行逐行执行时,它工作正常,但报告 _doc 变量在获取 save as行。

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 秒,但行为没有任何变化。为什么会发生这种情况,我该怎么办?

"why might this be happening" 的答案似乎是 "it probably is a timing problem" 和 "because there are a number of problems when automating Word from both AppleScript and VBA on Mac that Microsoft has not yet fixed"。我认为除了通过 Smiley 机制或通过 word.uservoice.com 向 Microsoft 报告外,您无能为力。在 uservoice 上,如果有的话,最好在现有请求上添加您的投票。但目前根本没有理由相信微软甚至会承认或解决相当严重的自动化问题。

我还没有遇到过你甚至无法将 _doc 设置为 window 1 文档的问题。我总是能够使用

set _doc to open _file

这里,我发现一个"delay 5"就可以解决你反映的问题,但也一直存在一个问题,就是“_doc”变量在另存为后就失效了。我有一个遍历 windows 的解决方案,所以将这个脚本放在一起 一种。应尽可能减少延迟 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

顺便说一句,当我使用 打开最近的文件 对单个文档进行测试时,获取对文档的引用从来没有任何问题。但这对您尝试做的事情毫无用处。