# Registering/detecting an importer by file name extension

I've developed an importer and exporter for FreeSurfer's MGH/MGZ file format; the specific format isn't important, but I was wondering if there is a 'correct' way to tell Mathematica that file names ending with ".mgh" should be automatically imported using my MGH importer?

Mathematica obviously does this for its built-in importers (e.g., Import["abc.csv"] is equivalent to Import["abc.csv", "CSV"]), so it seems like it should be possible, but I haven't been able to find any information about this in Mathematica's documentation.

One solution, which seems inelegant to me, is this:

Unprotect[Import];
Import[name_String /; StringMatchQ[name, __ ~~ ".mgh" ~~ EndOfString]] :=
Import[name, "MGH"];
Import[name_String /; StringMatchQ[name, __ ~~ ".mgz" ~~ EndOfString]] :=
Import[name, {"GZip", "MGH"}];
Protect[Import];


• Have you read this tutorial? It details how to develop Import/Export converters. I don't know if it contains the information you're asking for, but it's the best place to look. – Szabolcs Jun 20 '14 at 15:41
• I've read all of those a couple times and they seem to be the only official source of info on getting forms like Import["abc.mgh", "MGH"] to work (using ImportExportRegisterImporter). Unless I missed something there is no information about the auto-detection of file type in there, however. – nben Jun 20 '14 at 15:47

I believe all you need to do is add your extension to the ExtensionMappings.m file in SystemConvertersDump$FormatsDirectory. • This does solve the problem locally for my machine; however, the importer is part of a package/library that I would like to distribute, and this solution would require each library user to both have root privilege and go mucking around in their Mathematica systems directories. I'm looking for something that can be done at the time the package is loaded. – nben Jun 20 '14 at 15:59 • It's helpful to specify those details :) Your own idea is probably the best option, then, unless you want to do something hacky like Block[{SystemConvertersDumpExtensionMappings=Join[{"*.myFormat"->"MYFORMAT"},Join@@SystemConvertersDumpGetExtensionMappings/@SystemConvertersDump$FormatsDirectory,SystemConvertersDumpExtensionMappings]}, SystemConvertersDumpExtensionMappings] – mfvonh Jun 20 '14 at 16:09
• Thanks, that is definitely closer to the kind of answer I was looking for, but it doesn't work. By the way, AppendTo[SystemConvertersDump`ExtensionMappings, "*.mgh" -> "MGH"] would seem to be sufficient, but either way that I do it, Import fails to infer the format of files ending with the extension. Also, how can I find more documentation on these namespaces? As far as I can tell these are not documented anywhere... – nben Jun 20 '14 at 16:27