1
1
""" get UTI for a given file extension and the preferred extension for a given UTI
2
2
3
- On macOS <= 11 (Big Sur), uses objective C CoreServices methods
4
- UTTypeCopyPreferredTagWithClass and UTTypeCreatePreferredIdentifierForTag to retrieve the
3
+ On macOS <= 11 (Big Sur), uses objective C CoreServices methods
4
+ UTTypeCopyPreferredTagWithClass and UTTypeCreatePreferredIdentifierForTag to retrieve the
5
5
UTI and the extension. These are deprecated in 10.15 (Catalina) and no longer supported on Monterey.
6
6
7
- On Monterey, these calls are replaced with Swift methods that I can't call from python so
8
- this code uses a cached dict of UTI values. The code first checks to see if the extension or UTI
9
- is available in the cache and if so, returns it. If not, it performs a subprocess call to `mdls` to
10
- retrieve the UTI (by creating a temp file with the correct extension) and returns the UTI. This only
11
- works for the extension -> UTI lookup. On Monterey, if there is no cached value for UTI -> extension lookup,
7
+ On Monterey, these calls are replaced with Swift methods that I can't call from python so
8
+ this code uses a cached dict of UTI values. The code first checks to see if the extension or UTI
9
+ is available in the cache and if so, returns it. If not, it performs a subprocess call to `mdls` to
10
+ retrieve the UTI (by creating a temp file with the correct extension) and returns the UTI. This only
11
+ works for the extension -> UTI lookup. On Monterey, if there is no cached value for UTI -> extension lookup,
12
12
returns None.
13
13
14
14
Outside of macOS uses only the hardcoded list of UTIs.
19
19
from __future__ import annotations
20
20
21
21
import csv
22
+ import logging
22
23
import os
23
24
import pathlib
24
25
import re
31
32
import CoreServices
32
33
import objc
33
34
35
+ logger = logging .getLogger ("osxphotos" )
36
+
34
37
__all__ = ["get_preferred_uti_extension" , "get_uti_for_extension" , "get_uti_for_path" ]
35
38
36
39
# cached values of all the UTIs (< 6 chars long) known to my Mac running macOS 10.15.7
229
232
odt,org.oasis-open.opendocument.text,odt,application/vnd.oasis.opendocument.text
230
233
omf,com.avid.open-media-framework,omf,None
231
234
orf,com.olympus.raw-image,orf,None
235
+ orf,com.olympus.or-raw-image,orf,None
232
236
otc,public.opentype-collection-font,otc,None
233
237
otf,public.opentype-font,otf,None
234
238
otg,org.oasis-open.opendocument.graphics-template,otg,application/vnd.oasis.opendocument.graphics-template
@@ -576,10 +580,16 @@ def _get_ext_from_uti_dict(uti):
576
580
return None
577
581
578
582
579
- def get_preferred_uti_extension (uti : str ) -> str | None :
580
- """get preferred extension for a UTI type
581
- uti: UTI str, e.g. 'public.jpeg'
582
- returns: preferred extension as str or None if cannot be determined"""
583
+ def get_preferred_uti_extension (uti : str ) -> str :
584
+ """Get preferred extension for a UTI type
585
+
586
+ Args:
587
+ uti: UTI str, e.g. 'public.jpeg'
588
+
589
+ Returns: preferred extension as str or empty string if extension cannot be determined
590
+
591
+ Note: Logs a warning if extension cannot be determined.
592
+ """
583
593
584
594
if is_macos and (OS_VER , OS_MAJOR ) <= (10 , 16 ):
585
595
# reference: https://developer.apple.com/documentation/coreservices/1442744-uttypecopypreferredtagwithclass?language=objc
@@ -595,9 +605,13 @@ def get_preferred_uti_extension(uti: str) -> str | None:
595
605
if uti == "public.heic" :
596
606
return "heic"
597
607
598
- return None
608
+ logger .warning (f"Could not determine extension for UTI: { uti } " )
609
+ return ""
599
610
600
- return _get_ext_from_uti_dict (uti )
611
+ ext = _get_ext_from_uti_dict (uti ) or ""
612
+ if not ext :
613
+ logger .warning (f"Could not determine extension for UTI: { uti } " )
614
+ return ext
601
615
602
616
603
617
def get_uti_for_extension (extension : str ) -> str | None :
0 commit comments