index all rss twitter github linkedin email
Álvaro Ramírez

Álvaro Ramírez

12 October 2022 Improving on Emacs macOS sharing

A quick follow-up to Emacs: macOS sharing (DWIM style)… Though functional, the implementation had a couple of drawbacks.

Tohiko noticed fullscreen wasn't working at all while Calvin proposed enumeration for tighter Emacs integration.

Calvin's suggestion enables using completing-read to pick the sharing service. This makes the integration feel more at home. As a bonus, it also enables sharing from fullscreen Emacs.

As an ivy user, you can see a vertical list of sharing services.


Here's the new snippet, now pushed to dwim-shell-commands.el:

(defun dwim-shell-commands--macos-sharing-services ()
  "Return a list of sharing services."
  (let* ((source (format "import AppKit
                         NSSharingService.sharingServices(forItems: [
                         ]).forEach {
                         (string-join (mapcar (lambda (file)
                                                (format "URL(fileURLWithPath: \"%s\")" file))
                                      ", ")))
         (services (split-string (string-trim (shell-command-to-string (format "echo '%s' | swift -" source)))
    (when (seq-empty-p services)
      (error "No sharing services available"))

(defun dwim-shell-commands-macos-share ()
  "Share selected files from macOS."
  (let* ((services (dwim-shell-commands--macos-sharing-services))
         (service-name (completing-read "Share via: " services))
         (selection (seq-position services service-name #'string-equal)))
      "import AppKit

       _ = NSApplication.shared


       class MyWindow: NSWindow, NSSharingServiceDelegate {
         func sharingService(
           _ sharingService: NSSharingService,
           didShareItems items: [Any]
         ) {

         func sharingService(
           _ sharingService: NSSharingService, didFailToShareItems items: [Any], error: Error
         ) {
           let error = error as NSError
           if error.domain == NSCocoaErrorDomain && error.code == NSUserCancelledError {

       let window = MyWindow(
         contentRect: NSRect(x: 0, y: 0, width: 0, height: 0),
         styleMask: [],
         backing: .buffered,
         defer: false)

       let services = NSSharingService.sharingServices(forItems: [\"<<*>>\"].map{URL(fileURLWithPath:$0)})
       let service = services[%s]
       service.delegate = window
       service.perform(withItems: [\"<<*>>\"].map{URL(fileURLWithPath:$0)})" selection)
     :silent-success t
     :shell-pipe "swift -"
     :join-separator ", "
     :no-progress t
     :utils "swift")))

dwim-shell-command is available on melpa. What other uses can you find for it?