Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add files to targets in Xcode using rb-appscript?

Tags:

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.

like image 328
traversable Avatar asked Jul 13 '11 21:07

traversable


People also ask

How do I add files to Xcode?

Xcode offers several ways to add existing files and folders to your project: Drag the files from the Finder into the Project navigator. Click the Add button (+) in the Project navigator's filter bar, and choose Add Files to “projectName”. Choose File > Add Files to “projectName”.

How to go to targets in Xcode?

When you create a new project from a template, Xcode adds one or more targets to the project automatically. For example, the multiplatform app template contains separate targets for an iOS app and Mac app. To view the targets in a project, select the project in the navigator area.

What are targets Xcode?

A Target specifies a product to build and contains the instructions for building the product from a set of files in a project or workspace. An Xcode scheme defines a collection of targets to build, a configuration to use when building, and a collection of tests to execute.


2 Answers

I'm not 100% sure what you are trying to do and whether you are building for the Mac or for iOS. Therefore I'm not sure if I can be of much help. If you are simply trying to add files to your project for the sake of bundling them rather than compiling them in the build then you do not need to code this. Instead click on your project at the top of your files list on the left hand panel, then in the main window click on your target bundle. This should give you a screen with the options 'summary - info - build settings - build phases - build rules' along the top. Click on build phases and then choose copy bundle resources form the list below. Then click the + sign at the bottom of this list and select the file you want to copy to the bundle during the build.

That said, if the file has to be added programmatically then you would need a more advanced user than me to answer your question. Though, in thinking about it, I guess it depends on the type of file, where you are getting it from and the permissions required to do this on the iphone. If you are building for the OSX then I'm in unknown water and this advice probably isn't that helpful.

like image 190
James B. Avatar answered Nov 02 '22 15:11

James B.


So, one issue with Xcode since it moved to the AppStore is that older versions were left intact, and not removed - not to mention the binaries are all different (llvm vs cc). It wasn't the same issue you are having, but I did run into complications using the command-line compilers for things like homebrew, and came across this command:

sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer 

See: http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/xcode-select.1.html

I don't know for sure here, but it's possible this will "re-orient" the build system you are working with to reference the current xcode and not the older version, which may have the intended result.

like image 43
FilmJ Avatar answered Nov 02 '22 15:11

FilmJ