Setting Default Application Handlers

One of the questions I get asked a lot is how to set default application handlers in Firefox (the Applications page in preferences). Most companies have resorted to creating a default mimeTypes.rdf file and loading this into their default profile. I finally took the time to understand how these handlers work and can give you some code to use.

   var handlerSvc = Components.classes["@mozilla.org/uriloader/handler-service;1"]
                              .getService(Components.interfaces.nsIHandlerService);
  var mimeService = Components.classes["@mozilla.org/mime;1"]
                              .getService(Components.interfaces.nsIMIMEService);
  // Change "image/tiff" the mime type you want to set the preference for
  var realMIMEInfo = mimeService.getFromTypeAndExtension("image/tiff", "");

  var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsIFile);
  // This should be the path to the .app file on Mac or the EXE on Windows
  file.initWithPath("/Applications/Preview.app");
  var localHandlerApp = Components.classes["@mozilla.org/uriloader/local-handler-app;1"].createInstance(Components.interfaces.nsILocalHandlerApp);
  localHandlerApp.executable = file;
  // The name that will be shown in preferences.
  // Not used on Mac
  localHandlerApp.name = "Preview";
 
  realMIMEInfo.preferredApplicationHandler = localHandlerApp;

  // This says to always use the helper app 
  realMIMEInfo.preferredAction = 2; // useHelperApp
  // This says not to ask
  realMIMEInfo.alwaysAskBeforeHandling = false;

  handlerSvc.store(realMIMEInfo);

You can add this code to your autoconfig file.

Please note: I reserve the right to delete comments that are offensive or off-topic.

Leave a Reply

Your email address will not be published. Required fields are marked *

14 thoughts on “Setting Default Application Handlers

  1. How do I tell Firefox that *it* can handle a mime type, possibly by loading a JS library, or handing it to a web site? E.g. open .zip files using jar: protocol, or various mime types that the browser can view reasonably well as text, and *especially* all the amazing JavaScript (cf. http://badassjs.com) that can render .midi files, video files, 3D files, camera RAW files, etc. in the browser?

    I use the Open in Browser extension and it sometimes helps, but for a browser ostensibly built around the web it’s quite hard and it seems users have to save somefile.EXT to their hard drive, Google for some privacy-sucking web site that hosts a viewer for .EXT files, upload the file, and only then can they see its contents.

    No doubt it’s a hard problem, but I hope it’s solvable (Firefox OS Web activities associated with the URLs of JS decoder libraries?).

  2. The example code appears to be missing a line:

    var mimeService = Components.classes[“@mozilla.org/mime;1”].getService(Components.interfaces.nsIMIMEService);

  3. Can a similar method be used to add a new uri type handler? The background for this idea is in http://bugs.debian.org/696508 , and the need to add support for the bitcoin: uri type. It is trivial in konqueror, but I have not seen a way to do it in firefox/iceweasel.

  4. I can’t seem to get it to work for application/sdp to open in VLC with Firefox 24 ESR.
    Here is my firefox.cfg file:

    //
    var handlerSvc = Components.classes[“@mozilla.org/uriloader/handler-service;1”].getService(Components.interfaces.nsIHandlerService);

    var mimeService = Components.classes[“@mozilla.org/mime;1”].getService(Components.interfaces.nsIMIMEService);

    // Change “image/tiff” the mime type you want to set the preference for
    var realMIMEInfo = mimeService.getFromTypeAndExtension(“application/sdp”, “”);

    var file = Components.classes[“@mozilla.org/file/local;1”].createInstance(Components.interfaces.nsIFile);
    // This should be the path to the .app file on Mac or the EXE on Windows
    file.initWithPath(“/Applications/VLC.app”);

    var localHandlerApp = Components.classes[“@mozilla.org/uriloader/local-handler-app;1”].createInstance(Components.interfaces.nsILocalHandlerApp);
    localHandlerApp.executable = file;

    // The name that will be shown in preferences.
    // Not used on Mac
    localHandlerApp.name = “VLC”;

    realMIMEInfo.preferredApplicationHandler = localHandlerApp;

    // This says to always use the helper app
    realMIMEInfo.preferredAction = 2;
    // This says not to ask
    realMIMEInfo.alwaysAskBeforeHandling = false;

    handlerSvc.store(realMIMEInfo);

    I know the cfg file is being read because it will add VLC to the drop down list in firefox preferences pane. But Quicktime plug-in is still checked as the default. I also tried changing the cfg above for application/pdf to use Preview.app and that worked, but not with sep files?

    Any help would be appreciated.
    Thanks,
    Jay

    • I haven’t forgotten about. I’m investigating this now.

      Dumb question. What is application/sdp? And does it show up in your Firefox list after running this? Was it there before?

      I don’t see application/sdp in my list before or after running this…

      • Thanks so much for looking into this, I appreciate it.

        We use sdp for streaming video. It describes the video feed to the player so it can properly play the stream. http://en.wikipedia.org/wiki/Session_Description_Protocol

        I don’t know much more about it, the video folks here handle the stream and they have decided that VLC is the best player for us. I am responsible for deploying VLC to the desktop (done) and configuring Firefox to use VLC instead of the Quicktime plug-ing. Configuring Firefox is where I’ve hit a snag.

        Yes I did have sdp in my list before making any changes to Firefox, it was associated with the Quicktime plug-in by default. But I did have VLC installed already.

        • OK, so I’m seeing different results on my machine. When I run your code, I get a new entry added called “Videos” that is associated with VLC. When I look in my mimeTypes.rdf, that does seem to be sdp:

          <RDF:Description RDF:about="urn:mimetype:externalApplication:application/sdp"
                             NC:prettyName="VLC"
                             NC:path="/Applications/VLC.app" />

          I think we’ll need to take this off the comments. Can you open a ticket at cck2.freshdesk.com and attach your mimeTypes.rdf file? It’s in your profile directory.