Mercurial > lasercutter
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 the3 ; 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 by6 ; the terms of this license.7 ; You must not remove this notice, or any other, from this software.9 (ns10 ^{:author "Christophe Grand",11 :doc "Start a web browser from Clojure"}12 clojure.java.browse13 (:require [clojure.java.shell :as sh])14 (:import (java.net URI)))16 (defn- macosx? []17 (-> "os.name" System/getProperty .toLowerCase18 (.startsWith "mac os x")))20 (def *open-url-script* (when (macosx?) "/usr/bin/open"))22 (defn- open-url-in-browser23 "Opens url (a string) in the default system web browser. May not24 work on all platforms. Returns url on success, nil if not25 supported."26 [url]27 (try28 (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 e35 nil)))37 (defn- open-url-in-swing38 "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 app42 ; see http://code.google.com/p/clojure-contrib/issues/detail?id=3243 (require 'clojure.java.browse-ui)44 ((find-var 'clojure.java.browse-ui/open-url-in-swing) url))46 (defn browse-url47 "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)))