Adding files via Applescript to an Xcode project target
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开发者_JAVA技巧) & "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.
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.
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
精彩评论