Skip navigation

Can I embed a full shell script inside an applescript?

2734 Views 12 Replies Latest reply: Jan 22, 2013 7:41 PM by red_menace RSS
aarons510 Level 1 Level 1 (10 points)
Currently Being Moderated
May 19, 2012 12:53 AM

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)
  • Frank Caggiano Level 7 Level 7 (22,905 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.

  • red_menace Level 6 Level 6 (14,335 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.

  • twtwtw Level 5 Level 5 (4,585 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.

  • Frank Caggiano Level 7 Level 7 (22,905 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

  • red_menace Level 6 Level 6 (14,335 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.

  • red_menace Level 6 Level 6 (14,335 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.

  • hary5366 Calculating status...

    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.

  • red_menace Level 6 Level 6 (14,335 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.

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.