File extension associations with applications.

Questions about applications and software
Forum rules
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
halfshavedyaks
Level 1
Level 1
Posts: 36
Joined: Sat Dec 08, 2018 12:32 pm

File extension associations with applications.

Post by halfshavedyaks »

I need to associate different file extensions with different apps for opening the files.

There are many many different types of text files, the contents are all essentially plain text, but they have different purposes and need to be opened in different apps.

It isn't reasonable in this case to expect the OS to correctly analyse the file when it may be something obscure and there are probably hundreds of different types of text files

In any case I want it to take my word for the type of file it is and rely on the extension rather than attempt a file analysis.

Specifically I want to associate the extension .scad with Openscad (which I run from an appimage) but not affect any other plain text files with no extension or different extensions.

If I change a .scad file in properties to open with Openscad as default then ALL plain text files attempt to open with Openscad from then on.

Similarly I want to associate whatever other extensions with specific apps even when the contents are all just plain text.

I have looked in ~/.config/mimeapps.list but I have not had any success editing that manually and I can't find a guide to the syntax.

The whole issue seems very confused between trusting a file extension to identify the file type and analyzing the file. I want it to rely on the extension by preference and only revert to contents analysis if there is no extension ( or maybe if it is is obviously incorrect such as a binary file with a .txt suffix)

I'm using Mint 19 Cinnamon
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
Flemur
Level 20
Level 20
Posts: 10096
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: File extension associations with applications.

Post by Flemur »

halfshavedyaks wrote: Sat Dec 08, 2018 12:47 pmThe whole issue seems very confused between trusting a file extension to identify the file type and analyzing the file.
"Confused" is exactly right. The mime implementation is a complicated mess.
I want it to rely on the extension by preference and only revert to contents analysis if there is no extension ( or maybe if it is is obviously incorrect such as a binary file with a .txt suffix)
That would be too easy.

I think you need to create a new "mime type", like text/scad; here are some amazingly poorly written instructions:
https://help.ubuntu.com/community/AddingMimeTypes

About half-way down ("Another way to add a MIME Type") it says the instructions up to that point don't work. They didn't work for me, so ignore the top half.

And you're in luck - he's adding the "scad" file type!

Edit: I tried the "Another way" and it didn't work either; the " update-mime-database" command deletes the scad.xml file.

This worked:
In the file

Code: Select all

/usr/share/mime/packages/freedesktop.org.xml
I added

Code: Select all

#NEW 1
  <mime-type type="application/scad">

    <comment>Scad</comment>
    <acronym>Scad</acronym>
    <expanded-acronym>Scad</expanded-acronym>
    <sub-class-of type="text/plain"/>
    <glob pattern="*.scad"/>
  </mime-type>
#NEW 2
after "TriG RDF" type (about line 6438), so it'd be after other "text" values.
Then run

Code: Select all

sudo update-mime-database /usr/share/mime
And now a regular text file aa.scad opens with abiword and aa.txt opens with leafpad by default (just double-click)

Free advice: make a backup copy of freedesktop.org.xml before you edit it!

More free advice:
I made the association with aa.scad by file-browser->rt-click->open with (choose, e.g. abiword) -> set as default , and in

Code: Select all

.config/mimeapps.list
this line is under [Default...] AND under [Added...]

Code: Select all

application/scad=abiword.desktop;
A regular text file opens with leafpad.
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?
User avatar
Flemur
Level 20
Level 20
Posts: 10096
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: File extension associations with applications.

Post by Flemur »

I think the second part of that ubuntu "help" might work - IF YOU PUT THE XML FILE IN
/usr/share/mime/packages
and NOT where they said to:
/usr/share/mime/application,
where it will get silently erased (the files there say "automatically generated, don't edit")
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?
halfshavedyaks
Level 1
Level 1
Posts: 36
Joined: Sat Dec 08, 2018 12:32 pm

Re: File extension associations with applications.

Post by halfshavedyaks »

wow thanks - your method worked. (I haven't looked at the link to the ubuntu help)

I just added the definition you gave at the end of the freedesktop file so I can find it again.

there's 40k lines in my freedesktop file, much of it much more niche than openscad and with support for loads of languages I will never use.

seems like some optimisation might be in order.

when I updated I got this:

Code: Select all

sudo update-mime-database /usr/share/mime
Unknown media type in type 'all/all'
Unknown media type in type 'all/allfiles'
not sure what that is supposed to mean, but the file association now seems to work and I assume I can use the same process for other file types.
Locked

Return to “Software & Applications”