3

我想在OS X(10.11)中使用新的JavaScript Automation feature来编写不提供字典的应用程序。我有一个AppleScript,使用原始Apple事件与该应用程序交互,如下所示:从OS X(El Capitan)上的JavaScript发送和接收“原始”Apple事件

tell application "Bookends" 
    return «event ToySSQLS» "authors REGEX 'Johnson' " 
end tell 

现在我的问题是:如何将其翻译为JavaScript?我无法找到有关Javascript OSA API发送和接收原始Apple事件的任何信息。

一种可能的解决方法可能是call a piece of AppleScript through the shell,但我更愿意使用“真实”API。

回答

1

您至少可以在几个辅助函数使用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%'") 

更加充实的例子在这里:JavaScript wrappers for Bookends functions

相关问题