交互式/实时制作osascript打印标准输出 [英] Make osascript print stdout interactively / in real-time

查看:282
本文介绍了交互式/实时制作osascript打印标准输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,所以我有这个非常简单的python脚本:

Ok, so I have this very simple python script:

import time
import sys

for i in range(25):
    time.sleep(1)
    print(i)

sys.exit()

当我使用python运行它(/usr/local/bin/python3.6 testscript.py)时,一切正常,输出显示为:

When I use python to run it (/usr/local/bin/python3.6 testscript.py), all works fine and the output reads:

1
2
3
4
etc..

每个数字都紧随其后打印1秒钟.

With each number printed 1 second after the other.

但是当我跑步时:

/usr/bin/osascript -e 'do shell script "/usr/local/bin/python3.6 testscript.py" with prompt "Sart Testing " with administrator privileges'

25秒没有任何输出,最后打印出来:

There isn't any output for 25 seconds and finally it prints:

24

到终端.

问题是:如何使osascript与直接运行Python脚本时输出的输出完全相同?

The question is: How can I make osascript print the exact same output as when I run the Python script directly?

推荐答案

AppleScript的 do shell script 命令在非交互式外壳中运行,因此您无法执行osascript 命令,并希望它输出与通过python运行python 脚本相同或直接运行.换句话说,直接添加python shebang 并使文件可执行,从而使 Terminal 中的./testscript.py成为您所需要的.或者使用 Terminal (端子),以及使用osascriptdo script 命令.

AppleScript's do shell script command runs in a non-interactive shell, so you cannot execute the osascript command as you have it and expect it to output the same as running the python script via python or run directly. In other words, directly being adding the python shebang and making the file executable and thus ./testscript.py in Terminal is all you need. Or do it with Terminal and its do script command with 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"'

或者python 代码没有 shebang ,并且在使用 Terminal的 do script 命令:

Or the python code without the shebang and not made executable while using Terminal's do script command:

osascript -e 'tell app "Terminal" to do script "/usr/local/bin/python3.6 /path/to/testscript.py"'

这篇关于交互式/实时制作osascript打印标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆