Skip to main content
Source Link
Lri
  • 108k
  • 20
  • 234
  • 285

You can use the run handler to pass arguments:

dialog() {
    osascript -e 'on run args
    try
    tell app "SystemUIServer" to display dialog (item 1 of args)
    end
    activate app (path to frontmost application as text)
    end' "$1"
}

If you want to call it like dialog a b (instead of dialog 'a b'), change $1 to $*.

System Events quits automatically when it is not used, and there is a small delay when it is opened, so telling it to display the dialog would sometimes be slower.

Without the try block there would be an error if the user presses a cancel button. Without activate the previously focused window wouldn't get focus back when the dialog is closed.

You can use something like this to display text dialogs:

answer=$(osascript -e 'try
tell app "SystemUIServer"
set answer to text returned of (display dialog "" default answer "")
end
end
activate app (path to frontmost application as text)
answer' | tr '\r' ' ')
[ -z "$answer" ] && exit