Skip to content

Commit

Permalink
Pick dwim-shell-commands-macos-share services using completing-read
Browse files Browse the repository at this point in the history
  • Loading branch information
xenodium committed Oct 12, 2022
1 parent 9198175 commit 20e782b
Showing 1 changed file with 38 additions and 44 deletions.
82 changes: 38 additions & 44 deletions dwim-shell-commands.el
Original file line number Diff line number Diff line change
Expand Up @@ -367,14 +367,30 @@ ffmpeg -n -i '<<f>>' -vf \"scale=$width:-2\" '<<fne>>_x<<Scaling factor:0.5>>.<<
:join-separator ", "
:utils "swift"))

(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.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* ((position (window-absolute-pixel-position))
(x (car position))
(y (- (x-display-pixel-height)
(cdr position)
(line-pixel-height))))
(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
Expand All @@ -384,44 +400,7 @@ ffmpeg -n -i '<<f>>' -vf \"scale=$width:-2\" '<<fne>>_x<<Scaling factor:0.5>>.<<
NSApp.setActivationPolicy(.regular)
let window = InvisibleWindow(
contentRect: NSRect(x: %d, y: %s, width: 0, height: 0),
styleMask: [],
backing: .buffered,
defer: false)
NSApp.activate(ignoringOtherApps: true)
DispatchQueue.main.async {
let picker = NSSharingServicePicker(items: [\"<<*>>\"].map{URL(fileURLWithPath:$0)})
picker.delegate = window
picker.show(
relativeTo: .zero, of: window.contentView!, preferredEdge: .minY)
}
NSApp.run()
class InvisibleWindow: NSWindow, NSSharingServicePickerDelegate, NSSharingServiceDelegate {
func sharingServicePicker(
_ sharingServicePicker: NSSharingServicePicker, didChoose service: NSSharingService?
) {
if service == nil {
print(\"Cancelled\")
// Delay so \"More...\" menu can launch System Preferences
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
NSApplication.shared.terminate(nil)
}
}
}
func sharingServicePicker(
_ sharingServicePicker: NSSharingServicePicker,
delegateFor sharingService: NSSharingService
) -> NSSharingServiceDelegate? {
return self
}
class MyWindow: NSWindow, NSSharingServiceDelegate {
func sharingService(
_ sharingService: NSSharingService,
didShareItems items: [Any]
Expand All @@ -438,7 +417,22 @@ ffmpeg -n -i '<<f>>' -vf \"scale=$width:-2\" '<<fne>>_x<<Scaling factor:0.5>>.<<
}
exit(1)
}
}" x y)
}
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.perform(withItems: [\"<<*>>\"].map{URL(fileURLWithPath:$0)})
service.delegate = window
NSApp.run()" selection)
:silent-success t
:shell-pipe "swift -"
:join-separator ", "
Expand Down

0 comments on commit 20e782b

Please sign in to comment.