Applescript: Run or Call a Shell Script

by on January 26, 2009 · 5 comments· LAST UPDATED July 16, 2011

in , ,

How do run a shell script with an AppleScript? How do I integrate shell scripts into AppleScript? How do I call a shell script called /path/to/chkhost.sh using an applescript?

AppleScript is a scripting for Mac OS. It is the Mac OS scripting interface, which is meant to operate in parallel with the graphical user interface. With AppleScript, you can control, and communicate among, applications, databases, networks, Web services, and even the operating system itself.

Running Shell Commands From AppleScript Scripts

You can easily execute shell commands from your AppleScript using the following syntax:

do shell script "command"
do shell script "command1; command2"
set variableName to do shell script "command"
set variableName to do shell script "command1; command2"
do shell script "/path/to/yourscript.sh"
do shell script "/bin/tcsh /path/to/yourscript.csh"
do shell script "/bin/tcsh -c  'command1'"
 

The following script statement uses a do shell script command to get list of the files from the current directory and store it into the AppleScript variable filesLists:

set filesLists to do shell script "ls"

Task: Pass an AppleScript Variable To Shell Command

Use the following syntax (h is an AppleScript variable which is passed to traceroute shell command):

set h  to "cyberciti.biz"
do shell script "traceroute " &
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 5 comments… read them below or add one }

1 mike July 16, 2011 at 5:38 am

Thanks, now I want to simply copy files from the same dir as the bash script. Applescript and also *.command mess the current directory so the files won’t get found. Users can run it the classic way ./bashscript but I want it clickable in whatever way.

Reply

2 sid July 18, 2011 at 5:46 am

Hello,

I have the same problem as above.
I want to create an apple script which runs a pre written shell script (.sh) file.

my apple script file and .sh file are in the same folder.
I have written the following line in apple script file.

do shell script “sh configCS.sh”

so it gives me following error.
error “sh: configCS.sh: No such file or directory” number 127

please guide me whatever is missing.

Reply

3 byron January 2, 2012 at 10:27 pm

I do what you say, but my script is not able to stop, and it doesn’t work here is my shell script while (true) do echo -n ” GO BADGERS “; sleep .008; done

Reply

4 staid June 17, 2012 at 2:54 am

You said about passing applescript variable to shell. But now I get a problem to passing shell variable to applescript. How can I do this?

Reply

5 bug September 7, 2012 at 8:32 pm

set shellvar to do shell script “echo ${varname}”

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , ,

Previous Faq:

Next Faq: