1

有没有人推断出成功从文件/字符串加载 XML 并允许访问 OS X Yosemite (10.10) Javascript for Automation 中的数据的语法?

文档和代码示例仍然很薄(截至 2014 年 11 月),我的归纳技能目前在三种不同的读取 XML (OPML) 文件的方法上已经枯竭:

  1. 最有希望的:$.NSXMLDocument

以各种方式获取字符串数据进展顺利,

function readTextFromFile(strPath) {
    return $.NSString.stringWithContentsOfFile(strPath);
}
function readDataFromFile(strPath) {
    return $.NSData.alloc.initWithContentsOfFile(strPath);
}
function filePath(strPath) { return Path(strPath); }

但是,关于这个主题的排列并没有取得成果:

var strPath='/Users/houthakker/Desktop/notes-2014-11-04.opml',
    dataXML = readData(strPath),
    strXML = readTextFile(strPath),
    oXMLDoc1, oXMLDoc2;

oXMLDoc1 = $.NSXMLDocument.alloc.initWithXMLString(strXML,0,null);
oXMLDoc2 = $.NSXMLDocument.alloc.initWithData(dataXML,0,null);

(“函数未定义”错误消息表明这两个初始化函数可能不会公开,但initWithRootElement()似乎确实如此)

  1. 最大进步:$.NSXMLParser

    var oParser = $.NSXMLParser.alloc.initWithData(dataXML);

    return oParser.parse; //true

但是事件驱动的解析似乎需要一些更复杂的东西,这些对我来说仍然是不透明的,并且可能不符合我的简单需求(读取和转换大小适中的本地 OPML 文件)。

  1. 最熟悉的:Application("System Events")

在 Applescript 中,这可以通过系统事件代码来完成:

set fOPML to (POSIX file "/Users/houthakker/Desktop/notes-2014-11-04.opml" as alias) as string
tell application "System Events"
    tell XML file fOPML
    -- now access XML Elements by name or index
end tell

但我还没有找到一个成功的 javascript 习惯用法,用于使用 unix Path()、字符串或冒号分隔的 mac 路径字符串的任何排列来初始化 XMLFile 对象。

这里有什么想法或更成功的经验吗?

4

1 回答 1

2

事实证明,这适用于(执行速度非常慢的)Application("System Events")路线:

var app = Application("System Events"),
    strPath = '~/Desktop/summarise.opml';

var oFile = app.xmlFiles[strPath],
    oRoot = oFile.xmlElements[0],
    oHead = oRoot.xmlElements.head,
    oBody = oRoot.xmlElements.body,
    lstOutlineXML = oBody.xmlElements.whose({
        name: 'outline'
    });

根据 JSExport 约定,从 XML 字符串初始化 NSXMLDocument 的函数是(其中每个“:”后面的字母大写,然后删除“:”).initWithXMLStringOptionsError()

因此,要选择一个本地 OPML 文件并将其解析为一个简单的 JSON 大纲:

function run() {
    var app = Application.currentApplication();
    app.includeStandardAdditions = true;

    function readTextFromFile(strPath) {
        return $.NSString.stringWithContentsOfFile(strPath);
    }

    var strPath = (
            app.chooseFile({withPrompt: "Choose OPML file:"})
        ).toString(), // Path → String
        strXML = strPath ? readTextFromFile(strPath) : '';

    if (!strXML) return;

    var oXMLDoc1 = $.NSXMLDocument.alloc.initWithXMLStringOptionsError(strXML, 0, null),
        oRoot = oXMLDoc1.rootElement,
        oBody = ObjC.unwrap(oRoot.children)[1],
        lstOutline =  ObjC.unwrap(oBody.children),
        lstParse, strJSON;

    function parseOPML(lst) {
        var lstParse=[], oNode, dctNode, lstChiln;

        for (var i = 0, lng=lst.length; i<lng; i++) {
            oNode = lst[i];
            dctNode = {};
            dctNode.txt = ObjC.unwrap(oNode.attributeForName('text').stringValue);
            lstChiln = ObjC.unwrap(oNode.children);
            if (lstChiln && lstChiln.length)
                dctNode.chiln = parseOPML(lstChiln);
            lstParse.push(dctNode);
        }
        return lstParse;
    }

    lstParse = parseOPML(lstOutline);
    strJSON = JSON.stringify(lstParse,null, 2);
    app.setTheClipboardTo(strJSON);
    return strJSON;
}
于 2015-01-13T20:53:04.913 回答