Difference between revisions of "Creating Ambient Filetypes"

From MorphOS Library

(Created page with "== About == == Examples == === Creating a Filetype for WMF files === #Open the [http://www.iana.org/assignments/media-types/ http://www.iana.org/assignments/media-types/] p...")
(No difference)

Revision as of 14:29, 8 March 2017

About

Examples

Creating a Filetype for WMF files

  1. Open the http://www.iana.org/assignments/media-types/ page (as suggested in the SYS:MorphOS/Ambient/recognition.db file), and look for the wanted media type.
  2. We can find wmf there and it seems to be categorized as image/wmf, let's click the image/wmf link in the second column on the page to get more information. We'll see its full name is Windows Metafile Format, its extension is .wmf (naturally), and magic numbers are D7 CD C6 9A, which is extremely useful information. Now we know all we need to know to create a new filetype.
  3. Ambient's filetype directory has the same hierarchy with the standard media type categories, so we can place our wmf filetype into the image directory. Let's create a new file there, for example from the shell: Ed SYS:Prefs/Ambient/filetypes/image/wmf
  4. Type in the following lines with the text editor.
    1. The standard headers which all filetypes should contain:
      AMTD
      1
    2. Then a line containing the type, which we got in the step 2:
      Type image/wmf
    3. And the name we also got in the step 2:
      Name Windows Metafile Format
    4. Then start the match block, which defines how the file is recognized:
      Match
    5. Let's add a pattern hint rule with the extension we know:
      PatternHint #?.wmf
    6. And then an actual matching rule. We got the magic numbers in the step 2 and they indicate hex numbers found from the beginning of each wmf file. We tell Ambient to look for those numbers to make the recognition. $ tells the searching will be done in the hex format.
      Match $D7CDC69A
    7. Now we can end the match block:
      End
    8. We also need another End to end the whole filetype definition:
      End
  5. Save the file and we're done. You can now open the Ambient MIME type settings and add new actions for the new filetype there.


Note: the inner block can be made indented to make it look cleaner, here's the whole filetype definition:

AMTD
1
Type image/wmf
Name Windows Metafile Format
Match
	PatternHint #?.wmf
	Match $D7CDC69A
	End
End


Creating a Filetype for CGM files

Let's try the same for the CGM filetype then. The page in the step 1 tells us that its name is Computer Graphics Metafile and its type is image/cgm. Unfortunately it doesn't seem to tell more about the recognition, and a quick googling reveals that files may contain the string BegMF, but not always.

We have to create a rule which will work even if that string isn't found, and the only reasonable way seems to be to check the filename in this case. So, let's create a rule which first tries to look if the known string is found inside a file (add s in front of the string to indicate it's a plain text string instead of hex or something else) or if it's not found, make the recognition by the extension:

Match sBegMF
OR
Name #?.cgm

You could also make matching only by the extension, but it's more elegant to match the contents when you can.

The full filetype for the cgm files would be like this then:

AMTD
1
Type image/cgm
Name Computer Graphics Metafile
Match
	PatternHint #?.cgm
	Match sBegMF
	OR
	Name #?.cgm
	End
End