我正在OS X Yosemite上使用JavaScript for Automation。
我正在尝试在终端应用程序中打开一个新选项卡。这是我到目前为止所得到的:
var Terminal = Application('Terminal);
var Tab = Terminal.Tab;
// Activate the Terminal App, creates a new window if there isn't one already
Terminal.activate();
// This contains all the windows
Terminal.windows;
// This contains the first window
Terminal.windows.at(0) // alternatively, Terminal.windows[0]
// This contains the tabs in the first window
Terminal.windows.at(0).tabs
Terminal.windows.at(0).tabs
本质上是一个数组。它有一个.push
方法。我假设我可以使用以下语句向窗口添加选项卡:
Terminal.windows.at(0).tabs.push(new Tab());
但它引发了一个非常普遍的错误:
Error -10000: AppleEvent handler failed.
文档严重缺乏,我认为这个用于自动化的JavaScript只是让JavaScript开发人员加入的一个噱头。
注意:我已经看到AppleScript解决方案基本上只是告诉System Events
应用程序按Command + T打开一个新选项卡。感觉非常hacky,并使Command + T硬编码。
答案 0 :(得分:1)
以下代码适用于chrome
和safari
,但不适用于terminal
,我仍在查明原因,看看此信息是否有帮助。
chrome = Application("Google Chrome")
newTab = chrome.Tab()
chrome.windows[0].tabs.push(newTab)
答案 1 :(得分:1)
查看您的案例中是否有以下工作:
var system = Application('System Events');
var terminal = Application('Terminal');
// tell application "Terminal" to activate
terminal.activate();
// tell application "System Events" to tell process "Terminal" to keystroke "t" using command down
system.keystroke('t', {using: 'command down'});
答案 2 :(得分:0)
您可以模拟一个新标签的快捷方式。还需要声明目标选项卡
tell application "System Events" to keystroke "t" using {command down}
查看包含两个或更多标签的示例
teel application "Terminal"
do script "cd ~/ && ls" in tab 1 of front window
tell application "System Events" to keystroke "t" using {command down}
do script "cd ~/Applications && ls" in tab 2 of front window
end tell