From 8a9024dded5733de595329a04dde17a3032f368f Mon Sep 17 00:00:00 2001 From: Greg Pfeil Date: Sun, 15 Dec 2024 13:56:17 -0700 Subject: [PATCH] =?UTF-8?q?Replace=20=E2=80=9CAppleScript=20error=201?= =?UTF-8?q?=E2=80=9D=20with=20warning?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This error occurs mostly when Emacs doesn’t have System Event permissions, but it also pops up in other cases sometimes (#58). This doesn’t fix the issue, but downgrades it from an error to a warning, and tries to improve the messaging. --- README.md | 3 ++- auto-dark.el | 29 ++++++++++++++++++++++------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index b4be0b4..22ec8c9 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/auto-dark.el b/auto-dark.el index 4dda3f5..4eb4055 100644 --- a/auto-dark.el +++ b/auto-dark.el @@ -99,24 +99,39 @@ 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) + (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;