“发送和接收”;生的;OS X上JavaScript的苹果事件(El Capitan)

“发送和接收”;生的;OS X上JavaScript的苹果事件(El Capitan),javascript,applescript,osx-elcapitan,javascript-automation,Javascript,Applescript,Osx Elcapitan,Javascript Automation,我试图利用OSX(10.11)中的新功能编写一个不提供字典的应用程序。我有一个AppleScript,它使用原始Apple事件与该应用程序交互,如下所示: tell application "Bookends" return «event ToySSQLS» "authors REGEX 'Johnson' " end tell 现在我的问题是:如何将其转换为JavaScript?我找不到有关Javascript OSA API的任何信息,无法发送和接收原始Apple事件 一种可能的解决方

我试图利用OSX(10.11)中的新功能编写一个不提供字典的应用程序。我有一个AppleScript,它使用原始Apple事件与该应用程序交互,如下所示:

tell application "Bookends"
  return «event ToySSQLS» "authors REGEX 'Johnson' "
end tell
现在我的问题是:如何将其转换为JavaScript?我找不到有关Javascript OSA API的任何信息,无法发送和接收原始Apple事件


一种可能的解决方法可能是,但我更喜欢使用“真正的”API。

通过在两个助手函数中使用OSAKit,您至少可以比shell脚本调用更快地执行某些操作:

// evalOSA :: String -> String -> IO String
function evalOSA(strLang, strCode) {

    var oScript = ($.OSAScript || (
            ObjC.import('OSAKit'),
            $.OSAScript))
        .alloc.initWithSourceLanguage(
            strCode, $.OSALanguage.languageForName(strLang)
        ),
        error = $(),
        blnCompiled = oScript.compileAndReturnError(error),
        oDesc = blnCompiled ? (
            oScript.executeAndReturnError(error)
        ) : undefined;

    return oDesc ? (
        oDesc.stringValue.js
    ) : error.js.NSLocalizedDescription.js;
}

// eventCode :: String -> String
function eventCode(strCode) {
    return 'tell application "Bookends" to «event ToyS' +
        strCode + '»';
}
这样您就可以编写如下函数:

// sqlMatchIDs :: String -> [String]
function sqlMatchIDs(strClause) {
    // SELECT clause without the leading SELECT keyword
    var strResult = evalOSA(
        '', eventCode('SQLS') +
        ' "' + strClause + '"'
    );

    return strResult.indexOf('\r') !== -1 ? (
        strResult.split('\r')
    ) : (strResult ? [strResult] : []);
}
打电话来像

sqlMatchIDs("authors like '%Harrington%'")
这里有更完整的示例集: