【问题标题】:new tab in iTerm2iTerm2 中的新选项卡
【发布时间】:2016-08-01 06:35:02
【问题描述】:

我使用的是 Iterm2 版本 Build 3.0.4 我想创建别名以从命令行打开新选项卡(在 bash 中) 我试过这段代码:

    function tab () {
    osascript &>/dev/null <<EOF
activate application "iTerm"
    tell application "System Events" to keystroke "t" using command down
    tell application "iTerm" to tell session -1 of current terminal to write text "pwd"
EOF
}

但它不起作用。谁能解决这个版本(或更新版本)的问题?

【问题讨论】:

    标签: bash iterm2 iterm


    【解决方案1】:

    iTerm2 v3 具有大大改进的 AppleScript 支持,因此您现在可以直接创建选项卡,而无需发送击键:

    tab() {
        osascript &>/dev/null <<EOF
          tell application "iTerm"
            activate
            tell current window to set tb to create tab with default profile
            tell current session of current window to write text "pwd"  
          end tell
    EOF
    }
    

    要水平拆分新标签(就像按 ⇧⌘D 一样),请添加:

    tell current session of current window to split horizontally with same profile
    

    pwd 写入拆分创建的新会话(新标签的下半部分):

    tab() {
        osascript &>/dev/null <<EOF
          tell application "iTerm"
            activate
            tell current window to set tb to create tab with default profile
            tell current session of current window to set newSplit to split horizontally with same profile
            tell newSplit
              select
              write text "pwd"
            end tell    
          end tell
    EOF
    }
    

    要浏览 iTerm2 的可用 AppleScript 命令,请打开 Script Editor.app,选择 File &gt; Open Dictionary...,然后选择 iTerm.app

    还请考虑我的ttab CLI,它包含标签/窗口创建以及Terminal.appiTerm2.app 的高级功能(但它不支持拆分标签)。

    【讨论】:

    • 拆分会话中执行 pwd 的代码对我来说不起作用。将tell newSplit 替换为tell second session of current tab of current window。来自this answer。您还可以删除 水平拆分 命令的 set newSplit to 部分。 要拆分当前会话(不创建新标签),请删除整个 create tab 行。
    • @TobiasGeisler:我已经有一段时间没有看到这个了,但是这两个功能在我运行 iTerm 3.2.9 的 macOS 10.14.5 机器上似乎都可以正常工作。
    • ttab 可以冲泡,是一个非常简单的选择
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多