除非在脚本调试器的步骤调试器中运行脚本,否则为什么不设置变量?

时间:2019-11-21 01:13:45

标签: ms-word applescript

我有以下AppleScript,当我使用脚本调试器一次浏览每一行时,它可以正常工作,但是报告_doc变量在获得{{1 }}行。

missing value

我尝试使用save as暂停长达5秒钟,但行为没有改变。为什么会发生这种情况,我该怎么办?

1 个答案:

答案 0 :(得分:1)

“为什么会发生”的答案似乎是“这可能是计时问题”和“因为在Mac上从AppleScript和VBA自动化Word时存在许多问题,而微软尚未解决” 。除了通过Smiley机制或通过word.uservoice.com向Microsoft报告以外,我认为您无能为力。在uservoice上,如果有现有请求,最好添加您的投票。但是根本没有理由相信微软现在甚至会承认或解决非常严重的自动化问题。

我还没有遇到无法甚至将_doc设置为窗口1的文档的问题。我一直可以使用

set _doc to open _file

在这里,我发现“延迟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

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