macOS python打开一个终端并执行指令(亲测有效)

实现:

可以使用的库是:applescript, 但是每执行一次tell.app()就会打开一个新终端,没办法连续性操作,网上的文档很少。

                旧版库:appscript,据说已经停用

安装:

pip3 install applescript

源码: 

from applescript import tell

#set what command you want to run here
yourCommand = 'ls'

tell.app( 'Terminal', 'do script"' + yourCommand + '"' , background = True)
tell.app( 'Terminal', 'do script"' + "who" + '"')

 官方文档 :

applescript · PyPIrun applescripthttps://pypi.org/project/applescript/

不执行指令,单纯用python打开一个终端 

import os
os.system('open -a Terminal .')

参考 :Python & macOS: open new Terminal window from Python, passing command to be executed - Stack Overflowhttps://stackoverflow.com/questions/48408278/python-macos-open-new-terminal-window-from-python-passing-command-to-be-exec

你可能感兴趣的:(python,macos,applescript)