view src/clojure/java/browse.clj @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
line wrap: on
line source
1 ; Copyright (c) Rich Hickey. All rights reserved.
2 ; The use and distribution terms for this software are covered by the
3 ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4 ; which can be found in the file epl-v10.html at the root of this distribution.
5 ; By using this software in any fashion, you are agreeing to be bound by
6 ; the terms of this license.
7 ; You must not remove this notice, or any other, from this software.
9 (ns
10 ^{:author "Christophe Grand",
11 :doc "Start a web browser from Clojure"}
12 clojure.java.browse
13 (:require [clojure.java.shell :as sh])
14 (:import (java.net URI)))
16 (defn- macosx? []
17 (-> "os.name" System/getProperty .toLowerCase
18 (.startsWith "mac os x")))
20 (def *open-url-script* (when (macosx?) "/usr/bin/open"))
22 (defn- open-url-in-browser
23 "Opens url (a string) in the default system web browser. May not
24 work on all platforms. Returns url on success, nil if not
25 supported."
26 [url]
27 (try
28 (when (clojure.lang.Reflector/invokeStaticMethod "java.awt.Desktop"
29 "isDesktopSupported" (to-array nil))
30 (-> (clojure.lang.Reflector/invokeStaticMethod "java.awt.Desktop"
31 "getDesktop" (to-array nil))
32 (.browse (URI. url)))
33 url)
34 (catch ClassNotFoundException e
35 nil)))
37 (defn- open-url-in-swing
38 "Opens url (a string) in a Swing window."
39 [url]
40 ; the implementation of this function resides in another namespace to be loaded "on demand"
41 ; this fixes a bug on mac os x where the process turns into a GUI app
42 ; see http://code.google.com/p/clojure-contrib/issues/detail?id=32
43 (require 'clojure.java.browse-ui)
44 ((find-var 'clojure.java.browse-ui/open-url-in-swing) url))
46 (defn browse-url
47 "Open url in a browser"
48 {:added "1.2"}
49 [url]
50 (or (open-url-in-browser url)
51 (when *open-url-script* (sh/sh *open-url-script* (str url)) true)
52 (open-url-in-swing url)))