get return from a shellscript dialog?

2

I'm trying to create a script .sh to capture the action of a dialog on Mac that shows the buttons No and Si , and then execute code depending on the answer, I have not managed to make it take the actions of the user.

This is what I have achieved:

    #!/bin/sh
    #!/usr/bin/env bash

    # error "Message"
    function advertencia() {
    osascript <<EOT
    tell app "System Events"
    display dialog "$1" buttons {"No","Si"} default button 2 with icon caution with title "$(basename $0)"
    return result
    end tell
    EOT
    }

    resultado=$(advertencia "Selecciona una opcion:")

    if ["$resultado" == "Si"]; then
       advertencia "Seleccionaste Si"
    else
       advertencia "Seleccionaste no"
    fi

But it does not work.

    
asked by Angel Montes de Oca 15.09.2017 в 20:28
source

1 answer

1

I found this way to do it and it worked for me:

#!/bin/sh

osascript <<EOT
app "System Events"
set answer to the button returned of (display dialog "Selecciona una opcion" buttons {"Si", "No"} default button 2)
if answer = "No" then
display dialog "Seleccionaste No" buttons {"Aceptar"}
else
if (answer = "Si")then
display dialog "Seleccionaste Si" buttons {"Aceptar"}
end if
end if
return  -- Suppress result
end
EOT
    
answered by 16.09.2017 / 01:37
source