AppleScript { public static boolean execute(String appleScript, StringBuilder outputBuffer) { if(!OsFamily.MAC_OS_X.isCurrent()) return false; LOGGER.debug("Executing AppleScript: "+appleScript); String tokens[] = new String[] { "osascript", "-s", "o", }; OutputStreamWriter pout = null; try { AbstractProcess process = ProcessRunner.execute(tokens, outputBuffer==null?null:new ScriptOutputListener(outputBuffer, AppleScript.getScriptEncoding())); pout = new OutputStreamWriter(process.getOutputStream(), getScriptEncoding()); pout.write(appleScript); pout.close(); int returnCode = process.waitFor(); LOGGER.debug("osascript returned code="+returnCode+", output="+ outputBuffer); if(returnCode!=0) { LOGGER.debug("osascript terminated abnormally"); return false; } return true; } catch(Exception e) { LOGGER.debug("Unexcepted exception while executing AppleScript", e); try { if(pout!=null) pout.close(); } catch(IOException e1) { } return false; } }  static boolean execute(String appleScript, StringBuilder outputBuffer); static String getScriptEncoding(); final static String UTF8; final static String MACROMAN; }
@Test public void testScriptOutput() { StringBuilder output = new StringBuilder(); boolean success = AppleScript.execute("count {\"How\", \"many\", \"items\", \"in\", \"this\", \"list\"}", output); if(OsFamily.MAC_OS_X.isCurrent()) { assert success; assert "6".equals(output.toString()); } else { assert !success; } }
@Test public void testScriptError() { assert !AppleScript.execute("blah", new StringBuilder()); }