<?xml version="1.0"?>
<rss version="2.0">

<channel>
	<title>planet openArchitectureWare</title>
	<link>http://www.openarchitectureware.org/planet</link>
	<language>en</language>
	<description>planet openArchitectureWare - http://www.openarchitectureware.org/planet</description>

<item>
	<title>Peter Friese: Running AppleScript from Java</title>
	<guid>http://www.peterfriese.de/?p=387</guid>
	<link>http://www.peterfriese.de/running-applescript-from-java/</link>
	<description>&lt;p&gt;In my current project, I need to launch an external application and maybe execute some additional commands on this external application. Due to the very nature of the project, the whole system will always be run on Mac OS X. So I thought, &quot;why not use AppleScript&quot;?&lt;/p&gt;
&lt;p&gt;Turns out using AppleScript to launch applications is fairly easy, all you have to do is &lt;/p&gt;
&lt;pre&gt;tell application &quot;name of your app&quot; to launch&lt;/pre&gt;
&lt;p&gt;If you want to try this from the command line, &lt;em&gt;osascript&lt;/em&gt; comes in handy:&lt;/p&gt;
&lt;pre&gt;osascript -e 'tell app &quot;iTunes&quot; to launch'&lt;/pre&gt;
&lt;p&gt;So far so good. Should be easy to do this from Java, shouldn't it? Turns out it's not so easy at all. Let's try this:&lt;/p&gt;
&lt;pre&gt;
  String launchCmd = &quot;osascript -e 'tell application \&quot;iTunes\&quot; to play'&quot;;
  process = Runtime.getRuntime().exec(launchCmd);

  BufferedReader bufferedReader = new BufferedReader(
    new InputStreamReader(process.getErrorStream()));
  while ((lsString = bufferedReader.readLine()) != null) {
    System.out.println(lsString);
  }
&lt;/pre&gt;
&lt;p&gt;I'm not sure why, but it results in a nasty &quot;&lt;em&gt;0:1: syntax error: A unknown token can't go here. (-2740)&lt;/em&gt;&quot; error message.&lt;/p&gt;
&lt;p&gt;But there is another signature for Runtime.exec:&lt;/p&gt;
&lt;pre&gt;
  String[] cmd = { &quot;osascript&quot;, &quot;-e&quot;,	&quot;tell app \&quot;iPhone Simulator\&quot; to launch&quot; };
  process = Runtime.getRuntime().exec(cmd);

  BufferedReader bufferedReader = new BufferedReader(
    new InputStreamReader(process.getErrorStream()));
  while ((lsString = bufferedReader.readLine()) != null) {
    System.out.println(lsString);
  }
&lt;/pre&gt;
&lt;p&gt;... and this works out just fine!
&lt;/p&gt;
&lt;p&gt;
Thanks for reading this post. You should follow me on twitter &lt;a href=&quot;http://twitter.com/peterfriese&quot;&gt;here&lt;/a&gt;&lt;/p&gt;</description>
	<pubDate>Mon, 08 Mar 2010 19:42:38 +0000</pubDate>
</item>

</channel>
</rss>
