Uses for Uniform Type Identifier?

Hi,

Can someone tell me what can be done with the uti of a file. Here I used TextEdit and Tex-Edit Plus rtf files:

set f_list to choose file with multiple selections allowed
set uti_list to {}
repeat with this_f in f_list
	tell application "System Events"
		set end of uti_list to type identifier of this_f
	end tell
end repeat
uti_list
--> {"public.rtf", "com.apple.traditional-mac-plain-text"}
-- "TextEdit" and "Tex-Edit Plus"

Thanks,
kel

Think I have one use for getting the uti. If you’re making an app and you want the app to be able to open that file, then you add the uti to the plist. Is there anything else you can do?

Edited: I guess you could also find out if an application can open that file.

Thanks,
kel

What excactly do you mean. You can use lsregister command line to look the binding between utis and apps.

Hello kel1

On my side, I use type identifiers in this kind of instruction :


choose file of type{"public.plain-text"} # TextEdit text documents
choose file of type{ "public.rtf", "public.rtfd"}
choose file of type{"com.apple.iwork.pages.pages"}
choose file of type{"com.apple.iwork.numbers.numbers","com.apple.iWork.Numbers.sffnumbers","com.apple.iWork.Numbers.numbers-tef"}
choose file of type{"com.adobe.pdf"}

Yvan KOENIG (VALLAURIS, France) dimanche 28 septembre 2014 22:07:55

Hi Yvan,

That is very useful!!! I have never notice the ‘of type’ option for ‘choose file’ in this new system.

Thanks a lot, :smiley:
kel

Hi Yvan,

I just thought of something. Someone could identify files from a certain application using ‘of type’ in the ‘choose file’ command especially if the type identifier is unique or rare.

Thanks a lot,
kel

i.e. you can get all files made by a certain application using something like this:

set F to choose folder
tell application "System Events"
	every file of entire contents of F whose type identifier is some_type_id
end tell

Excellent! I didn’t know this.

Thanks a lot,
kel

much, much, much, much . much faster using Spotlight


set inputFolder to POSIX path of (choose folder)
set some_type_id to "public.rtf"
paragraphs of (do shell script "mdfind -onlyin " & quoted form of inputFolder & " 'kMDItemContentType  = \"" & some_type_id & "\"'")

PS: entire contents in System Events has a different meaning as in Finder

Hi Stefan,

Yes that is much better. In fact, I cannot use Finder’s ‘entire contents’ with System Event’s ‘type identifier’ without merging the tell blocs somehow. And, I don’t like that anyway if it could be done. The script is quick!

I have never seen the kMDItemContentType. Is that part of the mdfind? I need to look at the man page still.

Thanks a lot,
kel

I need to search for the kMD things. There must be a list somewhere. :slight_smile:

For the record: It isn’t new, it’s introduced in Tiger, just like UTI itself.

Hi DJ,

Darn, that was just after Jaguar. I knew I was missing out on things back then.

Thanks a lot,
kel

p.s. I found the list I think:
https://developer.apple.com/library/mac/documentation/Carbon/Reference/MDItemRef/Reference/reference.html

There is one: File Metadata Attributes Reference: Spotlight Metadata Attributes

Great stuff Stefan. That’s what I’ve been looking for for several weeks.

Thanks for the great direction. You know, you have an uncanny ability to fathom what people don’t know what they are trying to find!

Thanks a lot,
kel