Skip to content

Commit

Permalink
Replace “AppleScript error 1” with warning
Browse files Browse the repository at this point in the history
This error occurs mostly when Emacs doesn’t have System Event permissions, but
it also pops up in other cases sometimes (LionyxML#58). This doesn’t fix the issue, but
downgrades it from an error to a warning, and tries to improve the messaging.
  • Loading branch information
sellout committed Dec 15, 2024
1 parent 56abf3a commit b18c17a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ by going to:


```
Settings -> Privacy & Security -> Emacs -> System Events
System Settings -> Privacy & Security -> Automation -> Emacs -> System Events
```
![](https://github.com/user-attachments/assets/dd45a58f-b417-4255-8339-af7221ba8790)


Also notice if you run emacs from the terminal, `Osascript` is the only method that
Expand Down
30 changes: 23 additions & 7 deletions auto-dark.el
Original file line number Diff line number Diff line change
Expand Up @@ -99,24 +99,40 @@ In order to check if dark mode is enabled. Return true if it is."
"This is AppleScript code that returns an AppleScript boolean value.
It is “true” if the system is in dark mode, and “false” otherwise.")

(defun auto-dark--handle-system-events-access (pattern err)
"Convert an ERR matching PATTERN into a warning about System Events access."
(if (equal pattern (cdr err))
(lwarn 'auto-dark
:warning
"Failed to get the mode from System Events. Please see the README \
(https://github.com/LionyxML/auto-dark-emacs?tab=readme-ov-file#notes-for-macos-users) \
for possible solutions")
(signal (car err) (cdr err))))

(defun auto-dark--is-dark-mode-ns ()
"Check if dark mode is enabled using `ns-do-applescript'."
;; FIXME: We shouldn’t need to check `fboundp' on every call, just when
;; setting the detection method.
(when (fboundp 'ns-do-applescript)
;; `ns-do-applescript' doesn’t support booleans, so we convert to an
;; integer.
(/= 0
(ns-do-applescript (concat auto-dark--is-dark-mode-applescript
" as integer")))))
(condition-case err
;; `ns-do-applescript' doesn’t support booleans, so we convert to an
;; integer.
(/= 0
(ns-do-applescript (concat auto-dark--is-dark-mode-applescript
" as integer")))
(error
(auto-dark--handle-system-events-access '("AppleScript error 1") err)))))

(defun auto-dark--is-dark-mode-mac ()
"Check if dark mode is enabled using `mac-do-applescript'."
;; FIXME: We shouldn’t need to check `fboundp' on every call, just when
;; setting the detection method.
(when (fboundp 'mac-do-applescript)
(string-equal "true"
(mac-do-applescript auto-dark--is-dark-mode-applescript))))
(condition-case err
(string-equal "true"
(mac-do-applescript auto-dark--is-dark-mode-applescript))
(error
(auto-dark--handle-system-events-access '("AppleScript error 1") err)))))

(defun auto-dark--is-dark-mode-osascript ()
"Invoke AppleScript using Emacs using external shell command;
Expand Down

0 comments on commit b18c17a

Please sign in to comment.