Determine the extensions that can open a chosen app.

Hello, everyone.

Currently I am trying to write the script which can determine the extensions that can open a chosen by the user application. I wrote following script, which works with some applications (like Calendar.app and Automator.app), and doesn’t work with other apps (like Finder.app).

Can anyone help me to make my script universal?


use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

-- get posix path of chosen application
set applicationPosixPath to POSIX path of (path to (choose application))
-- coerce to NSURL
set theURL to current application's |NSURL|'s fileURLWithPath:applicationPosixPath
-- get chosen application's NSBundle
set theBundle to current application's NSBundle's bundleWithURL:theURL
-- get document types info
set CFBundleDocumentTypes to (theBundle's infoDictionary()'s objectForKey:"CFBundleDocumentTypes") as list

-- get the list of extensions the chosen application opens
set theExtensions to {}
repeat with anItem in CFBundleDocumentTypes
	set end of theExtensions to item 1 of CFBundleTypeExtensions of anItem
end repeat

return theExtensions

OK,

I found what was the mistake. So, if legal filename extensions are indicated in the Info.plist file of the application bundle, then their list can be obtained as follows. If they are not specified, then the result will be an empty list:


-- script: Trying to get Legal Filename Extensions to open with chosen app
-- written by KniazidisR, right now

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

-- get posix path of chosen application
set applicationPosixPath to POSIX path of (path to (choose application))
-- coerce to NSURL
set theURL to current application's |NSURL|'s fileURLWithPath:applicationPosixPath
-- get chosen application's NSBundle
set theBundle to current application's NSBundle's bundleWithURL:theURL
-- get document types info
set CFBundleDocumentTypes to (theBundle's infoDictionary()'s objectForKey:"CFBundleDocumentTypes")

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

theExtensions