【问题标题】:Open new Terminal Tab with OS X JavaScript for Automation使用 OS X JavaScript 为自动化打开新的终端选项卡
【发布时间】:2015-02-11 15:24:16
【问题描述】:

我在 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。

【问题讨论】:

  • "Terminal.windows.at(0).tabs 本质上是一个数组" -- 这是不正确的。它实际上是一个查询对象(“对象说明符”),描述了窗口对象与其选项卡对象之间的一对多关系。 (JXA 的实现是 FUBAR,它的文档存在,Apple 对用户的支持几乎不存在。欢迎。)虽然这次真正的罪魁祸首是 Terminal 蹩脚的 Apple 事件接口:它的 make 命令完全被破坏并且已经存在多年.所以你要么不得不求助于 GUI 脚本,要么让自己成为一个更好的终端仿真器应用程序,比如 iTerm。对不起。

标签: applescript osx-yosemite javascript-automation


【解决方案1】:

以下代码适用于chromesafari,但不适用于terminal,我仍在寻找原因,看看这些信息是否有帮助。

chrome = Application("Google Chrome")
newTab = chrome.Tab()
chrome.windows[0].tabs.push(newTab)

【讨论】:

    【解决方案2】:

    查看以下是否适用于您的情况:

    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'});
    

    【讨论】:

      【解决方案3】:

      您可以模拟一个新标签的快捷方式。还需要声明目标选项卡

      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
      

      【讨论】:

        猜你喜欢
        • 2011-11-02
        • 1970-01-01
        • 2017-06-12
        • 1970-01-01
        • 2014-11-15
        • 1970-01-01
        • 2018-06-28
        • 1970-01-01
        相关资源
        最近更新 更多