我在执行不同的arguments时遇到问题。相同的NSTask,不同的NSTask。我有一个类,它的实例管理自己的NSTask对象,根据初始化这些实例的参数,依赖于NSTask对象的创建。我有两个初始化器:
// Method for finished task
- (void)taskFinished:(NSNotification *)aNotification {
[myTask release];
myTask = nil;
[self createTask];
}
// Designated initializer
- (id) init {
self = [super init];
if (self != nil) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(taskFinished:)
name:NSTaskDidTerminateNotification
object:nil];
[self createTask];
}
return self;
}
// Convenience initializer
- (id)initWithCommand:(NSString *)subCommand {
self = [self init];
if (self)
{
[self setCommand:subCommand];
}
return self;
}下面是createTask方法:
- (void)createTask {
// myTask is a property defined as NSTask*
myTask = [[NSTask alloc] init];
[myTask setLaunchPath:@"/usr/bin/executable"];
}这些操作是通过选择NSOutlineView中的不同行(使用PXSourceList作为包装器)来执行的:
- (void)sourceListSelectionDidChange:(NSNotification *)notification {
id sourceList = [notification object];
NSIndexSet *selection = [sourceList selectedRowIndexes];
NSString *identifier = [[sourceList itemAtRow:[selection firstIndex]] identifier];
// this way `/usr/bin/executable ${identifier}` is being created
MyCommand *command = [[MyCommand alloc] initWithSubcommand:identifier];
// this method executes [myTask launch];
[command execute]
}问题是只有第一个会被执行。第二个甚至不会触发"click“事件(通过target-action)。我认为这可能是我正在尝试使用launchPath的原因,因为简单的/bin/ls运行良好。在终端中相同的命令返回值为0(即一切正常)。任何指南或陷阱都非常感谢。
发布于 2011-08-17 06:56:42
我不明白为什么..。但我从numerous places上读到NSTask只能运行一次...
使用NSTask,您的程序可以将另一个程序作为子进程运行,并可以监视该程序的执行。NSTask创建了一个单独的可执行实体;与NSThread不同,它不与父进程共享内存空间。默认情况下,任务继承其父环境的几个特征:当前目录、标准输入、标准输出、标准错误和任何环境变量的值。如果您想要更改其中任何一个,例如,当前目录,您必须在启动任务之前设置一个新值。一旦任务启动,任务的环境就建立起来了。
NSTask只能运行一次。随后尝试运行NSTask会引发错误。
如果在基于文档的应用程序中从文档运行任务,则应该(至少)在文档的清理代码中将终止消息发送到任务实例。另请参阅NSTaskTermination以了解更多讨论。
这看起来很荒谬..。如果我发现任何与这个来源相矛盾的信息,我会研究并发回(尽管它通常是可靠的)。
发布于 2011-09-15 07:00:20
如果您想涉足PyObjC周围的浑浊环境,您可以很容易地使用Python..的subprocess机制。一遍一遍,一遍又一遍。哦,是的。
https://stackoverflow.com/questions/2846747
复制相似问题