Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify AppleScript handling #59

Open
wants to merge 8 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
55 changes: 31 additions & 24 deletions auto-dark.el
Original file line number Diff line number Diff line change
Expand Up @@ -94,44 +94,51 @@ In order to check if dark mode is enabled. Return true if it is."
(if (auto-dark--is-dark-mode-mac) 'dark 'light)
(error "No AppleScript support available in this Emacs build. Try setting `auto-dark-allow-osascript` to t"))))

(defconst auto-dark--is-dark-mode-applescript
"tell application \"System Events\" to tell appearance preferences to return dark mode"
"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)
(string-equal "true" (ns-do-applescript "tell application \"System Events\"
tell appearance preferences
if (dark mode) then
return \"true\"
else
return \"false\"
end if
end tell
end tell"))))
(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 "tell application \"System Events\"
tell appearance preferences
if (dark mode) then
return \"true\"
else
return \"false\"
end if
end tell
end tell"))))

(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;
"Invoke AppleScript using Emacs using external shell command;
this is less efficient, but works for non-GUI Emacs."
(string-equal "true"
(string-trim (shell-command-to-string
"osascript -e 'tell application \"System Events\" to tell appearance preferences to return dark mode'"))))
(equal '("true")
(process-lines "osascript" "-e" auto-dark--is-dark-mode-applescript)))

(defun auto-dark--current-mode-dbus ()
"Use Emacs built-in D-Bus function to determine if dark theme is enabled."
Expand Down