blob: f40ca102bf423bbba03e26382d6e8fa3ecf0d8d1 (
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
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
|
;;; jao-mac.el --- Running applescript. -*- lexical-binding: t; -*-
;; Copyright (C) 2025 Jose Antonio Ortega Ruiz
;; Author: Jose Antonio Ortega Ruiz <mail@jao.io>
;; Keywords: lisp
;; 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 <https://www.gnu.org/licenses/>.
(require 'jao-shell)
;;; Applescript
(defun jao-mac-run-applescript (script)
(let (start cmd return)
(while (string-match "\n" script)
(setq script (replace-match "\r" t t script)))
(while (string-match "'" script start)
(setq start (+ 2 (match-beginning 0))
script (replace-match "\\'" t t script)))
(setq cmd (concat "osascript -e '" script "'"))
(setq return (shell-command-to-string cmd))
(string-trim return)))
(defun jao-mac-run-applescript* (&rest script-lines)
(jao-mac-run-applescript (mapconcat 'identity script-lines "\r")))
(defun jao-mac-tell-app (app &rest script-lines)
(let* ((app-id (string-split app))
(id (if (> (length app-id) 1) (car app-id) ""))
(app (if (> (length app-id) 1) (cadr app-id) (car app-id)))
(pre (list (format "tell application %s %S" id app)))
(post '("end tell\n")))
(jao-mac-run-applescript* (append pre script-lines post))))
;;; open
(defun jao-mac-open (thing &rest args)
"Invoke open after formatting thing with args, using `format'"
(jao-shell-exec (format "open %s" (apply #'format thing args))))
(defun jao-mac-open-in-skim (&optional file page height)
(interactive)
(let* ((file (if file (expand-file-name file) (buffer-file-name)))
(page (or page (and (derived-mode-p 'doc-view-mode)
(doc-view-current-page)))))
(jao-mac-open "skim://%s%s" file (if page (format "#page=%s" page) ""))))
;; https://alvinalexander.com/macos/applescript-how-to-open-pdf-file-in-preview-go-to-page/
;; This will work as long as Preview is the default app for the file at hand.
(defun jao-mac-open-in-preview (&optional file page height)
(interactive)
(let ((file (if file (expand-file-name file) (buffer-file-name)))
(page (or page (and (derived-mode-p 'doc-view-doc)
(doc-view-current-page)))))
(jao-mac-run-applescript*
(format "tell application id \"com.apple.Preview\" to open (POSIX file %S)\r\r"
(file-truename file))
"delay 1"
"tell application \"System Events\""
"keystroke \"g\" using {option down, command down}"
(format "keystroke %s" (or page 1))
"delay 0.1\rkeystroke return\rend tell")))
;;; DevonThink
(defun jao-devon-tell (&rest script-lines)
(apply #'jao-mac-tell-app "id DNtp" script-lines ))
(defun jao-devon-find-url (file)
(jao-devon-tell
"repeat with db in databases"
(format "set res to lookup records with path %S in db"
(file-truename file))
"if res /= {} then return the reference URL of (item 1 of res)"
"end repeat"
"return \"\""))
(defun jao-devon-show-search (s)
(jao-devon-tell (format "show search %S" s) "activate"))
(defun jao-devon-open (file &optional page height)
(let ((url (jao-devon-find-url file)))
(if (string-empty-p (or url ""))
(let ((jao-browse-doc-use-emacs-p t))
(jao-find-or-open file page height))
(let* ((p (if page (format "?page=%s" (- page 1)) ""))
(u (format "%s%s" url p)))
(jao-mac-open "%s%s" url p)))))
(provide 'jao-mac)
;;; jao-mac.el ends here
|