Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor UTI code #1681

Open
RhetTbull opened this issue Sep 13, 2024 · 0 comments
Open

Refactor UTI code #1681

RhetTbull opened this issue Sep 13, 2024 · 0 comments
Labels
refactoring Code that should be refactored

Comments

@RhetTbull
Copy link
Owner

RhetTbull commented Sep 13, 2024

For macOS 11+, the following should work to simplify the uti.py code:

import objc
from LaunchServices import UTTypeCreatePreferredIdentifierForTag, UTTypeCopyPreferredTagWithClass
from Foundation import NSString
import CoreServices
# import LaunchServices

def UTIForExtension(file_extension: str) -> str:
    if not file_extension:
        return None

    # Remove leading dot if included in the extension
    if file_extension.startswith('.'):
        file_extension = file_extension[1:]

    ext = NSString.stringWithString_(file_extension.lower())
    # uti = UTTypeCreatePreferredIdentifierForTag(LaunchServices.kUTTagClassFilenameExtension, ext, None)
    uti = UTTypeCreatePreferredIdentifierForTag(CoreServices.kUTTagClassFilenameExtension, ext, None)
    return str(uti) if uti else None


def PreferredExtensionForUTI(uti_string: str) -> str:
    if not uti_string:
        return None

    uti = NSString.stringWithString_(uti_string)
    preferred_ext = UTTypeCopyPreferredTagWithClass(uti, CoreServices.kUTTagClassFilenameExtension)
    return str(preferred_ext) if preferred_ext else None

# Example usage:
if __name__ == "__main__":
    uti = UTIForExtension(".jpg")
    print(f"UTI for .jpg: {uti}")  # Output should be: public.jpeg

    extension = PreferredExtensionForUTI("public.jpeg")
    print(f"Preferred extension for public.jpeg: {extension}")  # Output should be: jpeg

    uti = UTIForExtension(".mp4")
    print(f"UTI for .mp4: {uti}")  # Output should be: public.jpeg

    extension = PreferredExtensionForUTI("com.canon.cr2-raw-image")
    print(f"Preferred extension for com.canon.cr2-raw-image: {extension}")  # Output should be: jpeg
@RhetTbull RhetTbull added the refactoring Code that should be refactored label Sep 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactoring Code that should be refactored
Projects
None yet
Development

No branches or pull requests

1 participant