【发布时间】: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