Passing multiple variables to shell from Applescript

Hi all,

I'm trying to run an applescript that takes user input and pass it to a shell script. It is only passing one of the variables for the final results.

Here's the Applescript I'm using


delay 5
tell me to activate
display dialog "Please enter the following information."
property RoomNumber : ""
display dialog "Enter the room number and building (ie 1312CAC):" default answer RoomNumber
set the RoomNumber to text returned of the result
property StationNumber : ""
display dialog "Enter the Station Number:" default answer StationNumber
set the StationNumber to text returned of the result
do shell script ("echo " & RoomNumber & ";" & StationNumber & " | /Users/labadmin/Desktop/labbuild.sh")


and here is the shell script:


#!/bin/sh
RoomNumber="$1"
StationNumber="$2"
compname=$RoomNumber$StationNumber
echo $compname
scutil --set ComputerName $compname
scutil --set HostName $compname
scutil --set LocalHostName $compname
echo "Done setting the computer name"


It will echo both variables, but the shell is only taking one for the final result.

Can anyone see what I may have done wrong?

Thank you!!!

G5, Mac OS X (10.4.6)

Posted on Jul 19, 2010 12:58 PM

Reply
6 replies

Jul 19, 2010 2:09 PM in response to Rhian80

Replace your *do shell script* line with the following:

<pre style="width:630px;height:auto;overflow-x:auto;overflow-y:hidden;"
title="Copy this code and paste it into your Script Editor application.">
do shell script "/Users/labadmin/Desktop/labbuild.sh " & RoomNumber & space & StationNumber
</pre>

Hope this helps...

Jul 20, 2010 6:37 AM in response to Chachi

I tried that format and it didn't work, but I tried a variation of it and it was successful. I ended up using:

do shell script ("echo "& RoomNumber $ StationNumber & " | /Users/labadmin/Desktop/labbuild.sh")

and it is now passing both variables to the shell script. A couple more issues and I will be all set!

Thank you!!

Jul 20, 2010 3:41 PM in response to Rhian80

You're welcome Rhian80!

Interesting though... The original *do shell script* line I posted works just fine for me, not sure why you're having difficulties with it. The code you posted last just skips a step in the shell script you're using. It bypasses the second variable. Anyway...

Suggestion... Why not just do everything from within AppleScript and delete the shell script file all together? Try this code...

<pre style="width:630px;height:auto;overflow-x:auto;overflow-y:hidden;"
title="Copy this code and paste it into your Script Editor application.">
set the roomNum to text returned of ¬
(display dialog "Enter the room number and building:" default answer "1312CAC")
set the stationNum to text returned of ¬
(display dialog "Enter the Station Number:" default answer "")
set compName to roomNum & stationNum
do shell script "scutil --set ComputerName " & compName & ¬
";scutil --set HostName " & compName & ¬
";scutil --set LocalHostName " & compName

display dialog "All done." buttons {"Nice"} default button 1
</pre>

Either way, all that matters is it's working for you now; that's what counts in the long run. 🙂

Jul 28, 2010 12:24 PM in response to Chachi

Hi Chachi!

I'm just now getting back to this. I would use Applescript to do everything but I'm the only Apple person on staff and if I get hit by a beer truck tomorrow, someone has to know how to do this stuff. I figure they have better odds of finding someone on staff who knows UNIX.

I'd like to try your version out, but I have a stupid question. How do I get that little character at the end of the lines that looks like the corner of a box?

Thanks again!

Jul 28, 2010 3:52 PM in response to Rhian80

Rhian80 wrote:
I would use Applescript to do everything but I'm the only Apple person on staff and if I get hit by a beer truck tomorrow, someone has to know how to do this stuff. I figure they have better odds of finding someone on staff who knows UNIX.


Another option may be to write the whole thing as a shell script and use Pashua or Platypus to create a GUI around it.

...

One warning about creating and running shell scripts from AppleScript: make sure you correctly escape any user-supplied values when assembling the shell script. The easiest way to do this is by using the text object's 'quoted form' property, like this:

{
}
do shell script "/Users/labadmin/Desktop/labbuild.sh " & quoted form of RoomNumber & space & quoted form of StationNumber
{
}

or, if your shell script expects the room and station numbers to be supplied as a single argument rather than two separate ones:

{
}
do shell script "/Users/labadmin/Desktop/labbuild.sh " & quoted form of (RoomNumber & StationNumber)
{
}

If you forget to do this, Very Bad Things can easily happen when the inputted text contains spaces or other 'special' characters, either by accident or intent, e.g.:

{
}
set user_input to "; say Oops;"
do shell script "echo My name is " & user_input
{
}

Do a web search for 'injection attack' for more on the perils of failing to sanitize your inputs properly.

Incidentally, the latest edition of Apress's Learn AppleScript, which I co-wrote, has an entire chapter devoted to combining AppleScript and Unix shell scripting, if you're new to this subject./self-plug

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Passing multiple variables to shell from Applescript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.