IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    [原]OSX:设置用户默认浏览器

    afatgoat发表于 2014-09-29 12:28:14
    love 0

    最近我们遇到的情况是,需要统一设置用户的默认浏览器为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




沪ICP备19023445号-2号
友情链接