Convert plist to JSON

EDIT: see my plist2json post for a better version

Here’s a python program to convert a plist to JSON. You’ll need to type in the input and output filenames in the GUI (ie: /Users/bob/myDirectory/file.plist). It will overwrite the output file if it already exists. If anyone wants a version for batch processing, feel free to modify it or send me a request. Tested in Python 2.7.2.

# By Rab Beverly: http://www.robertbeverly.com
# Author assumes no responsibility for damages.
# Use this code however you wish.
# Attribution is not required.

import json
from plistlib import readPlist
import StringIO
import Tkinter

class Converter:

    def __init__(self):
        """ ... """
        self.root = root = Tkinter.Tk()
        root.title('Converter')
        # inFile
        inLabel = Tkinter.Label(root, text='plist file (in):')
        inLabel.grid(column=0, row=0)
        # grabbable entry field
        self.inFileBox = Tkinter.Entry(root, width=30)
        self.inFileBox.grid(column=1, row=0)
        # outFile
        outLabel = Tkinter.Label(root, text='JSON file (out):')
        outLabel.grid(column=0, row=1)
        # grabbable entry field
        self.outFileBox = Tkinter.Entry(root, width=30)
        self.outFileBox.grid(column=1, row=1)
        # Button to grab fields
        convertButton = Tkinter.Button(root, text='Convert',
                                      command=self.makeConversion)
        convertButton.grid(column=0, row=3)
        # A quit button
        quitButton = Tkinter.Button(root, text='Quit',
                                    command=self.onExit)
        quitButton.grid(column=1, row=3)
        root.mainloop()

    def onExit(self):
        self.root.quit()

    def makeConversion(self):
        """ grab the configuration values, convert, and write """
        inFile = self.inFileBox.get()
        outFile = self.outFileBox.get()

        plist = open(inFile,"r").read()
        theData = StringIO.StringIO(plist)
        plist_dict = readPlist(theData)
        open(outFile,"w").write(json.dumps(plist_dict))

Converter()

Download it:

plist-to-json.py (zip file, ~4KB)

This entry was posted in Uncategorized. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

2 Comments

  1. acca
    Posted January 5, 2012 at 10:40 pm | Permalink

    Can I have the batch processing version? My email address is vanlen@126.com. Thanks in advance.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>