码迷,mamicode.com
首页 > 其他好文 > 详细

OSX:设置用户默认浏览器

时间:2014-09-29 15:09:21      阅读:307      评论:0      收藏:0      [点我收藏+]

标签:launch service   python   duti   defaul browser   chrome   

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

这个设置是系统Launch Services基于用户管理的。意思就是说,即便是修改了系统全局参数,如果用户有特定设置,那么会依从用户配置。

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


上面的措施基本上可以解决几乎所有的工作站的情况和用户配置情况,关键是如何解决:


如果是Google Chrome,似乎很简单,因为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。下载编译很简单,之后运行命令:
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


OSX:设置用户默认浏览器

标签:launch service   python   duti   defaul browser   chrome   

原文地址:http://blog.csdn.net/cneducation/article/details/39667953

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!