2014-05-23 4 views
2

my_link_opener이라는 실행 파일이 있다고 가정 해 봅시다. 어떻게 기본 브라우저로 설정합니까? 성공없이쉘 스크립트를 기본 브라우저로 사용

이미 다음을 시도

:

내가 Appify를 사용하여 시도
.APP로 포장 한 다음 Safari에서 내 기본 브라우저로 .APP 생성 설정.
그러나 OS X은 프로그램이 평생 동안 많은 링크를 여는 하나의 "인스턴스"만 가질 것으로 예상하기 때문에 제대로 작동하지 않습니다.
URL을 명령 줄에 전달하는 대신 -psn_xxxxx 인수를 보냅니다.

당신은 인수로 연 파일을 확인
이 밝혀 자동화와 함께 할 수 if you add it as a workflow in Automator 포장.
그러나 이는 파일과 폴더에만 적용됩니다..
필자가 보았 듯이 URL을 Automator 워크 플로에 대한 유효한 입력으로 만들 수있는 방법은 없습니다.

그래서 나는 운이 없네. 내가 할 수있는 배치 마법의 다른 종류가 있습니까?

+0

왜'-psn_xxxxx' 옵션을 관리하고 그 각 옵션의'my_link_opener'의 새로운 인스턴스를 시작 Appify에 싸여 다른 스크립트를 가지고 있지? –

+0

그게 옵션이라고 생각합니다. 아무도 이미 해본 적이 없습니까? 배우는 것이 무척 힘듭니다. – Linus

+0

AFAIK 아니요. 하지만 나중에 확인하겠습니다 –

답변

1

이미 하나의 옵션을 찾을 수 없었지만 (여전히 일부는 존재할 수 있습니다) 그래서 하나를 작성했습니다. 기본적으로 앱에 브라우저가 기본 브라우저 (#define SET_AS_DEFAULT_BROWSER 참조)로 설정되고 마지막으로 URL을 이라는 스크립트에 전송하여 올바른 이벤트를 수신하면 URL을 관리합니다. 앱의 번들 (즉, .app 디렉토리 내부). 라는 bash는 명령은 다음과 같습니다

nohup /path/to/my_link_opener "_an_url_" >& /dev/null &

#import "ZFTAppDelegate.h" 

NSString *runCommand(NSString *commandToRun) 
{ 
    NSTask *task; 
    task = [[NSTask alloc] init]; 
    [task setLaunchPath: @"/bin/sh"]; 

    NSArray *arguments = @[@"-c" ,[NSString stringWithFormat:@"%@", commandToRun]]; 
    [task setArguments: arguments]; 

    NSPipe *pipe; 
    pipe = [NSPipe pipe]; 
    [task setStandardOutput: pipe]; 

    NSFileHandle *file; 
    file = [pipe fileHandleForReading]; 

    [task launch]; 

    NSData *data; 
    data = [file readDataToEndOfFile]; 

    NSString *output; 
    output = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; 
    return output; 
} 

#define SET_AS_DEFAULT_BROWSER 

@implementation ZFTAppDelegate 

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{ 
    NSAppleEventManager *em = [NSAppleEventManager sharedAppleEventManager]; 
    [em setEventHandler:self 
      andSelector:@selector(getUrl:withReplyEvent:) 
      forEventClass:kInternetEventClass 
      andEventID:kAEGetURL]; 
    [em setEventHandler:self 
      andSelector:@selector(getUrl:withReplyEvent:) 
      forEventClass:'WWW!' 
      andEventID:'OURL']; 


#ifdef SET_AS_DEFAULT_BROWSER 
    CFStringRef bundleID = (__bridge CFStringRef)[[NSBundle mainBundle] bundleIdentifier]; 
    LSSetDefaultHandlerForURLScheme(CFSTR("http"), bundleID); 
    LSSetDefaultHandlerForURLScheme(CFSTR("https"), bundleID); 
#endif 


} 

- (void)getUrl:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent 
{ 
    NSString *url = [[event paramDescriptorForKeyword:keyDirectObject] stringValue]; 
    NSString* scriptPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"my_link_opener"]; 
    runCommand([NSString stringWithFormat:@"nohup %@ \"%@\" >& /dev/null &", scriptPath, url]); 
} 

@end 

이 응용 프로그램의 Info.plist 파일에 그 기억 당신이해야이이 (HTTP/s의 URL을 처리하기 위해 응용 프로그램의 기능에 대해 OS를 알릴 필요) :

<key>CFBundleURLTypes</key> 
<array> 
    <dict> 
     <key>CFBundleURLSchemes</key> 
     <array> 
      <string>https</string> 
     </array> 
     <key>CFBundleURLName</key> 
     <string>Secure http URL</string> 
    </dict> 
    <dict> 
     <key>CFBundleURLSchemes</key> 
     <array> 
      <string>http</string> 
     </array> 
     <key>CFBundleURLName</key> 
     <string>http URL</string> 
    </dict> 
</array> 
+0

와우, 고마워! 당신은 이것을 컴파일하는 방법에 대한 몇 가지 포인트를 제공해 줄 수 있습니까? – Linus

+0

xcode를 설치할 수 있습니까? –

+0

예, 이미 XCode가 설치되어 있습니다. 프로젝트가 어떻게 생겼는지 확실하지 않습니다. – Linus

관련 문제