CocoaDialog Help

nnewport
New Contributor III

I've been working with Applescript and have had issues prompting for input because of JAMF running as root. I read some about CocoaDialog on here so I thought I was try it out.

However, does anyone know of a way to not have the button number returned with the input? It seems to always have a 1 or 2 in front of the text input that I do not want. I didn't see an option to only return typed input.

Thanks for your help.

3 REPLIES 3

mm2270
Legendary Contributor III

@nnewport You can put a --quiet flag into your script which will suppress the button numbers from showing up in the script output, but I would encourage you not to do this generally speaking. It's useful to know which button was clicked in your scripts. If you only have an OK button in the dialog, then it's fine to suppress that. If on the other hand you have a Cancel button as well, it's helpful to know when that button (button 2 usually) is clicked so you can take the appropriate action if needed. But it's up to you. Putting in that --quiet flag will get you what you want. Only the text returned or whatever input you were expecting from the user will show up. If nothing is returned it will come back blank.

nnewport
New Contributor III

I gotcha. I guess it would be good to keep them to do an if statement for a cancel button.

I'm not good with using awk and such, but would you have a good way to just pull out what was typed in for a variable without the number?

mm2270
Legendary Contributor III

@nnewport Yep, sure. I use cocoaDialog a lot, so here's some basic code for extracting the button clicked and the text entered, etc, from the user. The idea is, contain the cocoaDialog command into a variable, then pull apart the result from the variable with awk.

#!/bin/sh

dialog=$( /Library/Application Support/JAMF/bin/cocoaDialog.app/Contents/MacOS/cocoaDialog inputbox 
--label "Enter your name" --button1 "  OK  " --button2 "Cancel" )

buttonClicked=$(echo "$dialog" | awk 'NR==1{print}')
textEntered=$(echo "$dialog" | awk 'NR>1{print}')

echo "The button clicked was: $buttonClicked"
echo "The text entered was: $textEntered"

Here's another tip, if you're using the last beta version of cocoaDialog (recommended) you can ensure that you always get a result back from the user when the OK or Enter button is clicked by including a flag called --value-required
This flag ensures the dialog can't be dismissed with OK or whatever the default non-cancel button is without getting something entered. If the default button is clicked with no text entered, a sheet comes down in the dialog explaining that some text must be entered to click OK. There's also a companion flag to this called oddly enough --empty-text This flag can take a string after it that customizes the text in the sheet that comes down in the example described above. Once those are in place, its then safe to use the --quiet flag I mentioned before. So using those extra flags, the example script above would look like this.

#!/bin/sh

dialog=$( /Library/Application Support/JAMF/bin/cocoaDialog.app/Contents/MacOS/cocoaDialog inputbox 
--label "Enter your name" --button1 "  OK  " --button2 "Cancel" 
--value-required --empty-text "Please type in your name before clicking OK. Thanks." --quiet )

if [ ! -z "$dialog" ]; then
    echo "The text entered was: $dialog"
else
    echo "User canceled"
fi

In the example above, the user can still click Cancel to exit the dialog, but with the extra flags I added it means you actually don't need to test for any feedback other than if the variable returned something. If it wasn't empty, they clicked OK. If it was empty, they canceled. But for this to work it needs to include that --quiet flag or else it will never be an empty result, even when they click Cancel.

Hope that makes sense and helps you out. Feel free to ask any other questions. I know a fair amount of how to use cocoaDialog so I'm pretty sure I can answer just about any question you might have on how to use it.