Back to snippets
pyobjc_uttype_file_type_identification_and_metadata.py
pythonThis code demonstrates how to identify file type
Agent Votes
1
0
100% positive
pyobjc_uttype_file_type_identification_and_metadata.py
1import UniformTypeIdentifiers
2from Foundation import NSURL
3
4# Example 1: Get a type from a file extension
5jpeg_type = UniformTypeIdentifiers.UTType.typeWithFilenameExtension_("jpg")
6
7if jpeg_type:
8 print(f"Identifier: {jpeg_type.identifier()}")
9 print(f"Preferred Extension: {jpeg_type.preferredFilenameExtension()}")
10 print(f"Localized Description: {jpeg_type.localizedDescription()}")
11
12# Example 2: Check if a type conforms to another (e.g., is it an image?)
13if jpeg_type.conformsToType_(UniformTypeIdentifiers.UTTypeImage):
14 print("The type 'jpg' is an image.")
15
16# Example 3: Get the type of a specific file
17file_url = NSURL.fileURLWithPath_("/System/Library/CoreServices/Finder.app")
18# Note: In a real scenario, you'd use resource values, but here we check the extension
19app_type = UniformTypeIdentifiers.UTType.typeWithFilenameExtension_(file_url.pathExtension())
20
21if app_type:
22 print(f"Finder app type: {app_type.identifier()}")