我正在开发一个Applescript,以便更轻松地登录到双因素身份验证域。长话短说,我想轮询当前会话的内容,并在出现提示时立即输入用户名/密码/令牌代码,而不是使用延迟和发送文本。幸运的是,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:~|⇒
答案 0 :(得分:0)
我连续工作了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