Created
August 4, 2014 05:15
-
-
Save meoow/a80b7193259fcd3c5bb0 to your computer and use it in GitHub Desktop.
Clean all associated file types of a app bundle
This file contains 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 python2.7 | |
import plistlib | |
import os | |
import sys | |
import shutil | |
import subprocess | |
SUBDIR_PATH=os.path.sep.join(('Contents', 'Info.plist')) | |
class FileTypeCleaner(object): | |
def __init__(self,pathOfFile): | |
self.filepath = self.checkInfoPlistExistence(pathOfFile) | |
if self.filepath is not None: | |
try: | |
self.plistdata = plistlib.readPlist(self.filepath) | |
except: | |
try: | |
self.plistdata = plistlib.readPlistFromString( | |
self.binary2xml(self.filepath)) | |
except: | |
self.plistdata = None | |
else: | |
self.plistdata = None | |
@staticmethod | |
def checkInfoPlistExistence(pathOfFile): | |
path=os.path.sep.join((pathOfFile, SUBDIR_PATH)) | |
if os.path.isfile(path): | |
return path | |
return None | |
def checkInfoPlistHasTypeKey(self): | |
if 'CFBundleDocumentTypes' not in self.plistdata or \ | |
self.plistdata['CFBundleDocumentTypes'] == []: | |
return False | |
return True | |
def vacuumTypeKey(self): | |
self.plistdata['CFBundleDocumentTypes'] = [] | |
@staticmethod | |
def binary2xml(pathOfFile): | |
commandline = 'plutil -convert xml1 -o - -- ' + pathOfFile | |
run = subprocess.Popen(commandline, shell=True, stdout=subprocess.PIPE,stderr=subprocess.PIPE) | |
plistString, dummy = run.communicate() | |
if run.returncode == 0: | |
return plistString | |
return None | |
if __name__=='__main__': | |
if len(sys.argv)==1: | |
sys.stderr.write('Usage: cleanFTypeAssoc.py app1.app [app2.app ...]\n') | |
sys.stderr.flush() | |
raise SystemExit | |
for f in sys.argv[1:]: | |
p = FileTypeCleaner(f) | |
if p.plistdata is not None: | |
if p.checkInfoPlistHasTypeKey(): | |
p.vacuumTypeKey() | |
shutil.move(p.filepath, p.filepath+'~') | |
plistlib.writePlist(p.plistdata, p.filepath) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment