Skip to content

Instantly share code, notes, and snippets.

@dreness dreness/LSFileInfo example
Last active Sep 24, 2019

Embed
What would you like to do?
Interrogate LaunchServices using #PyObjC to query file paths for default app handler, all possible app handlers, and the UTI
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/"
))
#!/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)
@dreness

This comment has been minimized.

Copy link
Owner Author

dreness commented Nov 4, 2017

$ python ~/bin/LSFileInfo.py ~/manatee.mp4 

/Users/andre/manatee.mp4
UTI:  public.mpeg-4
default app:  file:///Applications/QuickTime%20Player.app/
all apps:  (
    "file:///Applications/Compressor.app/",
    "file:///Applications/QuickTime%20Player.app/",
    "file:///Applications/IINA.app/"

$ python ~/bin/LSFileInfo.py ~/Documents/ejabberd-pam-working.yml 

/Users/andre/Documents/ejabberd-pam-working.yml
UTI:  dyn.ah62d4rv4ge81w5pq
default app:  None

$ python ~/bin/LSFileInfo.py ~/Documents/MDKMobileDevice.h 

/Users/andre/Documents/MDKMobileDevice.h
UTI:  public.c-header
default app:  file:///Applications/Xcode.app/
all apps:  (
    "file:///Applications/Microsoft%20Word.app/",
    "file:///Applications/TextEdit.app/",
    "file:///Applications/Older%20Xcodes/Xcode_9M136h_Tigris15A297_2017-06-22-14-03-21.222659.app/",
    "file:///Applications/Microsoft%20Excel.app/",
    "file:///Applications/Pages.app/",
    "file:///Applications/Quip.app/",
    "file:///Applications/Numbers.app/",
    "file:///Applications/Notes.app/",
    "file:///Applications/LibreOffice.app/",
    "file:///Applications/Older%20Xcodes/Xcode_9M137e_2017-09-14-14-50-56.234467.app/",
    "file:///Applications/Xcode.app/"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.