如何在AppleScript变量中保存击键输出?



我编写了一个AppleScript,它通过在终端中输入命令来启动我的shell。但是我想测试shell,所以我想将命令结果保存在一个变量中。这是我的代码:

#!/bin/bash
osascript <<EOF
tell application "System Events"
keystroke "cd /Users/uwe/documents/coding/c/pshell"
delay 0.5
keystroke return
delay 0.5
keystroke "make compile_and_run"
delay 1
keystroke return
delay 0.5
keystroke "bash /Users/uwe/documents/coding/c/pshell/tests/user_test.sh"
delay 0.5
keystroke return
key code 126
delay 0.5
set arrow_result to keystroke return // this is where I want to save the output
display dialog arrow_result // it says there is no variable called 'arrow_result'
end tell
EOF

如果可能的话,我也可以将结果存储在bash变量中,但我认为这行不通

你当然可以发送按键命令,然后使用以下命令获取终端窗口的文本内容(然后解析它):

set arrow_result to value of text field 1 of scroll area 1 of splitter group 1 of window 1 of process "Terminal"

但为什么要这么变态。没有必要使用终端,系统事件和GUI脚本来完成您的任务。相反,使用AppleScript语言中最强大的工具,do shell脚本命令:

#!/bin/bash
osascript <<EOF
set arrow_result to(do shell script "cd /Users/uwe/documents/coding/c/pshell
make compile_and_run
bash /Users/uwe/documents/coding/c/pshell/tests/user_test.sh")
display dialog arrow_result
EOF
注意:我用下面的代码在终端中测试了我的建议:
osascript <<EOF
set arrow_result to do shell script "echo "Hello, World!""
display dialog arrow_result
EOF

最新更新