On my first blog post, “Ready… Set… Xcode!“, we ended off with a challenge: to build a utility tool that could automate new project deployments.
There’s a couple of solutions you could use to solve this problem. You could write a Unix script, an apple script, or (if you want to be overkill) a whole dedicated Xcode project. I did, though, suggest that you use this little guy →
Because Automator is simple, fast and allows you to provide easy to use UI feedback. As a tool it is also completely undervalued… And the little A.I. bot is adorable… Yes, I just said it.
You can download a version of our Automator Application solution with our sample project, our projects and tutorials are free and always will be.
Now to the meat and grind… First you will want to create a new Automator Application and save it to the parent folder of your base project. The reason for saving it in this location is so that you can use the relative location of the project to your advantage, allowing you to copy the parent folder to other locations/computers without having to rewrite the script.
You will need Automator to get the path of the parent folder containing the Automator app and your base project, you can do this by creating a Run Applescript command with the following execution script:
set UnixPath to POSIX path of ((path to me as text) & "::")
Make sure you disable input from this command, you can do this by right clicking on the command box. You will then need to assign the output of this command to an Automator variable, you do this by chaining the Run Applescript command to a Set Value of Variable command and give your variable a name. Your Automator workflow should now look like this:
Next, we need to let users set the name of the project. In order to do this, create a Ask for Text command. In order to avoid bogus errors, make the answer a requirement and, like before, make sure that this command disables all input and stores outputs to a Set Value of Variable command. You should now have a workflow that looks like this:
Now here comes the juicy part. We have two stored variables, “ProjectPath” and “ProjectName”, that we need to pass as arguments to our shell script. We do this by chaining two Get Value of Variable commands and outputting the result to a Run Shell Script command that runs the following script:
PROJECTS_PATH=$1; NEW_PROJECT_NAME=$2; OLD_PROJECT_NAME="EPICBaseProject" export LANG=C cd "$PROJECTS_PATH"; cp -r "$OLD_PROJECT_NAME" "$NEW_PROJECT_NAME"; cd "$NEW_PROJECT_NAME"; grep -Rl "$OLD_PROJECT_NAME" * | xargs sed -i "" "s/$OLD_PROJECT_NAME/$NEW_PROJECT_NAME/"; mv"$OLD_PROJECT_NAME.xcodeproj" "$NEW_PROJECT_NAME.xcodeproj";
This script does the following:
- Assigns the value of the first and second input arguments to local variables called PROJECTS_PATH and NEW_PROJECT_NAME.
- Creates a variable with the current name of your base project.
- Sets our location to be the parent folder path of our projects.
- We make a copy of our base project to a folder with the name of our new project. The copy is made inside the same parent directory.
- We set our location to our new project’s root folder.
- We find all usages of the base project name inside our new project and replace them with the new project name.
- Finally, we rename the xcodeproj file to our new project.
You now have an automated way of generating Xcode new projects on the fly. Congratulations! 🙂