如何从Cocoa应用程序运行二进制文件?
问题描述:
我正在为一个二进制文件制作一个简单的启动器,所以我可以让它出现在我的启动板中。如何从Cocoa应用程序运行二进制文件?
我最初的想法是运行system("./myProgram");
就足够了,但它并没有真正做任何事情,因为它运行的终端实例在运行该命令后不会保持打开状态,而是立即关闭该程序执行的其他任务。
所以我的问题是,有没有办法让我这样做,使它无限期地打开?
编辑:我想我的启动器来启动程序后立即关闭,所以这将是不太理想依赖的东西,需要它来保持开放
编辑:以下所有的工作,但只有当运行在Xcode,运行它时单机它根本不会
system("open /Applications/Utilities/Terminal.app myProgram");
system("open myProgram");
system("/bin/sh -c ./myProgram&");
system("./myProgram&");
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath: @"/bin/bash"];
[task setArguments: @[@"-c", @"./myProgram"]];
[task launch];
NSTask
启动程序不给任何错误,并且或者当应用程序运行
从字面上看它不抛出任何异常其他每一个aspe该程序的作品,它只是不会启动,它不会说为什么
基于所有的“反馈”这里是我到目前为止。它仍然无法正常工作,除非我提供了一个绝对路径(这是在情况下,没有好,我想以后移动)
//
// AppDelegate.m
// DFLauncher
//
// Created by Electric Coffee on 11/02/15.
// Copyright (c) 2015 Electric Coffee. All rights reserved.
//
#import "AppDelegate.h"
@interface AppDelegate()
@property (weak) IBOutlet NSWindow *window;
@end
NSString *CURRENT_DIR;
NSString *FILE_PATH;
NSString *INIT_PATH = @"/data/init/init.txt";
NSString *VOLUME_ON = @"[SOUND:YES]";
NSString *VOLUME_OFF = @"[SOUND:NO]";
BOOL contains(NSString *a, NSString *b) {
return [a rangeOfString: b].location != NSNotFound;
}
NSData *replaceString(NSString *fileContents, NSString *from, NSString *to) {
return [[fileContents stringByReplacingOccurrencesOfString: from withString: to]
dataUsingEncoding: NSUTF8StringEncoding];
}
@implementation AppDelegate
- (void)applicationDidFinishLaunching: (NSNotification *)aNotification {
CURRENT_DIR = [[NSFileManager new] currentDirectoryPath]; //[[NSBundle mainBundle] bundlePath];
//NSLog(@"%@", CURRENT_DIR);
FILE_PATH = [CURRENT_DIR stringByAppendingString: INIT_PATH];
_fileContents = [NSString stringWithContentsOfFile: FILE_PATH
encoding: NSUTF8StringEncoding
error: NULL];
if (contains(_fileContents, VOLUME_OFF))
[_toggleMute setState: YES];
if (contains(_fileContents, VOLUME_ON))
[_toggleMute setState: NO];
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
}
- (IBAction)playButtonClick: (id)sender {
//system("open /Applications/Utilities/Terminal.app df"); // doesn't quite work
//system("open /Applications/df_osx/df");
//system("/bin/sh -c /Applications/df_osx/df&");
//system("/Applications/df_osx/df&");
NSString *gamePath = [CURRENT_DIR stringByAppendingString: @"/df&"];
NSTask *task = [NSTask new];
[task setLaunchPath: @"/bin/bash"];
[task setArguments: @[@"-c", gamePath]];
NSError *error = task.standardError;
[task launch];
[NSAlert alertWithError: error];
//[NSApp terminate: self];
}
- (IBAction)folderButtonClick: (id)sender {
system("open .");
}
- (IBAction)quitButtonClick: (id)sender {
[NSApp terminate: self];
}
- (IBAction)mute: (id)sender {
NSData *result;
NSFileManager *fm = [NSFileManager defaultManager];
if ([sender state] == NSOffState)
result = replaceString(_fileContents, VOLUME_OFF, VOLUME_ON);
else
result = replaceString(_fileContents, VOLUME_ON, VOLUME_OFF);
[fm createFileAtPath: FILE_PATH contents: result attributes: nil];
}
@end
答
哈克解决方案,它的工作原理(但不优雅可言)
我不得不更换
FILE_PATH = [CURRENT_DIR stringByAppendingString: INIT_PATH];
随着
CURRENT_DIR = [[[[NSBundle mainBundle] bundlePath] stringByDeletingPathExtension] stringByDeletingLastPathComponent];
要获取文件的正确路径,但它现在工作。
+0
是的,你需要做一些阅读。 – trojanfoe 2015-02-11 19:06:49
也许'NSWorkspace'和/或'NSTask'可以帮助你做到这一点? – Eimantas 2015-02-11 13:15:08
运行它通过使用系统的shell(“/ bin/sh -c ./myProgram &"); – 2015-02-11 13:38:30
它是什么样的二进制文件(控制台或可可应用程序)? – trojanfoe 2015-02-11 13:40:11