summaryrefslogtreecommitdiffhomepage
path: root/sys/jao-applescript.el
blob: 233186cbae38404a3c94c7044a69274e45196f5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
;;; AppleScript and some macish bits
(autoload 'applescript-mode "applescript-mode"
   "major mode for editing AppleScript source." t)
(setq auto-mode-alist
      (cons '("\\.applescript$" . applescript-mode) auto-mode-alist))

(defun do-applescript (script)
  (with-temp-buffer
    (insert script)
    (shell-command-on-region (point-min) (point-max) "osascript" t)
    (buffer-string)))

(defun jao-as-tell-app (app something)
  (let ((res (do-applescript (format "tell application \"%s\"\n%s\nend tell"
                                     app something))))
    (or (and (stringp res) (substring res 0 -1)) "")))

(defmacro jao-as-get-doc (name application &optional doc)
  `(defun ,name ()
     (interactive)
     (let ((url (jao-as-tell-app ,application
                                 ,(format "get the URL of %s 1"
                                          (or doc "document"))))
           (name (jao-as-tell-app ,application "get the name of document 1")))
       (cons url name))))
(jao-as-get-doc jao-as-safari-doc "Safari")
(jao-as-get-doc jao-as-webkit-doc "WebKit")
(jao-as-get-doc jao-as-camino-doc "Camino" "window")

(defun jao-as-firefox-doc ()
  (interactive)
  (let ((url (shell-command-to-string
              (concat "osascript "
                      (expand-file-name "furl.applescript"
                                        (file-name-directory load-file-name)))))
        (name (jao-as-tell-app "Firefox" "get the name of window 1")))
    (cons (substring url 0 -1) name)))


;;; quicksilver
(defun jao-qs-buffer ()
  "Opens the current file in Quicksilver"
  (interactive)
  (cond ((and buffer-file-name (file-exists-p buffer-file-name))
         (call-process-shell-command (concat "qs \"" buffer-file-name "\"")))
        ;; dired handling
        ((eq major-mode 'dired-mode)
         (dired-do-shell-command "qs * "
                                 current-prefix-arg
                                 (dired-get-marked-files t current-prefix-arg)))
         ;; buffer-menu mode
        ((and (eq major-mode 'Buffer-menu-mode)
              (file-exists-p (buffer-file-name (Buffer-menu-buffer nil))))
         (call-process-shell-command
          (concat "qs \"" (buffer-file-name (Buffer-menu-buffer nil)) "\"")))
        (t
         (error "Not visiting a file or file doesn't exist"))))

 (defun jao-qs-region (start end)
   "Opens the contents of the region in Quicksilver as text."
   (interactive "r")
   (call-process-region start end "qs" nil 0 nil "-"))


(provide 'jao-applescript)