Get UTCopyDeclaredTypeIdentifiers from the computer.

Here is a messy code of mine… to get UTI from the computer and return extension.
If anyone like to dig into it and make it better I will more and happy.

Some part use swift code and I’m not sure there is objective-c.

I’m very sorry for his messy code. :slight_smile:

use framework "Foundation"
use framework "AppKit"
use scripting additions

set theSwiftSelect to "
import Foundation
@_silgen_name(\"_UTCopyDeclaredTypeIdentifiers\") func UTCopyDeclaredTypeIdentifiers() -> CFArray

let UTIs = UTCopyDeclaredTypeIdentifiers()

set theSourceCode to theSwiftSelect
set thePath to POSIX path of (path to temporary items)
set theUTCopyDeclaredTypeIdentifiers to my runSwiftWithString:theSourceCode inPath:thePath

set theString to my removeString:"(" fromString:theUTCopyDeclaredTypeIdentifiers
set theString to my removeString:")" fromString:theString
set theString to my removeString:"," fromString:theString
set theString to my removeString:space fromString:theString
set theString to my removeString:"\"" fromString:theString
set theString to my removeString:"\"" fromString:theString
set theList to every paragraph of theString

set theArray to current application's NSArray's arrayWithArray:theList
set theList to (theArray's sortedArrayUsingSelector:"compare:") as list
set theList to items 3 thru -1 of theList
set theType to (choose from list theList) as text

set workspace to current application's NSWorkspace's sharedWorkspace()
return (workspace's preferredFilenameExtensionForType:theType) as text

on runSwiftWithString:theSource inPath:thePath
	set theSourceName to "source.swift"
	return (do shell script "echo " & quoted form of theSource & " > " & thePath & theSourceName & ";" & space & "swift " & thePath & theSourceName)
end runSwiftWithString:inPath:

on removeString:_removeString fromString:_sourceString
	set sourceString to current application's NSString's stringWithString:_sourceString
	set removeString to current application's NSString's stringWithString:_removeString
	return (sourceString's stringByReplacingOccurrencesOfString:removeString withString:"") as text
end removeString:fromString:

A simple AppleScript way of doing it without calling to a Swift script.

use framework "Foundation"
use scripting additions

set allDeclaredUTIsText to do shell script "/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/
Versions/A/Support/lsregister -dump | grep '[[:space:]]uti:' | awk '{ print $2 }' | sort | uniq" as text

set allDeclaredUTIsList to every paragraph of allDeclaredUTIsText as list

set chosenUTIType to choose from list allDeclaredUTIsList with title "All system declared UTI types"

if chosenUTIType is equal to false then
	return "User Canceled UTI Type Selection"
	set chosenUTITypeText to item 1 of chosenUTIType as text
	set workspace to current application's NSWorkspace's sharedWorkspace()
	return (workspace's preferredFilenameExtensionForType:chosenUTITypeText) as text
end if

The declared system UTI’s can be referenced from deep in the Bowles of the CoreServices.framework and LaunchServices.framework lsregister.

Just another option.

You may like to know that the NSWorkspace preferredFilenameExtensionForType: method has been marked for deprecation after Mac OS 11.

Regards Mark

Thanks Mark, the lsregister is an alias in /usr/local/bin on my Mojave.

Both yours and my postings are vulnerable code.
The “@_silgen_name("_UTCopyDeclaredTypeIdentifiers")” is a back door or shortcut for Apple’s own engineers to use when working on their own system frameworks, in this case the engineer “silgen” has forgotten to remove his footprint.
Their not meant to be there for you and me to use, and you won’t see these functions listed in the documentation, because these backdoors are meant to be commented out when the frameworks are released.
Someone stumbled across this one while looking into or reverse engineering Apple’s source code.

So in short you can not rely on that shortcut function to be there in the future.

But my code is also vulnerable, because if you study the latest documentation on the CoreServices.framework and contained LaunchServices.framework you will see the amount of deprecations planned for future releases are extensive, and may result in the “lsregister” being removed or heavily modified.

It’s all part of the Swiftifiying and deObjectiveCifiying of the core system frameworks.
So that’s the future, and we can only work with what we have today.

regards Mark

I find the code (swift) on stackoverflow and also this link: When I did some research on UTI and also after I read this file UTCoreTypes.h

ex. class NSimage:

set imageTypes to (current application's NSImage's imageTypes()) as list

Thanks for sharing.

Yeah, if it’s not in the official Apple developer documentation, then don’t rely on it for future longevity.

If your interested in all of the information within the “lsregister”, then type this into a “do shell script” command.

set allDeclaredUTIsText to do shell script "/System/Library/Frameworks/CoreServices.framework/
Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -dump"

Regards Mark