-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathosascripts.el
227 lines (204 loc) · 8.35 KB
/
osascripts.el
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
;;; osascripts.el --- collection of smallish OSA scripts -*- lexical-binding: t; -*-
;; Copyright (C) 2013-2018 Leo Liu
;; Author: Leo Liu <[email protected]>
;; Version: 0.6.0
;; Keywords: OSA, languages, tools, extensions
;; Created: 2013-09-10
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; All symbols use prefix osx-.
;;; Code:
(eval-when-compile (require 'osa))
(require 'cl-lib)
(eval-and-compile
(or (fboundp 'user-error)
(defalias 'user-error 'error)))
(defcustom osx-notify-sound nil
"Default sound for `osx-notify'."
:type '(choice (const :tag "System default" nil) file)
:group 'osa)
(defun osx-emacs-selected-p ()
"Return t if Emacs is currently selected."
(equal (osa "current application is frontmost") "true"))
(defun osx-notify (body &optional title subtitle sound)
"Post a notification using the Notification Center.
SOUND may be the base name of any sound installed in
Library/Sounds."
(let ((title (substring-no-properties (or title "Emacs")))
(subtitle (substring-no-properties (or subtitle "")))
(sound (substring-no-properties (or sound osx-notify-sound ""))))
(osa "display notification #{body} with title #{title} \
subtitle #{subtitle} sound name #{sound}")))
(defun osx-say (text &optional nato)
"Speak TEXT."
(interactive
(let ((text
(if (use-region-p)
(buffer-substring-no-properties
(region-beginning) (region-end))
(read-string (if (current-word t)
(format "Text (default %s): " (current-word t))
"Text: ")
nil nil (current-word t)))))
(or (< (length text) 300)
(yes-or-no-p "Text longer than 300 chars; go ahead? ")
(user-error "Aborted"))
(setq deactivate-mark t)
(list text current-prefix-arg)))
(let ((text (if nato
(with-temp-buffer
(insert text)
(nato-region (point-min) (point-max))
(buffer-string))
text)))
(osa "say #{text} without waiting until completion")))
(defun osx-summarize (beg end &optional number)
(interactive
(if (use-region-p)
(list (region-beginning) (region-end))
(list (point-min) (point-max))))
(let* ((number (or number 1))
(text (buffer-substring-no-properties beg end))
(sum (read (osa "summarize #{text} in #{number}"))))
(when (called-interactively-p 'interactive)
(display-message-or-buffer sum))
sum))
(defun osx-empty-trash (&optional no-confirm)
(interactive "P")
(let* ((items (split-string (read (osa "\
tell application \"Finder\"
set temp to name of items in trash
copy (length of temp) to beginning of temp
if (count temp) > 30 then
set temp to items 1 thru 31 of temp
end if
set AppleScript's text item delimiters to {\"----\"}
return temp as text
end tell"))
"----"))
(count (string-to-number (pop items))))
(and (zerop count) (user-error "No trash"))
(when (or no-confirm
(let ((split-height-threshold 0))
;; See `dired-mark-pop-up' for example.
(with-temp-buffer-window
" *Trashes*"
(cons 'display-buffer-below-selected
'((window-height . fit-window-to-buffer)))
(lambda (window _v)
(unwind-protect (yes-or-no-p
(format "Empty Trash (%s %s)? "
count
(if (eq count 1) "item" "items")))
(when (window-live-p window)
(quit-restore-window window 'kill))))
;; Split items 5-item lines
(cl-loop for f in items
for c from 1
do (princ (format "%d. %s\t" c f))
when (zerop (mod c 5))
do (princ "\n"))
(unless (= (length items) count)
(princ "......")))))
(osa "ignoring application responses
tell application \"Finder\" to empty the trash
end ignoring"))))
(let (default-color)
(defun osx-choose-color ()
"Allows the user to choose a color from a color picker dialog.
The return value is a list similar to that of `color-values'."
(interactive)
(let* ((default (concat "{"
(mapconcat #'number-to-string
(or default-color '(65535 65535 65535))
", ")
"}"))
(color (read (osa
"set myColor to choose color default color #{default#s}"
;; XXX: restore old text item delimiters
"set AppleScript's text item delimiters to {\":\"}"
"return myColor as text")))
(result (mapcar 'string-to-number (split-string color ":"))))
(when (called-interactively-p 'interactive)
(apply #'message
"RGB:#%02x%02x%02x Emacs:%s"
(append (mapcar (lambda (x) (floor x 256)) result)
(list result))))
(setq default-color result)
result)))
(defun osx-get-default-browser ()
"Get the default browser application name."
(with-temp-buffer
(if (and (zerop (process-file "defaults" nil t nil "read"
"com.apple.LaunchServices/com.apple.LaunchServices.secure"
"LSHandlers"))
(progn (goto-char (point-min))
(re-search-forward "LSHandlerRoleAll = \"\\([^\"\n]+\\)\";\n[ \t]*LSHandlerURLScheme = http;" nil t)))
(read (osa "tell application \"Finder\" to get name of application file id #{(match-string 1)}"))
"Safari.app")))
(defun osx-make-browse-url-function ()
"A `browse-url' function that supports file:/// with anchors."
(let ((browser (osx-get-default-browser)))
(lambda (url &rest _args)
(do-applescript
(string-to-multibyte
(format "tell application %S to (open location %S) activate" browser
(substring-no-properties (url-encode-url url))))))))
(defun osx-finder ()
"Open Finder.app and reveal `buffer-file-name' if any."
(interactive)
(let ((dir (expand-file-name
(or (and (fboundp 'dired-file-name-at-point)
(dired-file-name-at-point))
(and buffer-file-name
(file-exists-p buffer-file-name)
buffer-file-name)
default-directory))))
(or (not (file-remote-p dir))
(error "Remote file/directory not supported"))
(osa "set f to POSIX file #{dir}"
"tell application \"Finder\""
"reveal f"
"activate"
"end tell")))
(defun osx-terminal ()
"Open Terminal.app and cd `default-directory'."
(interactive)
(let ((dir (expand-file-name default-directory)))
(or (not (file-remote-p dir))
(error "Remote file/directory not supported"))
(osa
"tell application \"Terminal\"
if not running then
launch
delay 0.5 -- launch can take time
end if
set aDir to the quoted form of #{dir}
try
set aTab to selected tab of the front window
-- if selected tab is not busy, reuse it
if aTab is not busy and processes of aTab doesn't end with \"ssh\" then
do script \"cd \" & aDir in aTab
else
error
end if
on error
do script \"cd \" & aDir
end try
activate
end tell")))
(defun osx-finder-or-terminal (&optional arg)
"Open Terminal.app if ARG else Finder.app."
(interactive "P")
(if arg (osx-terminal) (osx-finder)))
(provide 'osascripts)
;;; osascripts.el ends here