title: Adding files via Applescript to an Xcode project target
body: <p>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. </p>
  
  <p>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.</p>
  
  <p>on this line:</p>
  
  <pre><code>add fileRef to first target of testProject
  </code></pre>
  
  <p>Where fileRef is the variable set to the newly-created file reference, and testProject is the variable set to the project containing the fileRef.</p>
  
  <p>Here is the code:</p>
  
  <pre><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 &amp; "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) &amp; "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) &amp; 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
  </code></pre>
  
  <p>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? </p>
  
  <p>For reference, I'm running OSX 10.6.7 and Xcode 4.0.2.</p>