通过AppleScript将文件添加到Xcode项目目标

5
我希望能够在由另一个IDE创建的Xcode项目中作为后续构建步骤,自动添加文件。我们的项目设置为调用AppleScript,在项目中进行适当的文件引用,但是每次尝试将文件引用添加到项目中都失败了。
核心问题似乎是这个错误:Xcode发生错误:Xcode 3组ID“D82DCFB50E8000A5005D6AD8”的文件引用ID“DCDCC17E13819A8E004B4E75”在工作区文档“project.xcworkspace”的项目“TestProject”中无法理解添加消息。
出错代码行如下:
add fileRef to first target of testProject

在这里,fileRef是变量,代表新创建的文件引用;testProject是变量,代表包含fileRef的项目。

以下是代码:

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

我查阅了其他添加文件到目标的示例,这似乎是最简洁和清晰的方法,并且过去它已经为其他人工作过。是否有其他添加文件引用到目标的方法?
参考一下,我正在运行OSX 10.6.7和Xcode 4.0.2。
2个回答

5

我向苹果发送了关于这个问题的邮件,结果发现这是一个漏洞。我已经提交了一个漏洞报告,希望这个问题能够很快得到解决。


你有什么想法可以通过PHP脚本将一些文件添加到XCode项目中吗? - Mihai Fratu
据我所知,与XCode进行编程交互的唯一方式是通过Applescript,也许您的PHP脚本可以调用某种形式的Applescript?但我真的不知道PHP如何工作。 - Reuben
你最近有检查过那个 bug 的雷达吗?状态有更新吗? - ThomasW
1
我刚刚检查了这个 bug,状态还没有更新。我猜 XCode 对于 AppleScript 的支持现在被搁置了... - Reuben
哎呀,我也想能做到这个。除了在 XML 树中胡乱操作或手动拖放引用之外,任何其他方法都会更好... - conny
顺便说一句,这是最初引领我来到这里的脚本:https://github.com/gonzoua/xcs/ xcs:add File [Group] 将文件添加到组中。默认情况下添加到“Source”。 - conny

1

将此代码:

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

修改为:

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接