Skip to content

Commit

Permalink
Simplify osascript handling
Browse files Browse the repository at this point in the history
This makes a few changes, somewhat independent, but all related:
* defaults `auto-dark-allow-osascript` to true[^1];
* falls back to `osascript` on-demand if neither `ns-do-applescript` nor
  `mac-do-applescript` is available[^2]; and
* removes `osascript` as a separate `auto-dark-detection-method`[^3].

These can be easily separated depending on which, if any, you think are
improvements.

[^1]: I don’t know why this is even a variable. If it’s not allowed, then there
is no way to use Auto-Dark on systems that require it, and the failure mode is
opaque (“Could not determine a viable theme detection mechanism!”).

[^2]: If this change isn’t made, then the error message “Try setting
`auto-dark-allow-osascript` to t” doesn’t make sense, as it’s only reported in a
location where the value of `auto-dark-allow-osascript` has no effect. The
alternative is to change the error message to something like
“`auto-dark-detection-method` indicates that this Emacs build has AppleScript
support, but none could be found. Either rebuild Emacs with AppleScript support
or change the detection method and set `auto-dark-allow-osascript`”.

[^3]: With LionyxML#59, the two `fboundp` checks added before falling back to
`osascript` should be more than outweighed by the removal of shell invocation.
  • Loading branch information
sellout committed Jul 14, 2024
1 parent 39b168e commit cd56b90
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions auto-dark.el
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Emacs must be restarted for this value to take effect."
:group 'auto-dark
:type 'integer)

(defcustom auto-dark-allow-osascript nil
(defcustom auto-dark-allow-osascript t
"Whether to allow function `auto-dark-mode' to shell out to osascript:
to check dark-mode state, if `ns-do-applescript' or `mac-do-applescript'
is not available."
Expand All @@ -74,7 +74,7 @@ if left as such. Only change this value if you know what you're
doing!"
:group 'auto-dark
:type 'symbol
:options '(applescript osascript dbus powershell winreg termux))
:options '(applescript dbus powershell winreg termux))

(defvar auto-dark--last-dark-mode-state 'unknown)

Expand All @@ -87,7 +87,9 @@ In order to check if dark mode is enabled. Return true if it is."
(auto-dark--is-dark-mode-ns)
(if (fboundp 'mac-do-applescript)
(auto-dark--is-dark-mode-mac)
(error "No AppleScript support available in this Emacs build. Try setting `auto-dark-allow-osascript` to t"))))
(if auto-dark-allow-osascript
(auto-dark--is-dark-mode-osascript)
(error "No AppleScript support available in this Emacs build. Try setting `auto-dark-allow-osascript` to t")))))

(defun auto-dark--is-dark-mode-ns ()
"Check if dark mode is enabled using ns-do-applescript."
Expand Down Expand Up @@ -159,8 +161,6 @@ In order to determine if dark theme is enabled."
(pcase auto-dark-detection-method
('applescript
(auto-dark--is-dark-mode-applescript))
('osascript
(auto-dark--is-dark-mode-osascript))
('dbus
(auto-dark--is-dark-mode-dbus))
('powershell
Expand Down Expand Up @@ -272,15 +272,8 @@ Remove theme change callback registered with D-Bus."
(defun auto-dark--determine-detection-method ()
"Determine which theme detection method auto-dark should use."
(cond
((and (eq system-type 'darwin)
(or (fboundp 'ns-do-applescript)
(fboundp 'mac-do-applescript))
(or (eq window-system 'ns)
(eq window-system 'mac)))
((eq system-type 'darwin)
'applescript)
((and (eq system-type 'darwin)
auto-dark-allow-osascript)
'osascript)
((and (eq system-type 'gnu/linux)
(member 'dbus features)
(member "org.freedesktop.portal.Desktop"
Expand Down

0 comments on commit cd56b90

Please sign in to comment.