10

iTerm 2で 2 つのタブを開くには、次のように動作します。

代わりに分割ペインを使用する方法を理解できないようです。

いくつかのフォーラムで見たものを適用しようとしましたが、うまくいきません。誰かが私を正しい方向に向けることができますか?

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
4

6 に答える 6

31

新しいナイトリー ビルドでは、かなり快適です。約 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

私はこれを探すのに時間がかかりすぎたので、これは私が最初に見つけたものの1つだったので、これで誰かを助けることができるかもしれません(おそらく数週間で私自身). このソリューションは、他の回答にはないアクティブな focus-follows-mouse でも機能します。

于 2015-02-14T03:55:38.467 に答える
5

更新: iTerm2 v3では大幅に改善されましたが、互換性のない AppleScript サポートがあります。https ://www.iterm2.com/documentation-scripting.htmlを参照してください。

@Joel自身の回答の背景を提供するには:

iTerm 2 の AppleScript サポート (iTerm 2 v1.0.0.201306220 以降):

  • 不完全です: スクリプト分割ペインはサポートされていません。したがって、OP は、キーストロークを送信するという最適ではない手法に頼っています。

  • 奇妙な動作を示します:ブロック内のステートメントを(AppleScript Editor で)コンパイルすると、プレフィックスが不可解にもの前に挿入されます - 以下のコードはプリコンパイルされていないため、将来の問題を回避するために、このプレフィックスは含まれていません。tell "System Events" ...tell application "iTerm"i term application"System Events"

  • バグがある / 辞書と矛盾しています:辞書がコマンドとして説明しているものexec- 実際には機能しません - 実際にはwrite textコマンドによって実行されます: 渡された引数を実行するか、- 引数の末尾にスペースがある場合 - 単純に「型" 引数を提出せずに。

これは、回避策(キーストロークの送信)に基づく分割ペインを使用したソリューションbashです-AppleScriptコードを経由して呼び出すスクリプトosascript

ペインがディクショナリの一部ではないことによる制限:

  • 開いている他のペインに名前を付けることはできません
  • 他のペインにコマンドを送信できません
#!/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
于 2014-03-31T02:16:39.290 に答える
1

役立つ場合: 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 自体。これで問題が軽減されるのではないかと思います。)

于 2014-09-25T02:40:46.090 に答える
0

さて、私はついにこれを理解しました。

キーストロークをアプリケーションに送信することで、分割ペインを開いてナビゲートできます。

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"
于 2014-03-29T01:55:51.223 に答える