I am trying to automatically add files to my Xcode project using rb-appscript. I don't have much experience with Ruby or Applescript, but two people seem to have made this work for them:
https://github.com/gonzoua/xcs/blob/master/xcs.thor
Automatically adding generated source files to an xcode project
Unfortunately neither of these work for me. I can get them to add files to groups, but adding files to targets breaks. Here is the simplest piece of code that doesn't work for me:
require 'rubygems'
require 'appscript'
project_path = 'Users:me:Projects:xcode:project:src:AppScaffold.xcodeproj'
project_name = 'AppScaffold'
group_name = 'Classes'
file_name = 'ApplicationDelegate.m'
target_name = 'AppScaffold'
def lookup(sequence, name)
sequence.get.each { |item|
if item.name.get == name
return item
end
}
raise Exception.new("Couldn't find name '" + name + "' in sequence " + sequence.inspect)
end
app = Appscript.app('Xcode')
app.open(project_path)
project = lookup(app.projects, project_name)
target = lookup(project.targets, target_name)
group = lookup(project.root_group.item_references, group_name)
file = lookup(group.item_references, file_name)
file.add({:to => target})
# I also tried this:
# app.add(file, {:to => target})
This fails with this message:
/Library/Ruby/Gems/1.8/gems/rb-appscript-0.6.1/lib/appscript.rb:542:in `_send_command': CommandError (Appscript::CommandError)
OSERROR: -1708
MESSAGE: Application could not handle this command.
COMMAND: app("/Developer/Applications/Xcode.app").workspace_documents["project.xcworkspace"].projects["AppScaffold"].root_group.Xcode_3_groups.ID("080E96DDFE201D6D7F000001").Xcode_3_file_references.ID("1D3623250D0F684500981E51").add({:to=>app("/Developer/Applications/Xcode.app").workspace_documents["project.xcworkspace"].projects["AppScaffold"].targets.ID("1D6058900D05DD3D006BFB54")})
from /Library/Ruby/Gems/1.8/gems/rb-appscript-0.6.1/lib/appscript.rb:642:in `method_missing'
from add_to_target.rb:28
Note that I am using Xcode 4, but my project seems to have "Xcode_3_references". This is also the case for projects I create from scratch; I'm not sure why.
Anyway, I'm not sure what to make of this error message, or where to find documentation on this. (I looked at the Xcode dictionary, but it didn't have much to say about "add".) Any advice would be great. I'm not married to rb-appscript, I just want to add files to my project programmatically.