1

我在 OS X Yosemite 上玩 JavaScript 以实现自动化。

我正在尝试在终端应用程序中打开一个新选项卡。这是我到目前为止所得到的:

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 在那里硬编码。

4

3 回答 3

1

以下代码适用于chromeand safari,但不适用于terminal,我仍在找出原因,看看这个信息是否有帮助。

chrome = Application("Google Chrome")
newTab = chrome.Tab()
chrome.windows[0].tabs.push(newTab)
于 2015-01-13T14:39:14.680 回答
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'});
于 2015-09-19T03:31:25.080 回答
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
于 2016-02-28T01:59:02.437 回答