NSTask Launch causing crash
Asked Answered
K

2

1

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?

Karakalpak answered 5/1, 2011 at 18:12 Comment(0)
E
0

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.

Egomania answered 7/1, 2011 at 18:9 Comment(1)
Sure enough. Adobe was spitting out stuff that got stuck/parsed. Im not a C developer but this is what i was told happened.Karakalpak
H
0

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];
Howenstein answered 19/1, 2011 at 9:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.