Skip to content

Instantly share code, notes, and snippets.

@onetom
Created June 30, 2023 17:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onetom/0291878c1a4b00819f8297bbfa330b7f to your computer and use it in GitHub Desktop.
Save onetom/0291878c1a4b00819f8297bbfa330b7f to your computer and use it in GitHub Desktop.
Pop up an Apple Script Yes/No dialog from Clojure
(ns dialog
"Display a Yes/No dialog, which appears on the very top of every app and grabs
the focus and also gives it back to the REPL, after making the choice, unlike
a Swing dialog, for example.
It's useful for confirming irreversible operations, like file or DB deletion.
It uses AppleScript, so it only works on macOS and on a local REPL."
(:require
[clojure.java.shell :as shell]))
(def yes-no-dialog:scpt
"Literature:
* https://stackoverflow.com/questions/29258142/how-can-i-open-applescript-app-with-arguments
* https://stackoverflow.com/a/29266283/263983
* https://apple.stackexchange.com/questions/353040/how-to-get-error-status-back-to-bash-from-osascript
* https://apple.stackexchange.com/a/353057/60276
`error number` always returns exit code 1, regardless of its argument.
"
"
on run argv
display alert (item 1 of argv) buttons {\"No\", \"Yes\"} giving up after 600
if button returned of result = \"No\" then
error number 1
else
if button returned of result = \"Yes\" then
end if
end if
end run
")
(defn yes-no? [question]
(-> (shell/sh "osascript" "-s" "o" "-e"
yes-no-dialog:scpt
question)
:exit
(case 1 false
0 true)))
(comment
(when (yes-no? "Delete database?"))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment