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