Programmatically rename an XCode project - xcode

I know one can rename a project by slowly double clicking the project name but I was wondering if it's possible to automate that through the command line (any language, possibly Apple Script) ?

Renaming an Xcode4 project is quite easy.
Click on Show the Project Manager to see the top level project file on the left side.
Click on the project name (here DeeTeeSaver will be changed to DeeTeeSaver2) in order to edit it and enter the new name.
It takes a second to let Xcode4 find out which parts have to be changed.
It then displays a dialog which parts will be changed. Simply click on Rename.
That´s it. No need to fondle around with AppleScript.
You might miss important parts inside the bundle.
Greetings from Switzerland, Ronald Hofmann

You will have to manipulate the .xcodeproj file, which is in XML format.
In my build process I have placeholder names that I simply search & replace with a command line app. If you need a generic solution you will have to parse the XML format (which is undocumented but not that hard to grasp).

Related

Xcode can't find source files

I'm using Xcode 8. I recently inherited a project with several hundred files (including source and image files). I rearranged them on my local drive and the file names (appropriately) become red in the folder list on the left side of Xcode. I selected files/folders in this folder list, click on the "Hide or show Utilities" button to display the "Identity and Type" pane on the right side of Xcode, clicked on the little folder icon next to the Location, and selected the files'/folders' new locations. The text in that pane was updated to the new location and the file/folder names changed from red to black. So far, so good. I did this to all of the file/folder names until none of them were displayed in red.
However, when I go to build the project, I get numerous warning messages similar to, "image.png /Users/Me/Project/images/image.png is missing from working copy." The path shown in the error message is the file's OLD location. When I look at that image in the file list, it is displayed in black. When I select that file and look at the Full Path in the pane on the right, it shows the file's current location (e.g., "/Users/Me/Project/images/newfolder/image.png"). I'm unsure where in the project the old location is being stored. FWIW, I've tried Cleaning the project...
Thanks for insights.
So here is an approach that is perhaps not for the meek, yet it is something I do more often than one would expect to fix Xcode project files. I tend to be the one designated to do this on the teams I work with ... manually editing the project file. The .xcodeproj file is really just a special folder. The actual project file is project.pbxproj.
First back up the project file. Your choice on if you want to do the complete .xcodeproj or just the project.pbxproj.
Use your favorite text editor and open up the project.pbxproj file.
Search and replace the prefix to your path. For the sake of this exercise, you should try and keep your path as similar as possible to make it easier. For example, if the hardcoded path is /Users/Me/Project/Images/newfolder/image.png and all prefixes are generally "/Users/Me/Project", you can just do a search on "/Users/Me/" or "/Users/Me/Project" (the latter if you want more safety) and replace with "/Users/You/" or "/Users/You/Project". Note I am not searching on "Me" and replacing with "You". You want to search and replace but as controlled as possible.
Once done, save and open the project. If the project doesn't open at all, it means you messed something up. Start over. Note that changing the paths should not be sufficient to break the file. It will probably mean you accidentally added or deleted something.
If the project file opens now build. It should hopefully build.
Okay, so that gets you into a buildable state. Now you really want to fix things. Whomever did the project was a knucklehead for using absolute paths.
This next part will be tedious. There are probably ways to do this manually, but I'll leave that to an exercise for the reader right now. In file inspector within Xcode, you will want to change files to be anything but "Absolute Path". Here is an example, you can see the location is "Relative to Group".
Essentially you are going to have to around to Groups and files and fix things up to not be absolute. Make sure you backup incrementally and can build.
Wait, but unfortunately there is more. You'll then need to go into Build Settings to see if things are absolute paths. Then you'll need to decide how to adjust for that. For example, it is not uncommon for 3rd Party frameworks to be added with absolute paths.
Or I suppose if you want to, you can just get it working and skip the latter part of this and damn everyone else...

Couldn't load a Xcode project with pods [duplicate]

How do I rename a project in Xcode 5?
What steps do I need to take?
In the past this was always a very tricky manual process.
Well, the answer is very very very Apple simple in Xcode 5!
In the Project Navigator on the left side, click 2 x slowly and the Project file name will be editable.
Type the new name.
A sheet will appear with a warning and will list all the items Xcode 5 believes it should change.
You can probably trust it, but you should inspect it.
The list will include the info.plist and various files, but also all the relevant strings from nib/xib files like MainMenu menus.
Accept the changes and you will get the prompt to save a snapshot of the project.
Always make a snapshot when Xcode asks, it will be useful to restore if something does not work.
Change the project name:-
Click on the target in xcode, on the right in "Identify and Type" under name change the name and press the ENTER button on your keyboard.
A window will appear confirming the change and what it will change. Once you confirm it will make the changes.
Change the root folder name:-
Go to the project directory and rename the root folder,
Open the project and u will find all the file are missing, u need to add all the files of project again
Right click the project bundle .xcodeproj file and select “Show Package Contents” from the context menu. Open the .pbxproj file with any text editor.
4>Search and replace any occurrence of the original folder name with the new folder name.
5>Save the file.
Change the Scheme name:-
rename .xscheme file
If your Project is static framework then make sure your header file has public target membership
I really recommend just opening the folder in a general editor such as Sublime Text, and doing a find/replace across the whole folder. The other methods I found were unstable, particularly when combined with .xcworkspace and cocoapods.
In Xcode 8.0, to rename your project, just go through the following instructions as described in Xcode help:
1- Select your project in the project navigator.
2- In the Identity and Type section of the File inspector, enter a new
name into the Name field.
3- Press Return.
A dialog is displayed, listing the items in your project that can be
renamed. The dialog includes a preview of how the items will appear
after the change.
4- To selectively rename items, disable the checkboxes for any items
you don’t want to rename. To rename only your app, leave the app
selected and deselect all other items.
5- Click Rename.
Source: http://help.apple.com/xcode/mac/8.0/#/dev3db3afe4f
Xcode 6 (beta 6 as of now) seems to be not very reliable with renaming projects. For me it didn't rename several of the files and groups. It also doesn't rename the physical folder the project is in. To rename my project to be sure that everything is clean I went the extra length to create a new project with the new name and copy over all the files. The assets are easy to copy but groups have to be recreated. The biggest issue with this however are CoreData data model files. Trying to simply copy this will result in a corrupt model file, even though everything looks like it is alright.
When you re-name the project name in XCode5 then info.plist entry removed from Targets --- > General ---> identity. You just need to mention it again.
In Xcode 7, renaming a project can still break your app. Make sure you backed it up before trying it.
Click on the project icon and find the project name in the inspector pane. If you change it there, Xcode will ask you if you want to rename related files. Might work. But if not, try this brute force approach:
Close Xcode
Using an advanced text editor like Sublime Text or Atom, open the
root folder. It will open the folder structure.
Perform a Global Search and Replace (it's probably cmd + shift + f), and
replace My Wrong App Name with New App. If your project name contained spaces, also search for My_Wrong_App_Name and replace
with
New_App. This changes all file contents.
Now you need to find all
the files inside the project with your old app name. Rename them
all, also the folders.
Important: Open the project file with
right click > Show Package Contents, and rename all files in there.
Reopen your Xcode project or workspace. Compile.
If you use Pods, you need to open the pods project as well and change the files in there.
Here is another great example which works well with xcode 5

Keeping unused files in Xcode project

In Eclipse you can create a folder and name it, say, "unused", to keep currently unused files (incomplete code, resources for future use, etc)
which are ignored by project builder. Can I have something similar with Xcode?
If I move a file to a project folder without inserting it with Xcode, it may be OK, and I will be able to see it while opening in Finder. However, it will be more convenient to have it listed by Project Navigator.
As far as I am concerned, it's not possible. Though, you can comment all of your code in one click to momentarily disable it.
Press command + A;
Right click and select Comment Selection.

How do I rename a project in Xcode 5?

How do I rename a project in Xcode 5?
What steps do I need to take?
In the past this was always a very tricky manual process.
Well, the answer is very very very Apple simple in Xcode 5!
In the Project Navigator on the left side, click 2 x slowly and the Project file name will be editable.
Type the new name.
A sheet will appear with a warning and will list all the items Xcode 5 believes it should change.
You can probably trust it, but you should inspect it.
The list will include the info.plist and various files, but also all the relevant strings from nib/xib files like MainMenu menus.
Accept the changes and you will get the prompt to save a snapshot of the project.
Always make a snapshot when Xcode asks, it will be useful to restore if something does not work.
Change the project name:-
Click on the target in xcode, on the right in "Identify and Type" under name change the name and press the ENTER button on your keyboard.
A window will appear confirming the change and what it will change. Once you confirm it will make the changes.
Change the root folder name:-
Go to the project directory and rename the root folder,
Open the project and u will find all the file are missing, u need to add all the files of project again
Right click the project bundle .xcodeproj file and select “Show Package Contents” from the context menu. Open the .pbxproj file with any text editor.
4>Search and replace any occurrence of the original folder name with the new folder name.
5>Save the file.
Change the Scheme name:-
rename .xscheme file
If your Project is static framework then make sure your header file has public target membership
I really recommend just opening the folder in a general editor such as Sublime Text, and doing a find/replace across the whole folder. The other methods I found were unstable, particularly when combined with .xcworkspace and cocoapods.
In Xcode 8.0, to rename your project, just go through the following instructions as described in Xcode help:
1- Select your project in the project navigator.
2- In the Identity and Type section of the File inspector, enter a new
name into the Name field.
3- Press Return.
A dialog is displayed, listing the items in your project that can be
renamed. The dialog includes a preview of how the items will appear
after the change.
4- To selectively rename items, disable the checkboxes for any items
you don’t want to rename. To rename only your app, leave the app
selected and deselect all other items.
5- Click Rename.
Source: http://help.apple.com/xcode/mac/8.0/#/dev3db3afe4f
Xcode 6 (beta 6 as of now) seems to be not very reliable with renaming projects. For me it didn't rename several of the files and groups. It also doesn't rename the physical folder the project is in. To rename my project to be sure that everything is clean I went the extra length to create a new project with the new name and copy over all the files. The assets are easy to copy but groups have to be recreated. The biggest issue with this however are CoreData data model files. Trying to simply copy this will result in a corrupt model file, even though everything looks like it is alright.
When you re-name the project name in XCode5 then info.plist entry removed from Targets --- > General ---> identity. You just need to mention it again.
In Xcode 7, renaming a project can still break your app. Make sure you backed it up before trying it.
Click on the project icon and find the project name in the inspector pane. If you change it there, Xcode will ask you if you want to rename related files. Might work. But if not, try this brute force approach:
Close Xcode
Using an advanced text editor like Sublime Text or Atom, open the
root folder. It will open the folder structure.
Perform a Global Search and Replace (it's probably cmd + shift + f), and
replace My Wrong App Name with New App. If your project name contained spaces, also search for My_Wrong_App_Name and replace
with
New_App. This changes all file contents.
Now you need to find all
the files inside the project with your old app name. Rename them
all, also the folders.
Important: Open the project file with
right click > Show Package Contents, and rename all files in there.
Reopen your Xcode project or workspace. Compile.
If you use Pods, you need to open the pods project as well and change the files in there.
Here is another great example which works well with xcode 5

How do I control the "open quickly" search path in XCode3 (3.2.3)?

XCode2 had this beautiful feature: you put the cursor on a line with a #include <file> and execute the open-quickly command (command-shift-D) and it would open the file. I can't get it to work for me in XCode3.
I have something like this
#include <folder/subfolder/HeaderFile.hh>
and when I put the cursor on this line, and open the open-quickly dialog, it automatically fills the search window with
folder/subfolder/HeaderFile.hh
but doesn't find anything. If I then remove "folder/subfolder/" then the dialog will identify HeaderFile.hh and I can open it up. But this extra deleting is so much slower just the three-key command-shift-D opening I was used to with XCode2.
If I give it the full path to this file in the search window, then it finds it:
/Users/andrew/myproject/src/folder/subfolder/HeaderFile.hh
this path will find the file. Again, I don't want to spend all my time typing out the full path.
I created this project as an external build system, and I think that's related to the problem; the xcode project lives in
/Users/andrew/myproject/xcp_dir/
and I want it to search in the "../src/" directory so that the partial path I give it (which is already present in the #include) will match the file I want to open. In XCode3, I had to create the project in the xcp_dir directory; in XCode2, I could create the project directly into the existing myproject/ directory -- maybe that's why the search is failing?
I read on a previous thread (which never answered the question) that I should set the HEADER_SEARCH_PATHS option. I tried this (setting the option to /Users/andrew/myproject/src) but it did not work.
I don't have any sense of what other options (like the HEADER_SEARCH_PATHS option) are availalbe, so if you know which one I have to set, I'd love to hear it.
Since this is an old question I'll answer it for Xcode 4 instead of 3.
As far as I know Open Quickly doesn't use anything like a search path. It seems to have use the project's codesense index and will show results from that. Querying the database doesn't have an notion of file paths, so the workflow you describe just won't work anymore.
However that's okay, because there's a better solution: Just ⌘+click on the include line and the appropriate file will be found using the same rules as the compiler uses, and opened. If you want to open the file up in an assistant, another tab or window, you can use ⇧⌘+click instead, and you'll get a little UI for easily selecting where you want the file opened.
You can also use these shortcuts to go to definitions for any identifier.
If you don't want to use the mouse you can use the command "Jump to Definition" which has the shortcut ⌃⌘J, or ⌥⌃⌘J to open the definition in the assistant editor. This also works for both normal identifiers and #included files.

Resources