5

I want to automatically add files to an Xcode project that is created from scratch (through another IDE) as a post-build step. Our project is set up to call an applescript that makes the appropriate file references in the project, but every attempt to add the file references to the project fails.

The core problem seems to be this error: Xcode got an error: file reference id "DCDCC17E13819A8E004B4E75" of Xcode 3 group id "D82DCFB50E8000A5005D6AD8" of project "TestProject" of workspace document "project.xcworkspace" doesn’t understand the add message.

on this line:

add fileRef to first target of testProject

Where fileRef is the variable set to the newly-created file reference, and testProject is the variable set to the project containing the fileRef.

Here is the code:

on run argv

-- Get the folder containing items to be added to the project
tell application "Finder"
    set thisScript to path to me
    set projectFolder to get folder of thisScript as string
    set sourceFolder to projectFolder & "FilesToAdd:"
end tell

-- Get all the files that will be added to Xcode
tell application "System Events"
    set filesToAddList to the name of every disk item of (sourceFolder as alias)
end tell

tell application "Xcode"
    -- Open the project using posix-style paths
    open ((POSIX path of projectFolder) & "TestProject.xcodeproj")

    -- Give Xcode some time to open the project before we start giving it commands
    delay 1

    set testProject to project "TestProject"
    tell testProject
        set sourceGroup to group "Sources"
        tell sourceGroup
            -- Iterate over all files in the list
            repeat with i in filesToAddList

                set fileName to (contents of i)

                -- Get the file path using Unix-style pathing, since that is the kind that Xcode needs
                set filePath to (POSIX path of sourceFolder) & fileName

                -- Don't add duplicate file references
                if filePath is not in path of file references in sourceGroup then
                    -- Add a new file reference to the project
                    set fileRef to make new file reference with properties {name:fileName, full path:filePath, path type:absolute, path:filePath, file encoding:macos roman}

                    -- Add the file reference to the build target
                    add fileRef to first target of testProject
                end if
            end repeat

        end tell -- end group tell

    end tell -- end project tell

end tell -- end app tell

end run

I've looked up other examples of adding files to targets, and it seems like this is the cleanest and concise way of doing it, and it seems to have worked for other people in the past. Is there a different way that file references are supposed to be added to targets?

For reference, I'm running OSX 10.6.7 and Xcode 4.0.2.

2 Answers 2

5

So I emailed Apple about this problem, and it turns out to be a bug. I've submitted a bug report, so hopefully this issue will get fixed soon.

6
  • Any idea how I can add some files to an XCode project via a PHP script? Oct 6, 2011 at 18:43
  • As far as I know, the only way to programmatically communicate with XCode is via Applescript, maybe your PHP script can invoke some sort of Applescript? But I really don't know how PHP works at all.
    – Reuben
    Oct 22, 2011 at 1:47
  • Have you checked the radar for that bug recently? Has the status been updated?
    – ThomasW
    Jan 25, 2012 at 2:32
  • 1
    I just checked the bug, and the status has not been updated. I guess applescript support for XCode is on the backburner for now...
    – Reuben
    Feb 14, 2012 at 2:34
  • Doh, I would like to be able to do this too. Anything other than either mucking around in an XML tree or manually drag-dropping references would be an improvement...
    – conny
    Mar 1, 2013 at 4:43
1

Change this:

-- Add the file reference to the build target add fileRef to first target of testProject

To this:

-- Add the file reference to the build target tell application "Xcode" add fileRef to first target of testProject end tell

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.