Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Can I embed a full shell script inside an applescript?

I have a friend who is running Tiger on a PPC Mac and wants to download a large number of files from the web. Unfortunately, this friend is barely able to do basic web browsing with Safari or Firefox.


I thought of just sending him a shell script with a lot of curl commands, but I don't suppose making it executable on my Mac would make it executable on his. I would like to be able to send him an Applescript that he could just run by clicking on it, but it would be awkward to make each curl command a separete shell script within the applescript..


Is there a way of directly including in an applescript a multi-line shell script as a single entity that invokes only one shell? I know I can do it by putting the shell script in a separate file and have the applescript give it the necessary permissions, but then I'd have to explain to my friend where to put the shell script!

MacBook, Mac OS X (10.6.8)

Posted on May 19, 2012 12:53 AM

Reply
Question marked as Best reply

Posted on May 19, 2012 5:03 AM

Making it two seprate files will be the easiest way.


Otherwise you're going to have to escape all special characters and if the shell script is more then a few lines it will be a pain and prone to errors.

12 replies

May 19, 2012 12:12 PM in response to Frank Caggiano

Since all but the first couple of lines are identical except for a few digits in a specific location, I only need to know precisely what special characters to escape and how. It should be easy for me to then change all the lines quickly in a text editor. The only potentially 'special' characters in each line are those that occur in every URL, plus HYPHEN and SPACE, if those are 'special'.


The other thing I need to know is what, exactly, encloses the totality of these lines.


TIA.

May 19, 2012 2:05 PM in response to aarons510

The shell script permissions should stay the same, so it would be executable on another system. If it needs to run with elevated access, it would probably be easier to use an AppleScript to get authentication.


Another option would be to create a script application and include the shell script file in the bundle's Resources folder. From there, the path to the shell script can be fetched by using Standard Additions' path to resource command, where it can be used in AppleScript's do shell script command. Note that the shell script will just have a default shell, so you can't rely on any environment variables such as $PATH, and will therefore need to use its full path.

May 19, 2012 3:36 PM in response to aarons510

an example of writing a simple shell script into an applescript:


set ss to "cd ~/Downloads

echo 'This is a file list for the \"Downloads\" folder'

echo

ls -al"


set dlf to do shell scriptss

display alertdlfgiving up after 10


The characters you'll need to escape are double-quotes and backslashes: use \" for literal doublequotes and \\ for literal backslashes. Everything else should be fine, though you'll make your life simpler by using single-quotes where you can.


as red_menace pointed out, this may run without any of the PATH or environment variables you expect to see in a terminal session. that's because do shell script doesn't run to bash_profile files the way Terminal.app does.

May 21, 2012 1:22 AM in response to red_menace

This is something I will try sooner or later, since I may want to send more complex shell scripts in the future. Some immediate questions, though.


1) Can you point me to examples of how to do what you are suggesting?


2) Can I specify a particular shell? WIll the default shell be the same, presumably bash, for everybody? What if I want to use a shell I'm more familiar with, like csh or tcsh?

May 21, 2012 1:33 AM in response to twtwtw

So it's basically a matter of setting the value of a variable to the lines of the shell script? It also appears that line breaks don't have to be escaped, which is good.


Presumably, if I need a PATH to be defined, I can do it at the start of the script. Or I can just use the full path for every program to be invoked.


I don't know what the display alert command does, but I can presumably look it up for myself. I am now too tired to think, so I'll come back very soon.

May 21, 2012 6:58 AM in response to aarons510

While it is possible to do this in the Applescript if the shell script gets at all complicated escaping characters and debugging will be much harder then it needs to be.


For example taking twtwtw's example and just adding one Applescript variable gives:


set dir to POSIX path of (choose folder)


set ss to "cd " & dir & "

echo 'This is a file list for the \"" & dir & "\" folder'

echo

ls -l"


set dlf to do shell scriptss

display alertdlfgiving up after 10


Twtwtw's suggestion of creating an Applescript application bundle is, I believe, the best way to go. You can keep the shell script and Applescript separate making maintenance and debugging much simpler and your friend just gets one 'file' to install and run.


regards

May 21, 2012 7:28 AM in response to aarons510

You would create a script application something like:


settheCommandtoPOSIX pathof (path to resource"myShellScript")

settheResultto (do shell scripttheCommand)

theResult--> standard output from the shell script, if any

Then place myShellScript (or whatever the name of your executable file is) in the Contents/Resources folder of the application bundle (right click on an application and "Show Package Contents" to open the bundle). The do shell script command always calls bash - see Technical Note TN2065 for more information.

May 23, 2012 9:28 AM in response to red_menace

I'm rather confused by your use of four different colors in the 'something like' example you give. It appears that the words in blue, pink and purple are all meant to be used literally, while those in green, namely 'theCommand' and 'theResult', are variable names that can be changed arbitrarily?


BTW, I've always hated applescript becuse of its loose, pseudo-English syntax. I learned programming with Fortran66 and a couple of versions of Basic, with a bit of MAD, Algol, Comit, Snobol, and APL thrown in, mostly for fun. I wrote occasional C programs from the late 1970's into the 1990's, and many csh scripts over the last 30+ years. The only reason I'm resorting to applescript now is that I want to be able to write sometning that will be executable on a Tiger or Leopard system even by somebody who barely knows how to copy or move a file from one folder or volume to another.

May 23, 2012 9:50 AM in response to aarons510

I just pasted code from the script editor - it's been a while since I've used the default settings, so my settings use blue for language keywords, purple for parameter names, green for variable names, and the pink color for scripting addition commands.


You can check AppleScript Editor > Preferences > Formatting for the particular color meanings in your editor.

Can I embed a full shell script inside an applescript?

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