Skip to content

Instantly share code, notes, and snippets.

@grahamgilbert
Created September 5, 2016 11:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grahamgilbert/724e79b4a26ec172ae8b9e414e677c98 to your computer and use it in GitHub Desktop.
Save grahamgilbert/724e79b4a26ec172ae8b9e414e677c98 to your computer and use it in GitHub Desktop.
Writing a base64 encoded string into a plist
#!/usr/bin/python
import base64
import plistlib
import errno
import os
def mkdir_p(path):
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise
def main():
# This is the license.
license = 'abc123'
# Directory
plist_path = '/Users/Shared/Library/Preferences'
# File
plist_file = 'com.cambridgesoft.plist'
# make the directory if it exists
mkdir_p(plist_path)
# Encode the license to base64
encoded_license = base64.b64encode(license)
# An ampty dict for writing (we could read this if we cared about existing data)
plist = {}
plist['license'] = encoded_license
# Write our file - this will wipe out anything that exists
plistlib.writePlist(plist, os.path.join(plist_path, plist_file))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment