从 Apple 脚本将文本输入 iTerm 终端

Art*_*hou 2 mac iterm applescript

我正在尝试将文本从 Apple 脚本粘贴到 iTerm。

\n\n
activate application "iTerm"\ntell application "iTerm"\n    tell current tab of current window\n        set cmd to "command"\n        keystroke cmd\n        keystroke return\n    end tell\nend tell\n
Run Code Online (Sandbox Code Playgroud)\n\n

但我收到错误:

\n\n
\n

iTerm 出现错误:Can\xe2\x80\x99t 获取当前窗口当前选项卡的击键“命令”。

\n
\n\n

任何想法如何解决这一问题?

\n\n

正如我在这里有可用的write text命令https://www.iterm2.com/documentation-scripting.html

\n

use*_*894 7

write text 命令是在 中完成的current session,因此请使用以下示例 AppleScript 代码

activate application "iTerm"
tell application "iTerm"
    tell current session of current window
        set cmd to "command"
        write text cmd
    end tell
end tell
Run Code Online (Sandbox Code Playgroud)

要使用该keystroke 命令,请使用System Events

activate application "iTerm"
tell application "iTerm"
    tell current tab of current window
        set cmd to "command"
        tell application "System Events"
            keystroke cmd
            keystroke return
        end tell
    end tell
end tell
Run Code Online (Sandbox Code Playgroud)

注意:您可能需要策略性地放置一个delay 命令,以允许iTermwrite text在执行或keystroke 命令之前完全激活。