"Python script to change bundle id"

Wed 24 September 2014

import getopt, sys

import re

 

def replaceStringKey(key, value, result):

    pattern = "(?P<first><key>"+key+"</key>[\\r\\n\s]*<string>)[\S]*(?P<second></string>)"

    p = re.compile(pattern)

    result = p.sub("\g<1>" + value + "\g<2>", result)

    return result

 

def replaceIntegerKey(key, value, result):

    pattern = "(?P<first><key>"+key+"</key>[\\r\\n\s]*<integer>)[\S]*(?P<second></integer>)"

    p = re.compile(pattern)

    result = p.sub("\g<1>" + value + "\g<2>", result)

    return result

 

 

def main():

    appId = "com.your.new.id"

    bundle_display_name = "Display Name"

 

    fileName = "Plist.plist"

    f = open(fileName)

    line = f.read()

    result = replaceStringKey("CFBundleIdentifier", appId, line)

    result = replaceStringKey("CFBundleDisplayName", bundle_display_name, result)

 

    print result

    w = open(fileName, "w")

    w.write(result)

    w.close()

 

if __name__ == "__main__":

    main()