【问题标题】:How can I retrieve the contents of an iTerm session with Applescript?如何使用 Applescript 检索 iTerm 会话的内容?
【发布时间】:2016-06-10 23:53:48
【问题描述】:

我正在开发一个 Applescript,以便更轻松地登录到 2 因素身份验证域。长话短说,我不想使用延迟和发送文本,而是想轮询当前会话的内容,并在出现提示时立即输入用户名/密码/令牌代码。幸运的是,iTerm v3.X 有一堆很酷的 AppleScript 东西: https://www.iterm2.com/documentation-scripting.html

但是我在阅读终端会话的内容时遇到了很多麻烦。这是我到目前为止所得到的:

on run
# Start or activate iTerm
tell application "iTerm"
    activate

    tell the first window
        # Create a new tab, which will create a new session inside
        set newTab to (create tab with default profile)
        tell newTab
            # Since we just created the tab, there should only be one session right now.
            repeat with aSession in sessions
                tell aSession
                    delay 3
                    #set myvar to (tty)
                    #set myvar to (text)
                    set myvar to (contents)
                    #do shell script "echo " & myvar & " >> ~/some_file.txt"
                    #write text (contents)
                end tell
            end repeat
        end tell
    end tell
end tell
return myvar
end run

如您所见,我尝试了几种不同的方法,根据文档,“内容”似乎是最有希望的解决方案,但出现了一些疯狂的东西,如下所示:

session id "0986F3BD-D2AF-480F-B517-AB7A43B2A0C4" of tab 3 of window id "window-1" of application "iTerm"

这是什么东西?为什么我看不到我所期望的,是这样的:

Last login: Fri Jun 10 18:18:22 on ttys001
me@MacBook-Pro:~|⇒

【问题讨论】:

  • 在我的 item2 版本“Build 2.9.20160313”中,您的脚本运行良好并且完全符合预期。我通过“脚本编辑器”运行了这个脚本。
  • 好吧,我下载了那个版本,我发誓我让这个脚本连续工作了 3 或 4 次,但是当我再次开始编辑我的脚本时,它开始返回关于会话的疯狂内容再身份证。我不知道我做错了什么,或者出了什么问题。
  • 您是从“脚本编辑器”还是以其他方式运行脚本?我不明白“连续”的含义......
  • 是的,我正在从脚本编辑器编辑和运行脚本。另外,对不起,“连续”是一个英语习语。我能想到的最接近的字面定义是“连续”或“一个接一个”,尽管这也可能是一个成语。
  • 知道了 :) idioms.thefreedictionary.com/in+a+row 。很高兴你解决了它。

标签: applescript iterm2 iterm


【解决方案1】:

我让它连续工作了 3-5 次,但是一旦我再次编辑我的脚本,它就开始返回那个会话 ID 的东西。那时,我认为 applescript 或 iTerm 的 applescript API 太不透明了。我敲定了一个实际上似乎工作得很好的解决方法,这里适用于任何追随我的人:

on grepCountsFor(searchString)
    set terminalContents to my getContents()
    log "terminal contents: " & terminalContents

    set oneline to ""
    set allRecords to paragraphs of terminalContents
    repeat with aRecord in allRecords
        if length of aRecord is greater than 0 then
            set variable to aRecord
            log "variable: " & variable
            set oneline to oneline & variable
        end if
    end repeat

    log "oneline: " & oneline
    set command to "echo \"" & oneline & "\" | grep -o \"" & searchString & "\" | wc -l"

    log "command: " & command
    set counts to do shell script command
    return counts as number
end grepCountsFor

on getContents()
    #iTerm needs to be in the front for key presses to register.
    my waitForWindow("iTerm")
    # Mush buttons in the app
    tell application "System Events"
        keystroke "a" using command down
        keystroke "c" using command down
        set sessionContents to do shell script "pbpaste"
    end tell
    return sessionContents
end getContents

# Waits for a window to come into focus
on waitForWindow(appName)
    # Poll until "appName" is the active window
    set activeApp to "noApp"
    repeat until activeApp is appName
        set activeApp to (path to frontmost application as Unicode text)
        # If the active app name does not contain the target, 
        # try to activate it again.
        if appName is not in activeApp then
            tell application appName
                activate
            end tell
        else
            # Done
            exit repeat
        end if
        delay 0.1
    end repeat
end waitForWindow

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多