Terminal Applescript(osascript):在iTerm 2中打开拆分窗格并执行命令

Terminal Applescript(osascript):在iTerm 2中打开拆分窗格并执行命令,terminal,applescript,iterm,osascript,Terminal,Applescript,Iterm,Osascript,以下操作可在中打开两个选项卡 我似乎不知道如何使用拆分窗格来实现这一点 我已经尝试过应用我在几个论坛上看到的东西,但它从来都不起作用。有人能给我指出正确的方向吗 osascript好吧,我终于明白了 通过向应用程序发送击键,您可以打开并导航拆分窗格 tell i term application "System Events" to keystroke "D" using command down tell i term application "System Events" to keyst

以下操作可在中打开两个选项卡

我似乎不知道如何使用拆分窗格来实现这一点

我已经尝试过应用我在几个论坛上看到的东西,但它从来都不起作用。有人能给我指出正确的方向吗


osascript好吧,我终于明白了

通过向应用程序发送击键,您可以打开并导航拆分窗格

tell i term application "System Events" to keystroke "D" using command down

tell i term application "System Events" to keystroke "]" using command down
向拆分窗格发送命令并命名每个窗格的示例。我使用它来启动我的节点应用程序

write text "cd $projectsFolder/$2.m"

write text "/usr/local/bin/frontend.sh $1 $2"

tell i term application "System Events" to keystroke "D" using command down

tell i term application "System Events" to keystroke "]" using command down

set name to "$2.api"

write text "cd $projectsFolder/$2.api"

write text "/usr/local/bin/backend.sh $1 $2"
更新:iTerm2v3有了很大的改进,但是不兼容的AppleScript支持-请参阅

为@Joel自己的答案提供一些背景信息:

iTerm 2的AppleScript支持(自iTerm 2 v1.0.0.201306220起):

  • 不完整:不支持脚本分割窗格-因此OP采用了发送击键的次优技术

  • 表现出一些奇怪的行为:当编译(在AppleScript编辑器中)一个
    告诉“系统事件”…
    语句在
    告诉应用程序“iTerm”
    块中时,前缀
    i term application
    被莫名其妙地插入
    “系统事件”
    ,因为下面的代码没有预编译,不包括此前缀,以避免将来出现问题

  • hasbugs/与其字典不一致:字典所描述的
    exec
    命令实际上是由
    write text
    命令实现的,该命令执行传递的参数,或者如果参数有尾随空格,则只执行“types”没有提交的论点

下面是一个基于解决方案(发送击键)的拆分窗格解决方案—一个
bash
脚本,通过
osascript
调用AppleScript代码:

由于窗格不是字典的一部分,因此存在限制:

  • 无法命名打开的其他窗格
  • 无法将任何命令发送到其他窗格
#/usr/bin/env bash
projectFolder=“$HOME/Desktop”#示例

osascript如果有用的话:我有一个类似的问题,就是希望在iTerm中有一个组合键快捷方式来拆分窗格,并让新窗格继承原始会话的标题。我提出了以下方法,解决了这个问题,并且不太依赖于发送击键(尽管我希望完全消除它们)


我正在使用BetterTouchTool将一个键组合(即,
cmd+”
)绑定到这个AppleScript的执行。(我发现有些组合键会变得很奇怪,我天真地猜测,因为你实际上是在脚本发送的任何击键之上按住组合键。我还没有在iTerm本身的首选项中详细说明如何定义键盘快捷键。我怀疑这可能会缓解这个问题。)

新的夜间构建非常不错。尽管大约在一年前实施,但在公共版本中似乎没有:


我不得不花很长时间来寻找这个,所以也许我可以帮助别人解决这个问题(可能是我自己在几周内),因为这是我发现的第一件事。此解决方案甚至适用于其他答案中缺少的激活焦点跟踪鼠标。

从@mklement0开始,这是我的脚本,它打开一个新选项卡,分成4个面板并运行命令:

#!/usr/bin/env bash
osascript <<-EOF
    set cmds to {"rabbitmq-server", "mongod", "redis-server", "htop"}

    tell application "iTerm"
        activate
        set myterm to (current terminal)

        tell myterm
            launch session "Default Session"

            # split vertically
            tell application "System Events" to keystroke "d" using command down
            delay 1
            # previus panel
            tell application "System Events" to keystroke "[" using command down
            delay 1
            # split horizontally
            tell application "System Events" to keystroke "d" using {shift down, command down}
            delay 1
            # next panel
            tell application "System Events" to keystroke "]" using command down
            delay 1
            # split horizontally
            tell application "System Events" to keystroke "d" using {shift down, command down}

            set n to count of cmds
            repeat with i from 1 to n
                # next panel
                tell application "System Events" to keystroke "]" using command down
                delay 1
                tell the current session to write text (item i of cmds)
            end repeat
        end tell

    end tell  
EOF
#/usr/bin/env bash

osascript正如Dom所指出的,在新的2.9测试版中,其他答案将不再有效。我因无法实现自动化而感到沮丧,因此我编写了一个与teamocil兼容的命令行工具,它可以做到这一点:


它允许您为预先配置的窗口和窗格编写YAML文件,这些窗口和窗格可以为给定的项目运行预定义的命令集。

您是否意外地包含令牌
i term
?不,我没有,@mklement0.Thank;我后来意识到,当封装在
告诉应用程序“iTerm”
块中时,这实际上是有效的。我被这种不寻常的语法所吸引-似乎
iTerm
本身在
tell application“iTerm”
块中编译
tell application…
语句时,会将
I term application
预先添加到应用程序名称中。一种等效形式(带有一个显式的
激活
命令,以确保击键被发送到
iTerm
)是:
告诉应用程序“iTerm”激活
,然后
告诉应用程序“系统事件”使用命令down来击键“D”。啊,好的。实际上我还没有编译这些,它只是包含在我正在编写的bash脚本中;如果要返回到以前的活动窗格,最好发送
“[”
而不是
“]”
——这样,即使已经有多个窗格,也能正常工作。基本正确@mklement0,但您可以命名每个窗格并将击键发送到另一个窗格。我在上面更新了。@Joel:Hmmm。。。那对我不起作用。查看更新代码的逻辑,您可以在原始窗格中执行所有操作(因为
name
和另一组
write
命令是在发送后执行的)⌘-], 这将使您返回到原始窗格)。我错过什么了吗?您使用的是什么
iTerm
版本?这对我很有用。我不得不将
选择第一个终端窗口
更改为
选择第一个窗口
将准确的代码粘贴到apple脚本编辑器中,它给了我“错误”信息。我收到了一个错误:无法获取窗口id 35415的当前窗口。“编号-1728来自窗口id 35415的当前窗口”非常感谢!!告诉应用程序“iTerm”使用默认配置文件创建窗口告诉当前窗口的当前会话写文本“bash itermocil log”结束告诉结束告诉
tell application "iTerm"
    activate
    select first terminal window

    # Create new tab
    tell current window
        create tab with default profile
    end tell

    # Split pane
    tell current session of current window
        split vertically with default profile
        split vertically with default profile
    end tell

    # Exec commands
    tell first session of current tab of current window
        write text "cd ~/Developer/master-node"
        write text "coffee"
    end tell
    tell second session of current tab of current window
        write text "gulp w"
    end tell
    tell third session of current tab of current window
    end tell
end tell
#!/usr/bin/env bash
osascript <<-EOF
    set cmds to {"rabbitmq-server", "mongod", "redis-server", "htop"}

    tell application "iTerm"
        activate
        set myterm to (current terminal)

        tell myterm
            launch session "Default Session"

            # split vertically
            tell application "System Events" to keystroke "d" using command down
            delay 1
            # previus panel
            tell application "System Events" to keystroke "[" using command down
            delay 1
            # split horizontally
            tell application "System Events" to keystroke "d" using {shift down, command down}
            delay 1
            # next panel
            tell application "System Events" to keystroke "]" using command down
            delay 1
            # split horizontally
            tell application "System Events" to keystroke "d" using {shift down, command down}

            set n to count of cmds
            repeat with i from 1 to n
                # next panel
                tell application "System Events" to keystroke "]" using command down
                delay 1
                tell the current session to write text (item i of cmds)
            end repeat
        end tell

    end tell  
EOF