├── README.md └── osx_dialog.sh /README.md: -------------------------------------------------------------------------------- 1 | ## Summary 2 | *osx_dialog* is a simple tool which allows developers to easily collect user input via GUI dialog boxes on Mac OS X. It uses AppleScript to show the dialogs but wraps everything in an accessible bash interface. 3 | 4 | ## Usage 5 | Using *osx_dialog is simple, simply include the dialog functions via "source" command: 6 | `source osx_dialog.sh` 7 | 8 | Each dialog is wrapped in a unix function declaration for maximum modularity so you only need call the function and handle the result. 9 | 10 | ## Example 11 | 12 | user_confirm=$(confirm_dialog "Sure to delete?") 13 | 14 | This calls the `confirm_dialog` function to show a confirmation dialog with the prompt "Sure to delete?". The result (0 or 1) is stored in the `user_confirm` variable. 15 | 16 | ### Available Dialogs 17 | You can display three different types of dialogs. 18 | 1. Folder Dialog 19 | 2. Text input dialog 20 | 3. Confirm dialog 21 | 22 | ### Folder Dialog 23 | The `folder_dialog` function simply allows the user to choose a folder from their file system. It consumes no arguments and simply returns the absolute folder path. 24 | 25 | ### Text Input Dialog 26 | The `text_dialog` shows a dialog prompting the user for a single line of input. It accepts one parameter which sets the prompt text and returns the users response. 27 | 28 | ### Confirmation Dialog 29 | The `confirm_dialog` simply shows an alert with an "OK" or "Cancel" button and returns 1 or 0 respectively. It accepts one parameter which sets the prompt text. -------------------------------------------------------------------------------- /osx_dialog.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | # Library which is similar to the "dialog" tool for linux. Instead of using the linux gui toolkit, we use AppleScript to display the dialogs. 4 | # Usage: 5 | # Include this file using the "source" command then use one of the functions. 6 | 7 | # Reference for communicating with apple script: 8 | # http://stackoverflow.com/questions/3502913/how-to-pass-a-variable-from-applescript-to-a-shell-script 9 | # Reference for dialogs: 10 | # http://en.wikibooks.org/wiki/AppleScript_Programming/Advanced_Code_List/Display_Dialog 11 | # Reference for folder dialog 12 | # http://docs.info.apple.com/article.html?path=AppleScript/2.1/en/as309.html 13 | 14 | # displays a file dialog for choosing a *folder* not a file 15 | # @return the file path 16 | function folder_dialog() 17 | { 18 | local file_path=$(osascript << EOT 19 | tell application "Finder" 20 | activate 21 | set fpath to POSIX path of (choose folder) 22 | return fpath 23 | end tell 24 | EOT) 25 | 26 | echo $file_path 27 | } 28 | 29 | # displays a dialog for collecting a single line of text input 30 | # @param $1 the text of the prompt 31 | # @return the input text 32 | function text_dialog() { 33 | local answer=$(osascript << EOT 34 | tell application "Finder" 35 | activate 36 | set response to text returned of (display dialog "$1" default answer "Response...") 37 | return response 38 | end tell 39 | EOT) 40 | 41 | echo $answer 42 | } 43 | 44 | # displays a confirmation dialog with a Yes/No button. 45 | # @param $1 the text of the prompt 46 | # @return the response of the user: "0" - No, "1" - Yes 47 | function confirm_dialog() { 48 | local response=$(osascript << EOT 49 | tell application "Finder" 50 | activate 51 | try 52 | set response to button returned of (display dialog "$1") 53 | 54 | if (response is equal to "OK") then 55 | set response to 1 56 | end if 57 | on error errTet number errNum 58 | if (errNum is equal to -128) then 59 | -- user cancelled - not an error 60 | set response to 0 61 | end if 62 | end try 63 | return response 64 | end tell 65 | EOT) 66 | echo $response 67 | } --------------------------------------------------------------------------------