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: [
%s
]).forEach {
print(\"\\($0.prompt-title)\")
}"
(string-join (mapcar (lambda (file)
(format "URL(fileURLWithPath: \"%s\")" file))
(dwim-shell-command--files))
", ")))
(services (split-string (string-trim (shell-command-to-string (format "echo '%s' | swift -" source)))
"\n")))
(when (seq-empty-p services)
(error "No sharing services available"))
services))
(defun dwim-shell-commands-macos-share ()
"Share selected files from macOS."
(interactive)
(let* ((services (dwim-shell-commands--macos-sharing-services))
(service-name (completing-read "Share via: " services))
(selection (seq-position services service-name #'string-equal)))
(dwim-shell-command-on-marked-files
"Share"
(format
"import AppKit
_ = NSApplication.shared
NSApp.setActivationPolicy(.regular)
class MyWindow: NSWindow, NSSharingServiceDelegate {
func sharingService(
_ sharingService: NSSharingService,
didShareItems items: [Any]
) {
NSApplication.shared.terminate(nil)
}
func sharingService(
_ sharingService: NSSharingService, didFailToShareItems items: [Any], error: Error
) {
let error = error as NSError
if error.domain == NSCocoaErrorDomain && error.code == NSUserCancelledError {
NSApplication.shared.terminate(nil)
}
exit(1)
}
}
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)})
NSApp.run()" 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?