Launching Firefox from ANT on OSX

| 2 Comments | No TrackBacks

I've been a happy Linux user for quite some time, but I recently switched to OSX after getting a new Macbook laptop. After the switch, one of the first things that caught my attention was that my ANT tasks to launch a web browser would no longer work as expected.

Let's take a look at a quick example. I was launching Firefox with the following technique:

// In
browser = firefox

// In
browser = C:/Program Files/Mozilla Firefox/firefox.exe
<!-- Launch the browser via the exec ANT task -->
<exec executable="${browser}" spawn="yes">
	<arg line="${output.index.html}" />

Naturally, since this worked on Linux and Windows I figured it would work on OSX just fine as long as I got the path to the browser correct. The first thing that I tried was this:

browser = /Applications/

When I tested this it almost worked. If I didn't have Firefox already open, it would open the browser correctly and display my target output file. However, things broke down if I already had an instance of Firefox open.

Rather than open the target output file in a new tab (which was the Linux/Win behavior), I would get an error dialog saying that Firefox was already in use and that I was only allowed to launch one instance of it at a time. It asked me to quit Firefox first, and then try again. Since I almost always have a browser window open to keep tabs on my webmail, obviously this wasn't a workable solution for me.

After a bit of digging and experimenting, I came up with the idea of using the "open" command and passing it the Firefox application as a parameter. This has been working great so far and only required a slight modification to the script:

// In
#browser = open -a Firefox  #Does not work, need to pass -a Firefox as args to exec

browser = open
browser.args = -a Firefox
<!-- Updated the exec tag to use additional arguments -->
<exec executable="${browser}" spawn="yes">
	<arg line="${browser.args}" />
	<arg line="${output.index.html}" />

I know this really isn't new or groundbreaking information here. But, I couldn't find an easy answer trying to google for my problem. I'm posting this to try and save someone else some time in the future who might be running into the same problem that I did. Enjoy!

P.S. I'm starting to like OSX, but I still prefer Linux. I'd consider myself somewhat of a power-user, and there's just too many things Linux has that OSX doesn't. However, I can't get my external monitor xinerama display to work in Linux (doh!) so I think I'm an official OSX user for the time being until I can get my xorg.conf configured right...

No TrackBacks

TrackBack URL:


Hey Darron,

There is one major difference between the open command and the way you used to do this. The open command is not blocking. This means that you can't easily kill the spawned process from your script. This goes even further to mean that you can't run this script on a continuous integration server. I have really been struggling with this problem while working on Sprouts, and haven't really come up with much other than some hacky junk. If you find a solution, please post about it!

Thanks for the comment Luke - I didn't know about the non-blocking issue. Still getting used to the Mac world.. :-)

Interestingly enough, I *am* running this on a CI server and haven't had any problem.

In my case, the main reason for using this technique is to launch a unit test runner and examine the results. I actually have two separate tasks for determining the test result. I use the browser-based tasks to invoke the test runner by hand so I can see results as I build. But, I use a Flash-Player based task as part of the CI build process to have ANT determine if the tests passed or failed.

So, the reason this doesn't give me trouble in the CI world is that I don't use "open" as part of my CI process. Rather, I launch the test harness .swf file directly in the StandAlone flash player, which I can block until the tests are complete.

Hope that helps!

Leave a comment

About this Entry

This page contains a single entry by darron published on December 20, 2007 2:40 PM.

ActionScript 3 Singleton Redux was the previous entry in this blog.

Creating Default, Cancelable Event Handlers is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.


OpenID accepted here Learn more about OpenID
Powered by Movable Type 5.02