Macos Shell脚本作为默认浏览器
假设我有一个名为Macos Shell脚本作为默认浏览器,macos,Macos,假设我有一个名为my\u link\u opener的可执行文件,如何将其设置为默认浏览器 我已经尝试过以下方法,但没有成功: 将其包装为.app 我尝试使用生成的.app,然后将其设置为Safari中的默认浏览器。 但是,由于OSX希望程序在其生命周期中只有一个“实例”可以打开多个链接,因此这是行不通的。 它不在命令行上传递URL,而是发送一个-psn\uuxxxxx参数 用自动机包装它 事实证明,您可以将打开的文件作为参数。 但是,这仅适用于文件和文件夹。 就我所见,没有办法使URL成为自动
my\u link\u opener
的可执行文件,如何将其设置为默认浏览器
我已经尝试过以下方法,但没有成功:
将其包装为.app我尝试使用生成的.app,然后将其设置为Safari中的默认浏览器。
但是,由于OSX希望程序在其生命周期中只有一个“实例”可以打开多个链接,因此这是行不通的。
它不在命令行上传递URL,而是发送一个
-psn\uuxxxxx
参数
用自动机包装它事实证明,您可以将打开的文件作为参数。
但是,这仅适用于文件和文件夹。
就我所见,没有办法使URL成为自动机工作流的有效输入
所以,我运气不好。还有什么其他的包装魔术我可以做吗?我找不到任何已经存在的选项(但仍然有一些可能存在),所以我最后写了一个。 基本上,它所做的是告诉系统应用程序是一个浏览器,最终将自己设置为默认浏览器(请参见
#define set_as_default_browser
),并在收到正确事件时,通过将url发送到应用程序包中名为my_link_opener
的脚本来管理url(即,在.app目录中)。名为的bash命令是:
nohup/path/to/my\u link\u opener“\u an\u 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的能力):
CbundleUrlTypes
循环流化床锅炉方案
https
CFBundleURLName
安全http URL
循环流化床锅炉方案
http
CFBundleURLName
http URL
为什么不在Appify中包装另一个脚本,管理-psn\uxxxxx
选项,并为每个选项启动一个新的my\u link\u opener
实例?我想这是一个选项。还没有人这样做过吗?听起来需要学习的东西太多了。没问题。但我稍后会检查。哇,谢谢!你能提供一些吗关于如何编译它的要点?是的,我已经安装了XCode。我只是不确定这个项目应该是什么样子。只要创建一个新的mac应用程序项目,上面的代码就会放在appdelegate.m中,info.plist中的xml中。如果你不能使它工作,我会稍后在Github上更新它。对不起,我真的搞不清楚。它是构建的,但当我用它打开URL时,似乎什么也没有发生。如果你能提出任何建议来解决这个问题,我将不胜感激。我成功地将“nohup%@\“%@\”>&/dev/null&“改为更简单的”%@\“%@\”。也许>部分有问题?无论如何,谢谢你的帮助。
<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>