在macOS上,osascript
命令行工具可用于运行AppleScript和JavaScript,两者都可以访问Apple Events API。
对于大多数API,翻译非常简单。 E.g。
tell application "Terminal" to quit
转换为:
Application("Terminal").quit()
此外,在JS中有一个commandsOfClass
函数列出了可用的操作:
osascript -l JavaScript -e 'Application("Terminal").commandsOfClass()'
# close, count, delete, doScript, duplicate, exists,
# getURL, make, move, open, print, quit, save
osascript -l JavaScript -e 'Application("System Events").commandsOfClass()'
# abortTransaction, attachActionTo, attachedScripts, beginTransaction, cancel, click,
# close, confirm, connect, count, decrement, delete, disconnect, doFolderAction,
# doScript, duplicate, editActionOf, enable, endTransaction, exists, increment,
# keyCode, keyDown, keyUp, keystroke, logOut, make, move, open, perform, pick,
# print, quit, removeActionFrom, restart, save, select, shutDown, sleep, start, stop
但AppleScript有一些更加模糊的事件,特别是aevtrlgo
(“真正注销”)事件:
tell application "loginwindow" to «event aevtrlgo»
我甚至无法在终端中输入«»
个字符,所以这里是一个可以复制粘贴的版本:
printf 'tell application "loginwindow" to \xc2\xabevent aevtrlgo\xc2\xbb' | osascript
现在,我如何在JavaScript中执行此操作«event aevtrlgo»
?
我尝试过这样的事情:
Application("loginwindow").rlgo();
Application("loginwindow").aevtrlgo();
Application("loginwindow").reallyLogOut();
Application("loginwindow").event("rlgo");
Application("loginwindow").event("aevtrlgo");
Application("loginwindow")["«event aevtrlgo»"]();
但我总是得到完全相同的答案:
execution error: Error on line 1: Error: Message not understood. (-1708)
Plus Application("loginwindow").commandsOfClass()
会产生一个空列表。
答案 0 :(得分:1)
我的标准建议:坚持AppleScript,因为它是唯一官方支持的选项,知道如何正确地说Apple活动。
答案 1 :(得分:0)
这个解决方法适用于我,但只能在脚本编辑器中工作(据我所知):
var app = Application.currentApplication();
app.includeStandardAdditions = true;
app.doShellScript('osascript -e "tell application \\"loginwindow\\" to «event aevtrlgo»"');