Passing variables from Applescript to bash

Discussion in 'Mac Programming' started by urello, Mar 5, 2014.

  1. urello macrumors newbie

    Joined:
    Feb 28, 2014
    #1
    Hello,

    I'm trying to pass variables from Applescript to bash using the following code
    Code:
    #!/bin/bash
    osascript <<EOF
    tell application "SystemUIServer"
    set username to text returned of (display dialog "Enter your name" with icon caution default answer ""  buttons{"Continue"})
    set macname to text returned of (display dialog "Enter name of your Mac" with icon caution default answer ""  buttons{"Continue"})
    do shell script "export USERNAME=" & quoted form of username & " && export MACNAME=" & quoted form of macname
    end tell
    EOF
    echo "USERNAME is $USERNAME, MACNAME is $MACNAME"
    but no luck
    Code:
    Mac-mini:Downloads admin$./new.command
    
    USERNAME is , MACNAME is
    Any ideas?
     
  2. MorphingDragon, Mar 6, 2014
    Last edited: Mar 6, 2014

    MorphingDragon macrumors 603

    MorphingDragon

    Joined:
    Mar 27, 2009
    Location:
    The World Inbetween
    #2
    Have a look here:
    https://developer.apple.com/library/mac/technotes/tn2065/_index.html

    Mac OSX is Unix, maybe you could pipe output from applescript to a bash script?
     
  3. numero macrumors regular

    Joined:
    Jul 23, 2002
    Location:
    OR
    #3
    The "do shell script" runs and then dies. No export values will be retained after that command. You do not go in and out of the same shell instance. All shell commands that work in the same instance of the shell invocation need to be strung together with semicolons.

    I changed your example a little. I'm not sure if this helps you toward your end goal.

    Code:
    tell application "SystemUIServer"
    	set username to text returned of (display dialog "Enter your name" with icon caution default answer "" buttons {"Continue"})
    	set macname to text returned of (display dialog "Enter name of your Mac" with icon caution default answer "" buttons {"Continue"})
    	do shell script "export USERNAME=" & quoted form of username & " && export MACNAME=" & quoted form of macname & "; echo USERNAME is $USERNAME, MACNAME is $MACNAME"
    end tell
     

Share This Page