【问题标题】:Applescript: iTerm to split pane from the window the script was calledApplescript:iTerm 从调用脚本的窗口中拆分窗格
【发布时间】:2016-08-29 21:28:52
【问题描述】:

我有以下脚本尝试垂直拆分窗口,并在新窗格中添加一个日志文件。

#!/bin/bash

COMMAND="tail -f log_file"

# Do something important.
sleep 4

# Split the window, and tail logs.
osascript <<-EOF
tell application "iTerm"
    tell current session of current window
        split vertically with default profile command "$COMMAND"
    end tell
end tell
EOF

但是,此脚本会拆分当前焦点所在的窗口,而不是运行脚本的窗口。

重现问题的步骤:

  1. 打开一个 iTerm 窗口(比如 W1),然后运行此脚本。
  2. 当脚本正在执行 sleep 4 时,打开另一个窗口(比如 W2)并保持 W2 处于焦点。
  3. 4 秒后,较新的窗口(即 W2)将被垂直拆分。

如何从 W1(调用脚本的窗口)打开拆分窗口?

【问题讨论】:

    标签: bash applescript iterm2 iterm osascript


    【解决方案1】:

    在脚本开始时获取当前会话的 ID。

    稍后在脚本中,获取该标识符对应的会话

    #!/bin/bash
    myID=$(osascript -e 'tell application "iTerm" to id of current session of current window')
    
    COMMAND="tail -f log_file"
    
    # Do something important.
    sleep 4
    
    # Split the window, and tail logs. myID2 is the id of the new session (the split pane)
    myID2=$(osascript <<-EOF
    tell application "iTerm"
        repeat with w in windows
            repeat with t in tabs of w
                tell (first session of t whose its id = "$myID")
                    if exists then return id of (split vertically with default profile command "$COMMAND")
                end tell
            end repeat
        end repeat
    end tell
    EOF)
    

    【讨论】:

    • 完美运行,谢谢。是否有可能存在多个具有相同 Id 的窗口?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-27
    • 2011-07-23
    • 2018-12-22
    • 1970-01-01
    • 1970-01-01
    • 2013-03-20
    相关资源
    最近更新 更多