<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jayway Team Blog &#187; os x</title>
	<atom:link href="http://blog.jayway.com/tag/os-x/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.jayway.com</link>
	<description>Sharing Experience</description>
	<lastBuildDate>Sat, 11 Feb 2012 10:33:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Send growl notifications from Java using a script engine and AppleScript</title>
		<link>http://blog.jayway.com/2011/04/12/send-growl-notifications-on-os-x-using-a-java-6-script-engine-and-applescript/</link>
		<comments>http://blog.jayway.com/2011/04/12/send-growl-notifications-on-os-x-using-a-java-6-script-engine-and-applescript/#comments</comments>
		<pubDate>Tue, 12 Apr 2011 11:54:02 +0000</pubDate>
		<dc:creator>Tobias Södergren</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[User Experience]]></category>
		<category><![CDATA[AppleScript]]></category>
		<category><![CDATA[growl]]></category>
		<category><![CDATA[os x]]></category>
		<category><![CDATA[script engine]]></category>
		<category><![CDATA[ui]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=7939</guid>
		<description><![CDATA[Growl is a notification system for Mac OS X and "Growl lets Mac OS X applications unintrusively tell you when things happen". The about page shows an example of what Growl looks like. If you have written a Java application and want to enable Growl notifications for it, you previously had the possibility to use [...]]]></description>
			<content:encoded><![CDATA[<p>Growl is a notification system for Mac OS X and "Growl lets Mac OS X applications unintrusively tell you when things happen". The <a href="http://growl.info/about.php">about page</a> shows an example of what Growl looks like. </p>
<p/>
If you have written a Java application and want to enable Growl notifications for it, you previously had the possibility to use cocoa Java bindings for Growl but they have been deprecated and are no longer supported. However, there is an <a href="http://growl.info/documentation/applescript-support.php">AppleScript API</a> that together with the AppleScript script engine can give access to Growl from Java. If you use Java 6 that is.</p>
<h2>The example</h2>
<p>This example code uses the Growl class, defined below, which uses the Growl AppleScript API and Java AppleScript script engine in OS X:</p>
<pre class="brush:java; gutter:false">
public class GrowlDemo {
    public static void main(String... args) {
        // System and boss messages will be enabled by default, spam must be configured in growl settings
        Growl growl = new Growl("Growl Demo",
                new String[]{"system", "boss", "spam"},
                new String[]{"system", "boss"});
        growl.init();
        growl.registerApplication();
        growl.notify("system", "System message", "This seem to be working");
        growl.notify("boss", "From: Big brother", "Get back to work!");
        growl.notify("spam", "Get a diploma", "By going to university");
    }
}
</pre>
<p>The output will look something like:<br />
<div id="attachment_7957" class="wp-caption alignnone" style="width: 322px"><a href="http://blog.jayway.com/wordpress/wp-content/uploads/2011/04/growl-notification-example.png" rel="lightbox"><img src="http://blog.jayway.com/wordpress/wp-content/uploads/2011/04/growl-notification-example.png" alt="Example of Growl notification with demo code" title="growl-notification-example" width="312" height="150" class="size-full wp-image-7957" /></a><p class="wp-caption-text">Demo code output</p></div></p>
<p/>
Now on to the Growl AppleScript API implementation.</p>
<h2>The Growl code</h2>
<p>In order to use the AppleScript engine, an engine and a context has to be retrieved. Here's an example:</p>
<p/>
<pre class="brush:java; gutter:false">
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
ScriptEngine appleScriptEngine = scriptEngineManager.getEngineByName("AppleScript");
Script script = "&lt;AppleScript syntax goes here&gt;";
appleScriptEngine.eval(script, appleScriptEngine.getContext());
</pre>
<p/>
The rest of the code is just building the AppleScript API syntax for Growl:</p>
<p/>
<pre class="brush:java; gutter:false">
package com.jayway.growl;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

/**
 * Notify with Growl using AppleScript and Java Script engine.
 *
 * @author Tobias Södergren, Jayway
 */
public class Growl {
    private static final String GROWL_APPLICATION = "GrowlHelperApp";
    private final String applicationName;
    private String[] availableNotifications;
    private String[] enabledNotifications;
    private ScriptEngine appleScriptEngine;

    public Growl(String applicationName, String[] availableNotifications, String[] enabledNotifications) {
        this.applicationName = applicationName;
        this.availableNotifications = availableNotifications;
        this.enabledNotifications = enabledNotifications;
    }

    public void init() {
        ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
        appleScriptEngine = scriptEngineManager.getEngineByName("AppleScript");
        if (appleScriptEngine == null) {
            throw new RuntimeException("No AppleScriptEngine available");
        }

        if (!isGrowlEnabled()) {
            throw new RuntimeException("No Growl process was found.");
        }
    }

    public void registerApplication() {
        String script = script().add("tell application ")
                .quote(GROWL_APPLICATION)
                .nextRow("set the availableList to ")
                .array(availableNotifications)
                .nextRow("set the enabledList to ")
                .array(enabledNotifications)
                .nextRow("register as application ")
                .quote(applicationName)
                .add(" all notifications availableList default notifications enabledList")
                .nextRow("end tell").get();
        executeScript(script);
    }

    public void notify(String notificationName, String title, String message) {
        String script = script().add("tell application ")
                .quote(GROWL_APPLICATION)
                .nextRow("notify with name ").quote(notificationName)
                .add(" title ").quote(title)
                .add(" description ").quote(message)
                .add(" application name ").quote(applicationName)
                .nextRow("end tell").get();
        executeScript(script);
    }

    private boolean isGrowlEnabled() {
        String script = script().add("tell application ")
                .quote("System Events")
                .nextRow("return count of (every process whose name is ")
                .quote(GROWL_APPLICATION).add(") > 0")
                .nextRow("end tell")
                .get();
        long count = executeScript(script, 0L);
        return count > 0;
    }

    private &lt;T&gt; T executeScript(String script, T defaultValue) {
        try {
            return (T) appleScriptEngine.eval(script, appleScriptEngine.getContext());
        } catch (ScriptException e) {
            return defaultValue;
        }
    }

    private void executeScript(String script) {
        try {
            appleScriptEngine.eval(script, appleScriptEngine.getContext());
        } catch (ScriptException e) {
            // log.error("Problem executing script, e);
        }
    }

    private ScriptBuilder script() {
        return new ScriptBuilder();
    }

    private class ScriptBuilder {
        StringBuilder builder = new StringBuilder();

        public ScriptBuilder add(String text) {
            builder.append(text);
            return this;
        }

        public ScriptBuilder quote(String text) {
            builder.append("\"");
            builder.append(text);
            builder.append("\"");
            return this;
        }

        public ScriptBuilder nextRow(String text) {
            builder.append("\n");
            builder.append(text);
            return this;
        }

        public String get() {
            return builder.toString();
        }

        public ScriptBuilder array(String[] array) {
            builder.append("{");
            for (int i = 0; i < array.length; i++) {
                if (i > 0) {
                    builder.append(", ");
                }
                builder.append("\"");
                builder.append(array[i]);
                builder.append("\"");
            }

            builder.append("}");
            return this;
        }
    }
}
</pre>
<p>That's it.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2011/04/12/send-growl-notifications-on-os-x-using-a-java-6-script-engine-and-applescript/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>HP Laserjet 1020 in OS X Snow Leopard</title>
		<link>http://blog.jayway.com/2010/04/08/hp-laserjet-1020-in-snow-leopard/</link>
		<comments>http://blog.jayway.com/2010/04/08/hp-laserjet-1020-in-snow-leopard/#comments</comments>
		<pubDate>Thu, 08 Apr 2010 06:55:00 +0000</pubDate>
		<dc:creator>Tobias Södergren</dc:creator>
				<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[drivers]]></category>
		<category><![CDATA[hp]]></category>
		<category><![CDATA[laserjet]]></category>
		<category><![CDATA[os x]]></category>
		<category><![CDATA[snow leopard]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=5315</guid>
		<description><![CDATA[I've got 3 different types of Mac computers at home, running Snow Leopard, and on each computer I've had the need to print stuff using my HP Laserjet 1020 model. Each time I used a new computer I realised that there is no built-in support for the printer and each time I had forgot the [...]]]></description>
			<content:encoded><![CDATA[<p>I've got 3 different types of Mac computers at home, running Snow Leopard, and on each computer I've had the need to print stuff using my HP Laserjet 1020 model. Each time I used a new computer I realised that there is no built-in support for the printer and each time I had forgot the steps of getting it to work. This time I figured I better blog about it, maybe someone else also have the same problem.</p>
<h3>Step 1, getting the drivers.</h3>
<ol>
<li>Go to the <a href="http://support.apple.com/kb/dl907">Apple support page</a>, click on the 'Download' button. Be aware that the file is about 350MB.</li>
<li>Install the drivers.</li>
</ol>
<h3>Step 2, select the driver</h3>
<ol>
<li> Turn on the printer and connect it to the Mac. </li>
<li> Open the printer preference page, press the '+' button to add the printer. </li>
<li> In the 'add printer' dialog, make sure that the printer is visible. Open the driver list. </li>
<li> Select HP Laserjet 1022, but not the gutenberg version.</li>
</ol>
<p>The steps above should do the trick.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2010/04/08/hp-laserjet-1020-in-snow-leopard/feed/</wfw:commentRss>
		<slash:comments>110</slash:comments>
		</item>
	</channel>
</rss>

