OSX:设置用户默认浏览器

近期我们遇到的情况是,须要统一设置用户的默认浏览器为Google Chrome。而系统默认的是Safari。

这个设置是系统Launch Services基于用户管理的。

意思就是说,即便是改动了系统全局參数,假设用户有特定设置。那么会依从用户配置。



仅仅要一设计用户配置,那么就会相对麻烦点。要想改变。会涉及多种用户情况,比方:网络用户目录的情况。用户的配置信息都在server上,所以配置须要在server上改动;假设用户目录保存在本地,那么能够有两种对策,1是:系统默认用户目录模板须要改变。并且须要遍历并改变已登录用户的全部;2是:部署一个用户级别的launchagent服务,每当一个用户登陆后,都会执行一个程序来完毕设置。那么对于移动用户。特别是可能不知道什么时候才干连接到公司网络的情况,就须要一个终端部署管理系统,比方JAMF的或者免费得Munki等等。


上面的措施基本上能够解决差点儿全部的工作站的情况和用户配置情况,关键是怎样解决:


假设是Google Chrome,似乎非常easy,由于Chrome支持一个内部命令:

open -a "Google Chrome" --args --make-default-browser

只是要是针对其它浏览器就无效了,须要其它方法

一个是使用以下的python脚本:

#/usr/bin/env python

# ------------------------------------------------------
# Set default web browser app
# 
# org from: https://gist.github.com/miketaylr/5969656
#
# enhenced version 1.1 by Tony Liu
# ------------------------------------------------------

from LaunchServices import LSSetDefaultHandlerForURLScheme
from LaunchServices import LSSetDefaultRoleHandlerForContentType
import sys

webApp=sys.argv[1]

# 0x00000002 = kLSRolesViewer
# see https://developer.apple.com/library/mac/#documentation/Carbon/Reference/LaunchServicesReference/Reference/reference.html#//apple_ref/c/tdef/LSRolesMask

LSSetDefaultRoleHandlerForContentType("public.html", 0x00000002, webApp)
LSSetDefaultRoleHandlerForContentType("public.xhtml", 0x00000002, webApp)
LSSetDefaultRoleHandlerForContentType("public.url", 0x00000002, webApp)
LSSetDefaultHandlerForURLScheme("http", webApp)
LSSetDefaultHandlerForURLScheme("https", webApp)

使用方法比方设置Safari为默认的,那么就是

python /path/to/setDefaultBrowser.py com.apple.Safari
同理。Google Chrome是

python /path/to/setDefaultBrowser.py com.google.chrome

另外一个方式就是使用开源工具duti

下载编译非常easy,之后执行命令:

duti com.google.chrome.canary public.html all
duti com.google.chrome.canary public.xhtml all
duti com.google.chrome.canary public.url all
duti com.google.chrome.canary http
duti com.google.chrome.canary https

另外另一个人做了一个单独的app, 叫defaultbrowser

參考技术文档:Launch Services


posted @ 2017-05-07 16:39  yfceshi  阅读(348)  评论(0编辑  收藏  举报