【问题标题】:Mac OS cocoa app call command line tool fail in 10.15Mac OS 可可应用程序调用命令行工具在 10.15 中失败
【发布时间】:2021-02-23 12:26:12
【问题描述】:

我想创建一个可可应用程序来调用使用 c++ 编写的命令行工具。 使用 Xcode 调用命令行工具时我可以做得很好。 但是当我关闭 Xcode 时它失败了,双击应用程序并按更新按钮 运行命令行工具。 我已经厌倦了使用 NSTask 调用命令行工具,但它仍然失败。 这是我的代码。

- (IBAction)Update:(id)sender {

[self performSelectorOnMainThread:@selector(IspUpdate) withObject:nil waitUntilDone:YES];
}

-(void)IspUpdate {
strCurDir = [[NSBundle mainBundle] bundlePath];
NSRange range = [strCurDir rangeOfString:@"/" options:NSBackwardsSearch];
NSRange rangeDir = NSMakeRange(0, range.location + 1);
strCurDir = [strCurDir substringWithRange:rangeDir];

NSString *strCmd = [strCurDir stringByAppendingString:@"ISPTool --isp --fw fw.bin --comm 6"];


dispatch_async(dispatch_get_global_queue(QOS_CLASS_UTILITY, 0), ^{

system([strCmd UTF8String]);
});
}

【问题讨论】:

  • 显示您尝试过的使用Process (NSTask) 类的代码。 Process 类是您从 Cocoa 应用程序运行命令行程序的方式。使用此代码更新您的问题以及尝试调用命令行程序时出现的错误。
  • 1.通过在 appBundle/Contents/MacOS 中打开可执行文件来启动您的应用程序,其中 launchPath 为 /usr/bin/open,后跟可执行文件路径的 args。 2. 然后使用更新按钮,这次使用 /bin/sh 的启动路径运行另一个 NSTask,将命令添加到第一个终端窗口。

标签: xcode macos cocoa macos-catalina xcode-command-line-tools


【解决方案1】:

请运行此演示,看看它是否与您尝试执行的操作相似。该代码将允许您通过单击按钮在应用程序的终端窗口中运行 NSTask。将以下代码保存在名为“runCmd.m”的文件中,然后使用以下说明从命令行编译:

/*
 Run from Terminal using: clang runCmd.m -fobjc-arc -framework Cocoa -o runCmd && ./runCmd
 Should print current calendar when 'RunCommand' button is hit.
*/

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate> {
 NSWindow *window;
}
 -(void) runMyCmd;
 -(void) buildMenu;
 -(void) buildWindow;
@end

@implementation AppDelegate

- (void) runMyCmd {
 NSTask *task = [[NSTask alloc] init];
 [task setLaunchPath: @"/bin/sh"]; 
 NSArray *args = [NSArray arrayWithObjects: @"-c", @"cal", nil];
 [task setArguments: args];
 NSPipe *pipe = [NSPipe pipe];
 [task setStandardOutput: pipe];
 [task launch];
 [task waitUntilExit];
 NSData *data = [[pipe fileHandleForReading] readDataToEndOfFile];
 NSString *string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
 NSLog (@"\n%@",string);
}

- (void) buildMenu {
 NSMenu *menubar = [NSMenu new];
 NSMenuItem *menuBarItem = [NSMenuItem new];
 [menubar addItem:menuBarItem];
 [NSApp setMainMenu:menubar];
 NSMenu *appMenu = [NSMenu new];
 NSMenuItem *quitMenuItem = [[NSMenuItem alloc] initWithTitle:@"Quit"
 action:@selector(terminate:) keyEquivalent:@"q"];
 [appMenu addItem:quitMenuItem];
 [menuBarItem setSubmenu:appMenu];
}

- (void) buildWindow {

 #define _wndW  200
 #define _wndH  150

 window = [[NSWindow alloc] initWithContentRect: NSMakeRect( 0, 0, _wndW, _wndH )
 styleMask: NSWindowStyleMaskTitled | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskClosable | NSWindowStyleMaskResizable
 backing: NSBackingStoreBuffered defer: NO];
 [window center];
 [window setTitle: @"Test window"];
 [window makeKeyAndOrderFront: nil];

// **** RunCmdButton **** //
 NSButton *runBtn =[[NSButton alloc]initWithFrame:NSMakeRect( 30, 60, 135, 30 )];
 [runBtn setBezelStyle:NSBezelStyleRounded ];
 [runBtn setTitle: @"RunCommand"];
 [runBtn setAction: @selector(runMyCmd)];
 [[window contentView] addSubview: runBtn];

// **** Quit btn **** //
 NSButton *quitBtn = [[NSButton alloc]initWithFrame:NSMakeRect( _wndW - 50, 5, 40, 40 )];
 [quitBtn setBezelStyle:NSBezelStyleCircular ];
 [quitBtn setTitle: @"Q" ];
 [quitBtn setAutoresizingMask: NSViewMinXMargin];
 [quitBtn setAction:@selector(terminate:)];
 [[window contentView] addSubview: quitBtn];
}

- (void) applicationWillFinishLaunching: (NSNotification *)notification {
 [self buildMenu];
 [self buildWindow];
}

- (void) applicationDidFinishLaunching: (NSNotification *)notification {
}

@end

int main () {
 NSApplication *application = [NSApplication sharedApplication];
 AppDelegate *appDelegate = [[AppDelegate alloc] init];
 [application setDelegate:appDelegate];
 [application run];
 return 0;
}


【讨论】:

    猜你喜欢
    • 2012-08-03
    • 1970-01-01
    • 2015-06-11
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    相关资源
    最近更新 更多