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

2 Comments

  • biff

    November 14, 2015

    You can actually not use the EOT’s at all and just write your Applescript as normal without worrying about spacing:

    UserInput=$(/usr/bin/osascript -e
    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)

    By putting the osascript in $( ), the EOT is not needed and makes copying/pasting Applescript directly into your shell script.

    Reply
    • Andrews Ang

      November 16, 2015

      Wow thanks! It’s been a while since I wrote this. Won’t be able to test it but hope this helps other people too.

      Reply

Leave a Reply