Test whether file extension is in list of recognized extensions?

I’m trying to write a script that will do the following with a file:

  1. Get the name extension of the file.

  2. Test whether the name extension of the file is present in launch services’ list of extensions that it recognizes.

  3. If the extension of the file IS in launch services’ list, the script will add a new extension to the existing filename and extension.

So, for example, if a file is named

MySampleFile.TXT

the script will rename the file to

MySampleFile.TXT.newextension

(of course “newextension” is merely an example to show what I mean).

But if a file is named

MySampleText.strange

and “.strange” is not in launch services’ list of known extensions, then the script will NOT change the name of the file.

If anyone knows a way to create a list of all extensions known to launch services, I would be very grateful to have it.

Thank you.

Here’s a different approach you might take. I wrote a command line tool which returns the default application (from launch services) for a file extension amongst other things. So you could use that to query for the file extension directly rather than generating a list of all known extensions and checking your extension against that list.

See my tool DefaultApplication here. Basically if an extension is not known then you would get some error message stating that an application couldn’t be found.

That is exactly what I need - and I should have thought of it already, because I use your DefaultApplication in another one of my scripts. I simply didn’t think of using it here.

Thank you! This is perfect and, of course, a lot faster than extracting a full list of extensions…!