Thursday, February 25, 2021

#1 2021-01-08 07:22:12 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 625

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. smile

Applescript:

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

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

let UTIs = UTCopyDeclaredTypeIdentifiers()
print(UTIs)
"


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:

Last edited by Fredrik71 (2021-01-08 07:35:31 am)


The purpose to study someone else art is not to add, its to make less more.

Offline

 

#2 2021-01-08 01:42:28 pm

Mark FX
Member
From:: UK
Registered: 2011-08-12
Posts: 102

Re: Get UTCopyDeclaredTypeIdentifiers from the computer.

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

Applescript:


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"
else
   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

Last edited by Mark FX (2021-01-08 01:55:34 pm)

Offline

 

#3 2021-01-08 03:48:27 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 625

Re: Get UTCopyDeclaredTypeIdentifiers from the computer.

Mark FX wrote:

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


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


The purpose to study someone else art is not to add, its to make less more.

Offline

 

#4 2021-01-08 04:45:49 pm

Mark FX
Member
From:: UK
Registered: 2011-08-12
Posts: 102

Re: Get UTCopyDeclaredTypeIdentifiers from the computer.

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

Offline

 

#5 2021-01-08 05:29:38 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 625

Re: Get UTCopyDeclaredTypeIdentifiers from the computer.

I find the code (swift) on stackoverflow and also this link:
https://stackoverflow.com/questions/350 … t-language When I did some research on UTI and also after I read this file UTCoreTypes.h

ex. class NSimage:

Applescript:

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

Thanks for sharing.

Last edited by Fredrik71 (2021-01-08 05:30:42 pm)


The purpose to study someone else art is not to add, its to make less more.

Offline

 

#6 2021-01-08 05:56:44 pm

Mark FX
Member
From:: UK
Registered: 2011-08-12
Posts: 102

Re: Get UTCopyDeclaredTypeIdentifiers from the computer.

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.

Applescript:


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

Regards Mark

Last edited by Mark FX (2021-01-08 06:07:34 pm)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)