-
Notifications
You must be signed in to change notification settings - Fork 1
/
pullover-osa.el
88 lines (73 loc) · 2.81 KB
/
pullover-osa.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
;;; pullover-osa.el --- Applescript-based functionalities -*- lexical-binding: t; coding: utf-8 -*-
;; Copyright (C) 2019 Tuấn-Anh Nguyễn
;;
;; Author: Tuấn-Anh Nguyễn <ubolonton@gmail.com>
;;; Commentary:
;; This library implements functionalities that use Applescript.
;;; Code:
(require 'frame)
;; XXX: Unquote more reliably.
(defun pullover-osa--unquote (string)
"Unquote a STRING returned by `do-applescript'."
(replace-regexp-in-string (regexp-quote "\"") "" string))
(defun pullover-osa--get-current-app ()
"Return the current (frontmost) app."
(pullover-osa--unquote
(do-applescript "
tell application \"System Events\"
bundle identifier of first application process whose frontmost is true
end tell
")))
(defun pullover-osa--copy-text (bundle-id)
"Copy text from the app identified by BUNDLE-ID.
The copied text is put into the clipboard.
If BUNDLE-ID is nil, copy from the current (frontmost) app instead.
Return the bundle ID of the affected app. If the app is Emacs itself, return nil
without trying to copy.
Unlike `pullover-dyn--copy-text', this does not display a notification message if
the app takes too long to copy the text."
;; TODO: Compare PIDs instead of bundle identifiers.
(let ((bundle-id (if bundle-id
(do-applescript (format "
tell application \"System Events\" to tell first process whose bundle identifier is \"%s\"
set i to bundle identifier
if i is \"org.gnu.Emacs\" then return null
tell menu 1 of menu bar item \"Edit\" of menu bar 1
click menu item \"Select All\"
click menu item \"Copy\"
end tell
i
end tell
" bundle-id))
(do-applescript "
tell application \"System Events\" to tell first process whose frontmost is true
set i to bundle identifier
if i is \"org.gnu.Emacs\" then return null
tell menu 1 of menu bar item \"Edit\" of menu bar 1
click menu item \"Select All\"
click menu item \"Copy\"
end tell
i
end tell
"))))
(if (equal bundle-id "null")
nil (pullover-osa--unquote bundle-id))))
(defun pullover-osa--paste-text (bundle-id)
"Paste text from the clipboard into the app identified by BUNDLE-ID."
(do-applescript (format "
tell application \"System Events\" to tell first process whose bundle identifier is \"%s\"
set frontmost to true
tell menu 1 of menu bar item \"Edit\" of menu bar 1
click menu item \"Select All\"
click menu item \"Paste\"
end tell
end tell
" bundle-id)))
(defun pullover-osa--activate-app (bundle-id)
"Switch to the app identified by BUNDLE-ID (making it frontmost)."
(do-applescript (format "
tell application \"System Events\" to tell first process whose bundle identifier is \"%s\"
set frontmost to true
end tell" bundle-id)))
(provide 'pullover-osa)
;;; pullover-osa.el ends here