# Scripting XCode



## woffl (Oct 24, 2006)

Hi everybody in this forum,

I am trying to control XCode via a small AppleScript. But it does not work well and now I wonder if the problem's just that I am too stupid or if it is maybe buggy or ...?

The following piece of code works as expected (XCode builds the frontmost open project and the script exits):

tell application "Xcode"
  build
end tell

However, this does not (XCode builds the frontmost open project and starts the debugger, debugger runs through, but XCode never returns anything to my script so that it never exits):

tell application "Xcode"
  build
  debug
end tell

Any ideas would be very appreciated. Maybe there is something like the "&" in the terminal for applescript?

I am using Tiger 10.4.8. and XCode 2.4 on an iBook G4 / 2005.


----------



## EvLooij (Oct 31, 2006)

If you'd looked at the dictionary for Xcode, you'd have seen that the build command needs an object to work on, and if you look a bit further you'll see that that object needs to be a project or a target. So:

tell application "Xcode"
      local theResult
	set myProject to active project document
	try
		set theResult to build the first target of myProject 
      on error m
		set theResult to m
	end try
     return theResult
end

I recommend using error catching even when experimenting because I've found that when an error occurs XCode will tell you all about it and then quit which gets a bit tiresome.


----------



## woffl (Oct 31, 2006)

Hi EvLooij,

thank you very much for your support -- I instantly tried to make use of your advices, however it did not really solve my problem. I do not just want to build, I also want to launch the debugger. Therefore I modified your script a little bit, (actually I added only one line):

tell application "Xcode"
	local theResult
	set myProject to active project document
	try
		set theResult to build the target of myProject
		set theResult to debug the active executable of myProject --new
	on error m
		set theResult to m
	end try
	return theResult
end tell

It works just fine, the debugger is launched. However, XCode does not send any return value. Even when I stop the debugger, the script keeps waiting for a return value. After several minutes it stops and gives me the error "Erreur dans Xcode : Délai dépassé pour un AppleEvent." Sorry about the french, I'll try to translate: "Error in XCode: AppleEvent timed out".

Any idea? Is there any command that tells AppleScript not to wait for the return value?


PS: if you know a good book or article that deals with this stuff, let me know.


----------



## EvLooij (Nov 1, 2006)

I ran my own original script again (it's always good to check these things) and I did get a return value, namely {"Build succeeded"}. Perhaps I should have mentioned, though that I ran the script from Script Editor (2.1) while Xcode (2.4)  was running at the same time, with an open project.
When I quit Xcode and ran the script again, the script returned the error message that myProject was undefined.

When I ran the script with your extra line 

```
set theResult to debug the active executable of myProject
```
I got a "Timed out" error as well. I believe there is a way to set how long Applescript will wait before generating the timed out error (the default is something like two minutes -- look in the Applescript reference) but the question then becomes what do you want to wait for? 
It seems to me that you will only get a result to your debug command after some debugging has been done -- by you or someone or something else. Which would suggest that you should only use that command when you have at least on breakpoint set and configured.


----------



## woffl (Nov 1, 2006)

EvLooij said:


> It seems to me that you will only get a result to your debug command after some debugging has been done -- by you or someone or something else. Which would suggest that you should only use that command when you have at least on breakpoint set and configured.



For testing my script, I have only one project open in XCode: the "command line utility/standard tool" (actually "hallo world") provided by Apple. The only thing that I changed is I added a breakpoint in the line

```
printf("Hello, World!\n");
```
.

Now I run my AppleScript: XCode builds and launches the debugger. It stops at the breakpoint. I click "continue". The "hello world"-program ends and the status line in XCode indicates "Debugging ended normally". However, the AppleScript does not receive any return value ... and finally times out.


----------



## EvLooij (Nov 3, 2006)

Hmm, so it's waiting for an event that never occurs. Could well be a bug: I'm fresh out of ideas and no doubt you've noticed that there's precious little documentation around on the subject of applescripting Xcode, which is a shame. Good luck and here's hoping some other bright spark will come along and further our knowledge.


----------



## szymczyk (Nov 3, 2006)

Have you tried Automator? Automator comes with a Build Xcode Project action. Xcode has project templates to create your own Automator actions. Apple has an article on automating Xcode on their developer site.


----------

