XCode Project automation for multiple similar app by Python script


In app store I have over 50 dictionary applications by a single codebase. There are different constant files, different database and different icon set.

Every time when I modify the project manually for different language, it takes roughly 5 minutes. So I thought why not write a script to do the task automatically. And now it takes 3 seconds to modify my XCode project for different language app.

To make the job done, I select Python scripting language. Python is quite easy language to learn and have extensive library. Last time I wrote Python script in 2007 so its 10 years later I wrote another script.

In this blog post, I will share some common functions of the Python script which I think quite helpful to do similar job.

1. Importing some library in my script

import os
import shutil
import subprocess
import plistlib

2. Remove icon directory with files

 try:
        removeIcons = "/app_directory/app_name/Images.xcassets/AppIcon.appiconset"
        shutil.rmtree(removeIcons)
        print("Icons deleted")
except:
        print("ERROR: Icons not found")

3. Copying directory with files function

ref: pythoncentral.io

import errno
import shutil

def copy(src, dest):
    try:
        shutil.copytree(src, dest)
    except OSError as e:
        # If the error was caused because the source wasn't a directory
        if e.errno == errno.ENOTDIR:
            shutil.copy(src, dest)
        else:
            print('Directory not copied. Error: %s' % e)

Just call copy(src, dest) function and provide the directory path correctly it will perform the copying perfectly.

4. XCode project has .plist files. To modify .plist file

def pListModification():
    bundleName = "MY APP"
    bundleIdentifier = "net.myapp"
    
    plistNote ="app_directory/app_name/" + "/App/App-Info.plist"
    

    plistNoteData   = plistlib.readPlist(plistNote)

    plistNoteData['CFBundleDisplayName'] = "New My App"
    plistNoteData['CFBundleIdentifier'] = "net.newmyapp"
    plistNoteData['CFBundleName'] = "New My App"

    plistlib.writePlist(plistNoteData, plistNote)

5. Opening XCode project by Python Script

def openNotesXcodeProject():
    print("My App XCODE OPENING")
    pathToOpen = "/app_directory/app_name/"+ "App.xcworkspace"
    openFile   = "open " + "'" + pathToOpen + "'"
    os.system(openFile)

Hope it helps if you do similar job by Python script. Also during writing the script I take quick reference of Python language from this site tutorialspoint.com/python/

mahmud ahsan

Computer programmer and hobbyist photographer from Bangladesh, lives in Malaysia. My [Business | Twitter | Linkedin | Instagram | Flickr | 500px]

You may also like

LEAVE A COMMENT