【问题标题】:Applescript (osascript): opening split panes in iTerm 2 and performing commandsApplescript (osascript):在 iTerm 2 中打开拆分窗格并执行命令
【发布时间】:2014-03-11 23:59:12
【问题描述】:

以下工作在iTerm 2中打开两个标签

我似乎无法弄清楚如何使用拆分 panes 来实现这一点。

我已尝试应用我在多个论坛上看到的内容,但始终没有成功。有人能指出我正确的方向吗?

osascript <<-eof
        tell application "iterm"
                set myterm to (make new terminal)
                tell myterm
                        launch session "Default session"
                        tell the last session
                                set name to "Server"
                                write text "cd $projectsFolder"
                        end tell
                        launch session "Default session"
                        tell the last session
                                set name to "Console"
                                write text "cd $projectsFolder"
                        end tell
                end tell
        end tell
eof

【问题讨论】:

    标签: terminal applescript iterm osascript


    【解决方案1】:

    有了新的夜间版本,它非常好。公开版本中似乎缺少它,尽管它是在大约一年前实施的: Source - AppleScriptTest.m

    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
    

    我不得不为此搜索太长时间,所以也许我可以帮助某人解决这个问题(可能是我自己在几周内),因为这是我发现的第一件事。该解决方案甚至可以与激活的焦点跟随鼠标一起使用,而其他答案中没有。

    【讨论】:

    • 这对我有用。我确实必须将select first terminal window 更改为select first window
    • 将确切的代码粘贴到苹果脚本编辑器中,它给了我“错误”iTerm 出错:无法获取窗口 id 35415 的当前窗口。”从窗口 id 35415 的当前窗口开始的数字 -1728 "
    【解决方案2】:

    正如 Dom 所指出的,在新的 2.9 beta 版本中,其他答案将不再适用。我对无法自动执行此操作感到沮丧,因此我编写了一个与 teamocil 兼容的命令行工具来完成此操作:

    https://github.com/TomAnthony/itermocil

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

    【讨论】:

    • 非常感谢!! tell application "iTerm" create window with default profile tell current session of current window write text "bash itermocil log" end tell end tell
    【解决方案3】:

    更新:iTerm2 v3大大改进,但不兼容 AppleScript 支持 - 请参阅 https://www.iterm2.com/documentation-scripting.html

    为@Joel 自己的回答提供一些背景:

    iTerm 2 的 AppleScript 支持(从 iTerm 2 v1.0.0.201306220 开始):

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

    • 表现出一些奇怪的行为:当编译(在 AppleScript 编辑器中)tell application "iTerm" 块内的 tell "System Events" ... 语句时,前缀 i term application 是莫名其妙插在"System Events"之前——因为下面的代码没有预编译,所以包含这个前缀,以免以后出现问题。

    • 错误/与其字典不一致:字典中描述为 exec 命令的内容 - 实际上不起作用 - 实际上由 write text 命令完成:它执行传递的参数,或者 - 如果参数有一个尾随空格 - 只需“键入”参数而不提交它。

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

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

    • 无法命名打开的其他窗格
    • 无法将命令发送到其他窗格
    #!/usr/bin/env bash
    
    projectFolder="$HOME/Desktop" # example
    osascript <<-EOF
      tell application "iTerm"
        tell (make new terminal) # Create a new pseudo terminal...
          tell (launch session "Default session") # ... and open a session (window)
            # Name the new window (its original pane).
            set name to "Server"
            # Execute the 'cd' command in the original pane.
            write text "cd '$projectFolder'"
            # Create a new split pane, horizontally, by sending ⌘⇧-D 
            tell application "System Events" to keystroke "d" using {shift down, command down}
              # !! Note: We canNOT:
              #  - name this pane separately
              #  - execute a command in it.
            # Return to the original pane, by sending ⌘-[ 
            tell application "System Events" to keystroke "[" using {command down}
          end tell
        end tell
      end tell
    EOF
    

    【讨论】:

    • 大部分是正确的@mklement0,但您可以命名每个平移并将击键发送到另一个窗格。我在上面更新了。
    • @Joel:嗯……这对我不起作用。查看更新代码的 逻辑,您在 original 窗格中执行 all 操作(因为 name 和另一组write 命令在发送 ⌘-] 后执行,这会导致您返回到 original 窗格)。我错过了什么吗?你用的是什么iTerm 版本?
    【解决方案4】:

    从@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
    

    【讨论】:

      【解决方案5】:

      如果它有帮助:我有类似的问题,希望 iTerm 中的组合键快捷方式拆分窗格并让新窗格继承原始会话的标题。我想出了以下方法,它解决了这个问题并且减少了对发送击键的依赖(尽管我很想完全消除它们)。

      tell application "iTerm"
          tell the current terminal
              tell the current session
                  set the_name to get name
                  tell i term application "System Events" to keystroke "d" using {command down, shift down}
              end tell
      
              tell the current session
                  set name to the_name
              end tell
          end tell
      end tell
      

      我正在使用 BetterTouchTool 将一个组合键(即cmd+')绑定到此 AppleScript 的执行。 (我发现某些按键组合会变得很麻烦,我会天真地猜测,因为您有效地将按键组合放在脚本发送的任何按键之上。我没有追查如何在首选项中定义键盘快捷键iTerm 本身。我怀疑这可能会缓解这个问题。)

      【讨论】:

        【解决方案6】:

        好吧,我终于想通了。

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

        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"
        

        【讨论】:

        • 您是否不小心包含了令牌i term
        • 不,我没有,@mklement0。
        • 谢谢;从那以后,我意识到这实际上可以包含在tell application "iTerm" 块中。我被不寻常的语法所困扰 - 当您在 tell application "iTerm" 块内编译 tell application ... 语句时,iTerm 本身似乎将 i term application 添加到应用程序名称之前。等效形式(带有明确的activate 命令以确保将击键发送到iTerm)将是:tell application "iTerm" to activate,然后是tell application "System Events" to keystroke "D" using command down
        • 啊,好吧。我实际上并没有编译任何这些,它只是包含在我正在处理的 bash 脚本中。
        • 知道了;如果意图返回到 previously active 窗格,最好发送"[" 而不是"]" - 这样,即使已经有多个窗格,它也可以工作。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-03
        • 1970-01-01
        相关资源
        最近更新 更多