I'd like to run some terminal command within my Mac program.
For example, I normally use mate abc.txt
or python abc.py
from my shell to open Text Mate or running python script.
How can I do the same thing with 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's equivalent command is os.system()
.
NSString* item = [inputItem stringValue];
[NSRuncommandLikeShell item]; <-- ????
The enviro开发者_Python百科nment variables should be retained so that I don't need to run '/usr/bin/python'.
EDIT
I can use ~/.MacOSX/environment.plist
file for setup path environment. Or, I also could use this utility for GUI.
You can use an NSTask
object. 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.
You can use the C runtime system()
call.
#include <stdlib.h>
system([item UTF8String]);
精彩评论