Last active
October 17, 2022 17:51
-
-
Save dreness/54e32b54acb559a41abc29ae824bcd16 to your computer and use it in GitHub Desktop.
Interrogate LaunchServices using #PyObjC to query file paths for default app handler, all possible app handlers, and the UTI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| xomg% python LSFileInfo.py dylib-map.sqlite | |
| ('\n', u'/Users/andre/bin/dylib-map.sqlite') | |
| ('UTI: ', u'dyn.ah62d4rv4ge81g6pqrf4gn') | |
| ('default app: ', file:///Applications/DB%20Browser%20for%20SQLite.app/) | |
| ('all apps: ', ( | |
| "file:///Applications/DB%20Browser%20for%20SQLite.app/" | |
| )) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import LaunchServices as ls | |
| from Foundation import NSURL | |
| from sys import argv | |
| for arg in argv[1:]: | |
| url = NSURL.fileURLWithPath_(arg) | |
| if url == None: continue | |
| print("\n", url.path()) | |
| defaultApp = ls.LSGetApplicationForURL(url, ls.kLSRolesAll, None, None)[-1] | |
| allApps = ls.LSCopyApplicationURLsForURL(url, ls.kLSRolesAll) | |
| OSStatus, infos = ls.LSCopyItemInfoForURL(url, ls.kLSRequestAllInfo, None) | |
| uti = None | |
| # UTI from extension | |
| if infos[3] != None: | |
| uti = ls.UTTypeCreatePreferredIdentifierForTag(ls.kUTTagClassFilenameExtension, infos[3], ls.kUTTypeData) | |
| if uti is None: | |
| # No file extension, so try OSType | |
| typeString = ls.UTCreateStringForOSType(infos.filetype) | |
| if typeString != None: | |
| uti = ls.UTTypeCreatePreferredIdentifierForTag(ls.kUTTagClassOSType, typeString, ls.kUTTypeData) | |
| else: | |
| uti = "Can't tell?!" | |
| print("UTI: ", uti) | |
| print("default app: ", defaultApp) | |
| print("all apps: ", allApps) |
Author
dreness
commented
Nov 4, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment