[英]What's Python's os.system() equivalent to cocoa/Objective-C?
I'd like to run some terminal command within my Mac program. 我想在Mac程序中运行一些终端命令。 For example, I normally use
mate abc.txt
or python abc.py
from my shell to open Text Mate or running python script. 例如,我通常从外壳使用
mate abc.txt
或python abc.py
来打开Text Mate或运行python脚本。
How can I do the same thing with Cocoa/Objective-C? 如何使用Cocoa / Objective-C做同样的事情?
I thought about this method , but I prefer just running in one line. 我考虑过这种方法 ,但我更喜欢只运行一行。
For example, when users enter "python abc.py", I just run the command as if it's in the Terminal.app. 例如,当用户输入“ python abc.py”时,我就像在Terminal.app中一样运行命令。 Python's equivalent command is
os.system()
. Python的等效命令是
os.system()
。
<-- inputItem TextField
<-inputItem TextField
NSString* item = [inputItem stringValue];
[NSRuncommandLikeShell item]; <-- ????
The environment variables should be retained so that I don't need to run '/usr/bin/python'. 应该保留环境变量,这样我就不需要运行'/ usr / bin / python'。
I can use ~/.MacOSX/environment.plist
file for setup path environment. 我可以将
~/.MacOSX/environment.plist
文件用于安装路径环境。 Or, I also could use this utility for GUI. 或者,我也可以将此实用程序用于GUI。
You can use the C runtime system()
call. 您可以使用C运行时
system()
调用。
#include <stdlib.h>
system([item UTF8String]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.