Skip to content

Instantly share code, notes, and snippets.

@Spijkervet
Created December 22, 2019 18:09
Show Gist options
  • Save Spijkervet/40e08c4b23994e9bd9f6f2c7c09d4f61 to your computer and use it in GitHub Desktop.
Save Spijkervet/40e08c4b23994e9bd9f6f2c7c09d4f61 to your computer and use it in GitHub Desktop.
"""
Description:
Analyzes a path containing Kontakt libraries (nicnt files), and adds corresponding .plist files to the /Library/Preferences folder.
Usage:
python3 add_kontakt_libraries_macos.py [path to libraries]
"""
import sys
import os
import glob
import plistlib
import xml.etree.ElementTree as ET
def create_plist(path, name, version, hu, jdx):
p = {}
p["JDX"] = jdx
p["ContentVersion"] = version
p["ContentDir"] = (
":".join(path.replace("/Volumes/", "").replace("/", ":").split(":")[:-1]) + ":"
)
p["HU"] = hu
p["Visibility"] = 3
fname = "com.native-instruments.{}.plist".format(name)
plistlib.dump(p, open(os.path.join(work_dir, fname), "wb"), fmt=plistlib.FMT_BINARY)
print("Created {}".format(fname))
# os.system('sudo rm "{}"'.format('~/Library/Preferences/' + fname))
# os.system('sudo rm "{}"'.format('/Library/Preferences/' + fname))
if __name__ == "__main__":
work_dir = "./plists"
if not os.path.exists(work_dir):
os.makedirs(work_dir)
root_dir = sys.argv[1]
root_dir += "/**/**/*.nicnt"
print("searching {}".format(root_dir))
libraries = glob.glob(root_dir)
print("found {} libraries".format(len(libraries)))
for idx, l in enumerate(libraries):
with open(l, "r", encoding="ISO-8859-1") as f:
lines = f.read()
first = lines.find("<?xml")
last = lines.rfind("</ProductHints>")
_lines = lines[first : last + len("</ProductHints>")]
root = ET.fromstring(_lines)
name = root[0].find("Name").text
hu = ""
jdx = ""
try:
hu = root[0].find(".//HU").text
jdx = root[0].find(".//JDX").text
except:
continue
# pass
first = lines.find("<soundinfos")
last = lines.rfind("</soundinfos>")
_lines = lines[first : last + len("</soundinfos>")]
# print(_lines)
version = "1.0.0"
if _lines and 'all' in _lines:
root = ET.fromstring(_lines)
version = '.'.join(list(root.attrib['version']))
print(version)
else:
print('no version {}'.format(l))
create_plist(l, name, version, hu, jdx)
# break
print("Moving .plist files to /Library/Preferences")
os.system('sudo mv {}/*.plist /Library/Preferences'.format(work_dir))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment