-- script: Trying to get Legal Filename Extensions to open with chosen app
-- written by me, right now
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
my getApplicationTypeExtensions:"Atom"
on getApplicationTypeExtensions:appName
-- get chosen application's NSBundle
set theBundle to current application's NSBundle's bundleWithIdentifier:(id of application appName)
-- get document types info
set CFBundleDocumentTypes to (theBundle's infoDictionary()'s objectForKey:"CFBundleDocumentTypes")
-- get list of extensions
set theExtensions to {}
repeat with theDictionary in CFBundleDocumentTypes
set adding to (theDictionary's objectForKey:"CFBundleTypeExtensions") as list
if adding does not contain {missing value} then set theExtensions to theExtensions & adding
end repeat
return theExtensions
end getApplicationTypeExtensions:
That’s a bit of a roundabout way of getting the NSBundle instance for the application. The repeat loop is also not needed. Here’s a more condensed version of your code:
use framework "Foundation"
use framework "UniformTypeIdentifiers"
property text item delimiters : linefeed
on typeExtensionsForApplication:name
local id, name
set A to a reference to the application name
set id to A's id
tell the infoDictionary of my (NSBundle's bundleWithIdentifier:id)
tell the valueForKeyPath_(["CFBundleDocumentTypes", ¬
".@unionOfArrays", ".CFBundleTypeExtensions"] ¬
as text) as list to if it ≠ {} then return it
end typeExtensionsForApplication:
typeExtensionsForApplication_("Vivaldi") --> {}
However, it fails to return the type extensions for any application that doesn’t include the “CFBundleTypeExtensions” key in its info.plist file. Some applications specify a list of content types in the form of universal type identifiers, which are associated with the key named “LSItemContentTypes”. I’ve incorporated this into an extended version of the above handler, which maps the content types onto a filename extension and returns these:
use framework "Foundation"
use framework "UniformTypeIdentifiers"
property text item delimiters : linefeed
on typeExtensionsForApplication:name
local id, name
set A to a reference to the application name
set id to A's id
tell the infoDictionary of my (NSBundle's bundleWithIdentifier:id)
tell the valueForKeyPath_(["CFBundleDocumentTypes", ¬
".@unionOfArrays", ".CFBundleTypeExtensions"] ¬
as text) as list to if it ≠ {} then return it
tell the valueForKeyPath_("CFBundleDocumentTypes" & ¬
".@unionOfArrays.LSItemContentTypes")
repeat with contentType in (a reference to it)
tell the preferredFilenameExtension() ¬
of the typeWithIdentifier_(contentType) ¬
of the my UTType to if it ≠ missing value ¬
then set contentType's contents to it
end repeat
return it as list
end tell
end tell
end typeExtensionsForApplication:
typeExtensionsForApplication_("Vivaldi")
--> {"gif", "html", "xhtml", "js", "jpeg", "mht", ¬
--> "ogg", "ogm", "png", "svg", "public.text", ¬
--> "webm", "webp", "crx", "pdf"}
I updated my script in connection with this fair remark. I did not like your other recommendations.