简体   繁体   English

Python的os.system()等价于cocoa / Objective-C的东西是什么?

[英]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.txtpython 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 <-- 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'。

EDIT 编辑

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 an NSTask object. 您可以使用NSTask对象。 If you don't like all of the setup that goes in to this, you can create an NSTask category that does it for you. 如果您不喜欢其中涉及的所有设置,则可以创建一个NSTask类别来为您完成此任务。

You can use the C runtime system() call. 您可以使用C运行时system()调用。

#include <stdlib.h>

system([item UTF8String]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM