12 Replies Latest reply: Jan 22, 2013 7:41 PM by red_menace
aarons510 Level 1 Level 1 (10 points)

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)
  • 1. Re: Can I embed a full shell script inside an applescript?
    Frank Caggiano Level 7 Level 7 (23,830 points)

    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.

  • 2. Re: Can I embed a full shell script inside an applescript?
    aarons510 Level 1 Level 1 (10 points)

    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.

  • 3. Re: Can I embed a full shell script inside an applescript?
    red_menace Level 6 Level 6 (14,615 points)

    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.

  • 4. Re: Can I embed a full shell script inside an applescript?
    twtwtw Level 5 Level 5 (4,690 points)

    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 script ss

    display alert dlf giving 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.

  • 5. Re: Can I embed a full shell script inside an applescript?
    aarons510 Level 1 Level 1 (10 points)

    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?

  • 6. Re: Can I embed a full shell script inside an applescript?
    aarons510 Level 1 Level 1 (10 points)

    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.

  • 7. Re: Can I embed a full shell script inside an applescript?
    Frank Caggiano Level 7 Level 7 (23,830 points)

    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 script ss

    display alert dlf giving 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

  • 8. Re: Can I embed a full shell script inside an applescript?
    red_menace Level 6 Level 6 (14,615 points)

    You would create a script application something like:

     

    set theCommand to POSIX path of (path to resource "myShellScript")

    set theResult to (do shell script theCommand)

    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.

  • 9. Re: Can I embed a full shell script inside an applescript?
    aarons510 Level 1 Level 1 (10 points)

    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.

  • 10. Re: Can I embed a full shell script inside an applescript?
    red_menace Level 6 Level 6 (14,615 points)

    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.

  • 11. Re: Can I embed a full shell script inside an applescript?
    hary5366 Level 1 Level 1 (0 points)

    Saw this thread,which helped me.

    But, I also had similar problem like aarons.

     

    I followed red_menace suggestion and put the script directly in the Resources folder and not in the Scripts folder.

    path to resource gave the resource not found error if the script was in Scripts folder.

     

    Thanks.

  • 12. Re: Can I embed a full shell script inside an applescript?
    red_menace Level 6 Level 6 (14,615 points)

    The path to resource command has an optional in directory parameter that can be used to get resources in subdirectories such as the Scripts folder - see the StandardAdditions scripting dictionary.