我正在尝试使用OS (10.11)中的新JavaScript Automation feature来编写一个不提供字典的应用程序脚本。我有一个使用原始苹果事件与该应用程序交互的AppleScript,如下所示:
tell application "Bookends"
return «event ToySSQLS» "authors REGEX 'Johnson' "
end tell现在我的问题是:如何将其转换为JavaScript?我找不到关于发送和接收原始Apple事件的Javascript OSA API的任何信息。
一种可能的解决方法是使用call a piece of AppleScript through the shell,但我更喜欢使用“真正的”API。
发布于 2016-07-03 05:30:41
通过在几个帮助器函数中使用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%'")https://stackoverflow.com/questions/36728791
复制相似问题