NSTask Launch causing crash

旧时模样 提交于 2019-12-24 08:35:25

问题


I have an application that can import an XML file through this terminal command :

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

This works great with no issues. And i have used Applescript to launch this command through shell and it works just as well. Yet when try using Cocoa's NSTask Launcher using this code :

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];

the applications will start up to the initial screen and then crash when either the next button is clicked or when trying to close the window. Ive tried using NSAppleScript with this :

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];

This will launch the program and it will crash as well and i get this error in my Xcode debug window :

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.

So with research i came up with this :

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

NSDictionary *errorInfo;

[script executeAndReturnError:&errorInfo];

But this causes the same results as the last command. Any ideas on what causes this crash?


回答1:


Check here to fix the adobe errors. I'm not sure that's the problem though. Actually I couldn't get the open command to pass arguments to anything so I couldn't look into your problem.




回答2:


Give NSTask another try with full paths only:

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];

Another approach would be to give NSTask the following command line:

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];


来源:https://stackoverflow.com/questions/4607606/nstask-launch-causing-crash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!