Objective-C로 작성한 내 OS X 응용 프로그램으로 파일을 여는 방법을 알고 싶습니다. Info.plist에 파일 형식을 등록했고 코드에 application:openFile:
이 있습니다. 나는 해결 된 것으로 표시되었던 this post에 의해 모든 것을했다.objective-c로 작성된 자신의 응용 프로그램으로 파일 열기
문제는 응용 프로그램이 실행되는 동안 내 파일을 끌어서 놓는 경우에만 작동한다는 것입니다. 하지만 파일을 두 번 클릭하면 작동하지 않습니다. 그것은 내 응용 프로그램을 시작하지만 끌어서 놓기 만하면 시작되지 않습니다. 따라서 application:openFile:
에있는 코드는 두 번 클릭 할 때 실행되지 않지만 파일을 드래그 앤 드롭 할 때만 실행됩니다.
편집 :
내 코드, 그리고 내가 무엇을 달성하기 위해 노력하고 대한 좀 더 세부 사항. 다른 앱에 대한 래퍼 애플리케이션을 만들었습니다. 다른 앱을 "HelperApp.app"라고 부르 자. 이 HelperApp는 내 래퍼 앱의 /Contents/
폴더 안에 있습니다. 래퍼 앱에서 새로운 파일 유형을 지정 했으니 Info.plist 파일에서 ".ha"라고 부르 자. 이 파일에는 몇 가지 인수 명령이 들어 있습니다. 사용자가 ".ha"파일 인 파일을 클릭하면 래퍼 응용 프로그램이이 파일의 인수를 읽고 HelperApp를 설정 한 다음 HelperApp를 시작합니다. 이 HelperApp는 인수에 따라 다른 것들을 열어 놓고 있습니다. 아래에서 내 코드를 확인할 수 있습니다.
나는 최신 Xcode가 어떻게 그것을 창조하는지 디폴트로 AppDelegate.h
및 AppDelegate.mm
가있다. 난 그냥 "@end"전에 내 AppDelegate.h
이 라인을 추가 :
- (BOOL)processFile:(NSString *)file;
나는 이러한 기능을 가지고 내 AppDelegate.mm
:
#import "AppDelegate.h"
#import "ArgumentParser.h"
@implementation AppDelegate
- (void)dealloc
{
[super dealloc];
}
- (BOOL)application:(NSApplication *)WrapperApp openFile:(NSString *)filename
{
return [self processFile:filename];
}
- (BOOL)processFile:(NSString *)file
{
NSLog(@"The following file has been dropped or selected: %@",file);
std::string path = [file cStringUsingEncoding:[NSString defaultCStringEncoding]];
ArgumentParser parser = ArgumentParser();
parser.getArgumentfromFile(path);
parser.setArgumentinFile(); // <== This is the "corrupted" function
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *helperAppPath = [[mainBundle bundlePath]
stringByAppendingString:@"/Contents/HelperApp.app"];
[[NSWorkspace sharedWorkspace] launchApplication:helperAppPath];
return YES;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
}
손상된 기능 - setArgumentinFile()
:
void ArgumentParser::setArgumentinFile() {
std::string content = ""; // The file content
std::fstream file;
file.open("HelperApp.app/Contents/Wrapper/HelperApp.app/Contents/Info.plist");
// Open the file and modify the arguments
if(file.is_open()) {
std::stringstream stream;
stream << file.rdbuf();
std::string line = "";
bool isIt = false;
while(getline(stream, line)) {
// This line is the argument list, which needs to be modifyed
if(isIt) {
int index = (int)line.find_last_of("<");
std::string start = line.substr(0, index);
std::string end = line.substr(index, std::string::npos);
std::string argument_list = start + " " + _argument + end;
content += argument_list + '\n';
isIt = false;
}
// Save the rest of the file so we can overwrite it
else {
content += line + '\n';
}
// Next line is the argument list
if(line.find("WineProgramArguments") != std::string::npos) {
isIt = true;
}
}
file.flush();
file.close();
}
else {
file.flush();
file.close();
throw std::runtime_error("File isn't opened");
}
file.open("HelperApp.app/Contents/Wrapper/HelperApp.app/Contents/Info.plist", std::ios::out);
// Open the file and overwrite it with the modifyed argument
if(file.is_open()) {
file << content;
file.flush();
file.close();
}
else {
file.flush();
file.close();
throw std::runtime_error("File isn't opened");
}
}
processFile
함수에서 위의 함수를 AppDelegate
에 주석 처리하면 모든 것이 "부드럽게"작동합니다. 래퍼 앱이 시작되고 HelperApp가 기본 인수로 시작된다는 의미입니다. 그래서 여기에 오류가 있어야합니다 ...
이전 사본을 실행 중입니다. 내 솔루션을 참조하십시오. –