LionyxML / auto-dark-emacs

Auto-Dark-Emacs is an auto changer between 2 themes, dark/light, following MacOS, Linux or Windows Dark Mode settings
GNU General Public License v2.0
140 stars 34 forks source link

occasional “Applescript error 1” when switching modes #58

Open sellout opened 1 month ago

sellout commented 1 month ago

When I googled for this error, #13 was the first of very few results, but that is closed, and I feel like I have some more context and some suggestions.

This doesn’t happen to me on startup (as it seemed to in #13), regardless of whether the system is dark or light when I start Emacs. It also doesn’t happen on every timer firing[^1]. Even when the mode changes, it only happens sometimes, and can happen switching in either direction (dark ↔ light). When it happens, debugger-continue causes the mode to change as expected.

Perhaps the reason it appears to succeed is that it actually doesn’t succeed, but by the time I debugger-continue, the timer has fired again successfully, and the change was merely blocked by the debugger state?

[^1]: As you can infer from the Backtrace, my standard GNU Emacs 29 has ns-do-applescript, but not ns-system-appearance-change-functions, so it uses the timer, but not osascript (and enabling auto-dark-allow-osascript would have no effect).

Backtrace ``` Debugger entered--Lisp error: (error "AppleScript error 1") auto-dark--is-dark-mode-ns() debugger-may-continue = t inhibit-redisplay = nil inhibit-debugger = t inhibit-changing-match-data = nil auto-dark--is-dark-mode-applescript() auto-dark--is-dark-mode() auto-dark--check-and-set-dark-mode() apply(auto-dark--check-and-set-dark-mode nil) timer-event-handler([t 26256 59208 207654 5 auto-dark--check-and-set-dark-mode nil nil 0 nil]) inhibit-quit = t ```
additional debugging info ``` In GNU Emacs 29.3 (build 1, aarch64-apple-darwin23.4.0, NS appkit-2487.50 Version 14.4.1 (Build 23E224)) Windowing system distributor 'Apple', version 10.3.2487 System Description: macOS 14.5 Configured using: 'configure --prefix=/nix/store/pi4gs7hsf41gwf6y02n2ixrqdxnb66ni-emacs-29.3 --disable-build-details --with-modules --disable-ns-self-contained --with-ns --with-native-compilation --with-tree-sitter' Configured features: ACL GLIB GMP GNUTLS JPEG JSON LIBXML2 MODULES NATIVE_COMP NOTIFY KQUEUE NS PDUMPER PNG RSVG SQLITE3 THREADS TIFF TOOLKIT_SCROLL_BARS TREE_SITTER WEBP XIM ZLIB ```

I am using my branch for #57, so there’s a chance it’s related to that, but I don’t think any of the changes there come near this code.

recommendation

Since this seems intermittent and seems to work even when this error occurs, perhaps Auto-Dark should catch Applescript errors and re-report them as warnings with some additional context, like “Received the following error from Applescript when trying to determine whether the system is in dark mode. If Emacs has the appropriate appearance for the current mode, then you may ignore this error, otherwise, please report it at https://github.com/LionyxML/auto-dark-emacs/issues.”

Auto-Dark could also collect some statistics in a few vars about things like

and use these in the warning – either reporting them directly, or determining whether to even show the warning. E.g., if the failure is happening like 1/10 firings or less, maybe don’t even report the warning at all (or write it to *Messages*) – it’s more of an annoyance to the user than helpful, but if it’s failed the last 10 times in a row, we adjust the warning to say “You may need to give Emacs permission to run Applescript in System Settings > Privacy & Security > …” or something[^2].

[^2]: Is it possible to tell Emacs to request the permissions directly?