iTerm2中的新标签页

6
我正在使用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
}

但它不能正常工作。有人能解决这个问题吗(或使用更新版本)?

2个回答

15

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,选择 文件 > 打开词典...,然后选择 iTerm.app

另外可以考虑使用我的ttab 命令行工具,它提供了创建标签页/窗口和高级功能,支持 both Terminal.appiTerm2.app (但不支持拆分标签页)。


分离会话中执行pwd的代码没有起作用。请将tell newSplit替换为tell second session of current tab of current window,参考此答案。您还可以删除_split horizontally_命令中的set newSplit to部分。要拆分当前会话(而不是创建新标签),请删除整个_create tab_行。 - Tobias Geisler
@TobiasGeisler:我已经有一段时间没有看过这个了,但是在我的macOS 10.14.5机器上运行iTerm 3.2.9时,这两个函数似乎都能正常工作。 - mklement0
1
ttab 可以被编译,并且是一个非常简单的选项。 - xverges

0
tell application "iTerm"
activate
tell current window to create tab with default profile
tell session of current tab of current window
    select
    write text "pwd"
end tell

结束告诉


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接