Skip to content
New issue

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

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

Already on GitHub? # to your account

improve on macos: tries to get text directly via AppleScript from the frontmost application before using command+c #98

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
30 changes: 26 additions & 4 deletions emacs-everywhere.el
Original file line number Diff line number Diff line change
Expand Up @@ -679,10 +679,32 @@ return windowTitle"))
"Insert the last text selection into the buffer."
(pcase system-type
('darwin (progn
(call-process "osascript" nil nil nil
"-e" "tell application \"System Events\" to keystroke \"c\" using command down")
(sleep-for emacs-everywhere-clipboard-sleep-delay) ; lets clipboard info propagate
(yank)))
;; Try to get selected text directly via AppleScript
(let ((selection
(with-temp-buffer
(call-process "osascript" nil t nil
"-e" "tell application \"System Events\"
set frontApp to first application process whose frontmost is true
set frontAppName to name of frontApp
end tell
set theSelection to \"\"
tell application frontAppName
try
set theSelection to selection
if theSelection is not \"\" then
return theSelection
end if
end try
end tell")
(buffer-string))))
;; If direct selection fails, fall back to clipboard
(if (and selection (not (string-empty-p selection)))
(insert selection)
(progn
(call-process "osascript" nil nil nil
"-e" "tell application \"System Events\" to keystroke \"c\" using command down")
(sleep-for emacs-everywhere-clipboard-sleep-delay)
(yank))))))
((or 'ms-dos 'windows-nt 'cygwin)
(emacs-everywhere-insert-selection--windows))
(_ (when-let ((selection (gui-get-selection 'PRIMARY 'UTF8_STRING)))
Expand Down