NSTask 启动导致崩溃

发布于 2024-10-10 16:17:22 字数 1830 浏览 7 评论 0原文

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

打开/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];

但这会导致与最后一个命令相同的结果。 关于导致这次崩溃的原因有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青瓷清茶倾城歌 2024-10-17 16:17:22

检查此处以修复 adobe 错误。但我不确定这就是问题所在。实际上我无法获得 open 命令来将参数传递给任何东西,所以我无法调查你的问题。

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.

倾其所爱 2024-10-17 16:17:22

仅使用完整路径再次尝试 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];

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文