首页 > 解决方案 > AppleScript,在解析大型 XML 文件时收到“系统事件出错:连接无效。(-609)”

问题描述

我创建了一个 AppleScript 来解析 MS Word XML 文件,以便生成一个制表符分隔的文档,以便在 Excel 中打开。一切都适用于中小型 XML 文件。但是,在解析较大的文件时,我收到错误消息“系统事件出错:连接无效。(-609)”。下面是发生错误的告诉块:

tell application "System Events"
    activate
    with timeout of 0 seconds
        set quit delay to 0
        set theXMLFile to XML file theDocument
        set theRecords to XML elements of XML element "wx:sect" of XML element "w:body" of XML element "w:wordDocument" of theXMLFile whose name is "w:p"
    end timeout
end tell

我尝试调整超时和退出延迟参数,包括将它们注释掉,但即使将它们设置为 0,我仍然会收到错误消息。我测试了脚本以确保它通过了“设置 XMLFile.xml”。 ..” 行,但没有使其通过“set theRecords ...”行。

为了确认 XML 文件没有问题,我手动将文档拆分为两个较小的文件。这些文件中的每一个都通过脚本处理得很好。这使我相信错误是执行 theRecords 变量的大小或由于解析文件所需的时间而导致的某种超时。

非常感谢解决或解决此错误的任何帮助。谢谢你。

标签: applescript

解决方案


正如我所检查的,问题是使用who子句的属性不是 XML 元素的直接属性。您可以在脚本调试器中检查这一点。以下适用于大型 XML 文件。至少对我来说它有效:

-- set theDocument to "HARD_DISK:Users:123:Desktop:TEST files:SampleLarge.xml"
set aList to {}

tell application "System Events"
    activate
    tell (contents of XML file theDocument) to tell (XML element "w:wordDocument")
        tell (XML element "wx:sect" of XML element "w:body")
            set XMLElements to XML elements
            set XMLElementNames to name of XML elements
            repeat with i from 1 to count XMLElementNames
                if (item i of XMLElementNames) is "w:p" then
                    set end of my aList to item i of my XMLElements
                end if
            end repeat
        end tell
    end tell
end tell

return aList

推荐阅读