好的,所以我有这个非常简单的 python 脚本:
import time
import sys
for i in range(25):
time.sleep(1)
print(i)
sys.exit()
当我使用 python 运行它 ( /usr/local/bin/python3.6 testscript.py
) 时,一切正常,输出如下:
1
2
3
4
etc..
每个数字在另一个之后打印 1 秒。
但是,当我运行时:
/usr/bin/osascript -e 'do shell script "/usr/local/bin/python3.6 testscript.py" with prompt "Sart Testing " with administrator privileges'
25 秒内没有任何输出,最后打印:
24
到终端。
问题是:如何让 osascript 打印出与直接运行 Python 脚本时完全相同的输出?
AppleScript 的 do shell script
命令在非交互式 shell 中运行,因此您不能按原样执行该osascript
命令,并期望它输出与通过运行python
脚本python
或直接运行脚本相同的输出。换句话说,直接添加python
shebang并使文件可执行,因此./testscript.py
在终端中就是你所需要的。或者用做终端和它的do script
命令与osascript
。
将python
代码另存为。例如:
#!/usr/local/bin/python3.6
import time
import sys
for i in range(25):
time.sleep(1)
print(i)
sys.exit()
使其可执行:
chmod u+x testscript.py
在终端中运行它:
./testscript.py
或者:
osascript -e 'tell app "Terminal" to do script "/path/to/testscript.py"'
或者没有shebang并且在使用终端命令时无法执行的python
代码: do script
osascript -e 'tell app "Terminal" to do script "/usr/local/bin/python3.6 /path/to/testscript.py"'
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句