4

I've been trying to figure out how to programmatically add files to an Xcode4 project and it seemed like AppleScript would be the way to go, however I'm running into "missing value" errors.

Here's the code I've got:

tell application "Xcode"
    set theProject to first project
    set theTarget to first target of theProject
    set theBuildPhase to compile sources phase of theTarget
    tell first group of theProject
        set theFileRef to make new file reference with properties {full path:"/Users/jeff/Projects/XcodeTest/XcodeTest/MyViewController.h", name:"MyViewController.h", path:"XcodeTest/MyViewController.h", path type:group relative}
        add theFileRef to theProject
    end tell
    --tell theBuildPhase to make new build file with properties {build phase:theBuildPhase, name:"MyViewController.h", file reference:theFileRef, target:theTarget, project:theProject}
end tell

I've tried the commented-out line instead of the add-command as well, but that doesn't work either (I get "missing value").

The 'add' error is:

error "Xcode got an error: file reference id \"251AD3431432472E006E300F\" of Xcode 3 group id \"251AD32C14324645006E300F\" of project \"XcodeTest\" of workspace document \"XcodeTest.xcodeproj/project.xcworkspace\" doesn’t understand the add message." number -1708 from file reference id "251AD3431432472E006E300F" of Xcode 3 group id "251AD32C14324645006E300F" of project "XcodeTest" of workspace document "XcodeTest.xcodeproj/project.xcworkspace"

The "make new reference" does add the file to the list of files in Xcode, but I also need it to be added to the project target so that I can add actions and outlets to the file from Xcode w/o having to first check the checkbox to add it to the "target membership".

jstedfast
  • 35,744
  • 5
  • 97
  • 110

3 Answers3

6

I ended up sending this question to the devs on the xcode developer list and the response I got was effectively "you can't".

jstedfast
  • 35,744
  • 5
  • 97
  • 110
  • 2
    Sometimes, "you can't" means "we don't want you to do/know it. Sincerely, I believe that you can do it in some way, but they will block it at the store. – Marcelo Sep 12 '14 at 17:48
2

This appears to be completely broken in Xcode4, but I've seen a project that does it. I think what they are doing is parsing and modifying the "project.pbxproj" file directly. (this file is hidden inside the xcodeproj bundle)

The file is a GUID soup, but once you look at it for a while it seems possible to safely modify it, especially if you are only adding stuff.

Edit:

Found this stackoverflow answer that might help.

Tutorial or Guide for Scripting XCode Build Phases

Community
  • 1
  • 1
Calvin
  • 4,177
  • 1
  • 16
  • 17
  • Yea, I hacked on MonoDevelop's pbxproj serializer and that's what we do, but if MonoDevelop wants to push more files to the project later, when Xcode is already running with that project open, it's not a nice solution. – jstedfast Feb 06 '12 at 22:42
0

There is a poorly documented user defined build setting that can be added. Files can be both excluded and included from compilation

Go to your target's Build Settings > Tap the + button > Add User-Defined Setting

The key is either INCLUDED_SOURCE_FILE_NAMES or EXCLUDED_SOURCE_FILE_NAMES

The value is a space separated list of file paths

See reference: http://lists.apple.com/archives/xcode-users/2009/Jun/msg00153.html

Kyle Redfearn
  • 2,172
  • 16
  • 34