Browse Tag

bash

Mac osascript pass variable to bash shell

Just wanted to keep this solution found from several sites.

This scripts uses Mac’s osascript to call a simple dialog and asks user for an input. On return, the result will be passed to the bash variable $UserInput.

A very important note here is that the 2nd EOT line must not include any indent or space in front. It will return an error message saying “161:164: execution error: The variable EOT is not defined. (-2753)”. I was searching high and low for the problem and didn’t realize it is so stringent with indent/space.

#!/bin/sh

# Created by Andrews Ang on 6/29/11.

function GetDialog(){

UserInput=`/usr/bin/osascript « EOT

    tell application “System Events”

    activate

    set myReply to text returned of (display dialog “Here is a question” default answer “Here is a reply”)

    end tell

EOT`

    echo ”${UserInput}”

}

GetDialog