NSTask启动导致崩溃

时间:2011-01-05 18:12:05

标签: cocoa xcode crash applescript nstask

我有一个可以通过此终端命令导入XML文件的应用程序:

  

open / path / to / main \ app.app --args myXML.xml

这很好用,没有任何问题。我已经使用Applescript通过shell启动此命令,它也可以正常工作。然而,当尝试使用Cocoa的NSTask Launcher时,请使用以下代码:

NSTask *task = [[NSTask alloc] init];

[task setLaunchPath:@"/usr/bin/open"];

[task setCurrentDirectoryPath:@"/Applications/MainApp/InstallData/App/"];

[task setArguments:[NSArray arrayWithObjects:[(NSURL *)foundApplicationURL path], @"--args", @"ImportP.xml", nil]];

[task launch];

应用程序将启动到初始屏幕,然后在单击下一个按钮或尝试关闭窗口时崩溃。我尝试过使用NSAppleScript:

NSAppleScript *script = [[NSAppleScript alloc] initWithSource:@"tell application \"Terminal\" do script \"open /Applications/MainApp/InstallData/App/Main\\\\ App.app\" end tell"];

NSDictionary *errorInfo;

[script executeAndReturnError:&errorInfo];

这将启动程序,它也会崩溃,我在Xcode调试窗口中收到此错误:

12011-01-04 17:41:28.296 LaunchAppFile[4453:a0f] 
Error loading /Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types:  dlopen(/Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types, 262): no suitable image found.  
Did find: /Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types: no matching architecture in universal wrapper
LaunchAppFile: OpenScripting.framework - scripting addition "/Library/ScriptingAdditions/Adobe Unit Types.osax" declares no loadable handlers.

所以通过研究我想出了这个:

NSAppleScript *script = [[NSAppleScript alloc] initWithSource:@"do shell script \"arch -i386 osascript /Applications/MainApp/InstallData/App/test.scpt\""];

NSDictionary *errorInfo;

[script executeAndReturnError:&errorInfo];

但这会导致与上一个命令相同的结果。 有关导致此次崩溃的原因的任何想法?

2 个答案:

答案 0 :(得分:0)

检查here以修复adobe错误。我不确定这是不是问题。实际上我无法通过open命令将参数传递给任何东西,所以我无法调查你的问题。

答案 1 :(得分:0)

仅使用完整路径再次尝试NSTask:

NSTask *task = [[NSTask alloc] init];

[task setLaunchPath:@"/usr/bin/open"];

[task setArguments:[NSArray arrayWithObjects:[ @"'/path/to/main app.app'", @"--args", @"/path/to/ImportP.xml", nil]];

[task launch];

另一种方法是给NSTask以下命令行:

sh -c '/usr/bin/open "/path/to/main app.app" --args /path/to/myXML.xml'

...

NSTask *task = [[NSTask alloc] init];

[task setLaunchPath:@"/bin/sh"];

NSString *cmd = [NSString stringWithFormat:
         @"%@ %@ %@ %@", 
         @"/usr/bin/open", 
         @"'/path/to/main app.app'", 
         @"--args", 
         @"/path/to/myXML.xml"
];

[task setArguments:[NSArray arrayWithObjects:[ @"-c", cmd, nil]];

[task launch];