iterm2 - How can I retrieve the contents of an iTerm session with Applescript? -


i working on applescript make logging 2-factor authentication domains little easier. long story short, instead of using delays , sending text, i'd poll contents of current session , enter usernames/passwords/tokencodes prompt them appears. luckily, iterm v3.x has bunch of cool applescript stuff: https://www.iterm2.com/documentation-scripting.html

but i'm having lot of trouble reading contents of terminal session. here's i've got far:

on run # start or activate iterm tell application "iterm"     activate      tell first window         # create new tab, create new session inside         set newtab (create tab default profile)         tell newtab             # since created tab, there should 1 session right now.             repeat asession in sessions                 tell asession                     delay 3                     #set myvar (tty)                     #set myvar (text)                     set myvar (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 

as can see, i've tried several different things, "contents" seemed promising solution according documentation, crazy stuff comes out, this:

session id "0986f3bd-d2af-480f-b517-ab7a43b2a0c4" of tab 3 of window id "window-1" of application "iterm" 

what stuff? why don't see expect, this:

last login: fri jun 10 18:18:22 on ttys001 me@macbook-pro:~|⇒ 

i got work 3-5 times in row, edited script again, started returning session id stuff. @ point, decided applescript or iterm's applescript api opaque. hammered out workaround seems work pretty well, here comes after me:

on grepcountsfor(searchstring)     set terminalcontents getcontents()     log "terminal contents: " & terminalcontents      set oneline ""     set allrecords paragraphs of terminalcontents     repeat arecord in allrecords         if length of arecord greater 0             set variable arecord             log "variable: " & variable             set oneline oneline & variable         end if     end repeat      log "oneline: " & oneline     set command "echo \"" & oneline & "\" | grep -o \"" & searchstring & "\" | wc -l"      log "command: " & command     set counts shell script command     return counts number end grepcountsfor  on getcontents()     #iterm needs in front key presses register.     waitforwindow("iterm")     # mush buttons in app     tell application "system events"         keystroke "a" using command down         keystroke "c" using command down         set sessioncontents shell script "pbpaste"     end tell     return sessioncontents end getcontents  # waits window come focus on waitforwindow(appname)     # poll until "appname" active window     set activeapp "noapp"     repeat until activeapp appname         set activeapp (path frontmost application unicode text)         # if active app name not contain target,          # try activate again.         if appname not in activeapp             tell application appname                 activate             end tell         else             # done             exit repeat         end if         delay 0.1     end repeat end waitforwindow 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -