Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to run a shell script from Finder?

0 views
Skip to first unread message

Greg Ercolano

unread,
Nov 21, 2002, 6:32:37 AM11/21/02
to
I have some shell scripts (perl/sh/csh) that I'd like
to be able have users simply click to run, in the background,
with /no/ terminal window.

As it is, if you click on a script, it opens a /text editor/,
even if the execute bit (chmod a+x) is enabled..!

I know of these two techniques, which are bad for me:

A. Name the script with a ".command" suffix,
so that it runs in a Terminal window.

B. Make a C program wrapper to invoke the scripts,
doing a whole xxx.app/Contents/* hierarchy for it.

"A" needlessly invokes the Terminal application; these scripts
manage their own stdin/out/err redirection, and do not need a
terminal. The terminal app opening is excessive complexity
for the user, who shouldn't have to see it or deal with it.

"B" is complex maintenance wise (need one wrapper executable
and directory hierarchy for each script)

Is there not a simple way to have the Finder simply run the script,
eg. from a network drive with a minimum of hassle, and *no* terminal window?

Patrick Stadelmann

unread,
Nov 21, 2002, 7:41:51 AM11/21/02
to
In article <arig8n$1g3o$1...@madmax.keyway.net>,
Greg Ercolano <er...@3dsite.com> wrote:

> I have some shell scripts (perl/sh/csh) that I'd like
> to be able have users simply click to run, in the background,
> with /no/ terminal window.

Create AppleScript applications, using the "do shell script" command.

Patrick
--
Patrick Stadelmann <Patrick.S...@unine.ch>

Miro Jurisic

unread,
Nov 21, 2002, 9:05:35 AM11/21/02
to
In article <arig8n$1g3o$1...@madmax.keyway.net>, Greg Ercolano <er...@3dsite.com>
wrote:

> I have some shell scripts (perl/sh/csh) that I'd like


> to be able have users simply click to run, in the background,
> with /no/ terminal window.

<http://www.apple.com/downloads/macosx/unix_open_source/dropscript.html>

hth

meeroh

Greg Ercolano

unread,
Nov 27, 2002, 4:32:48 AM11/27/02
to
Miro Jurisic wrote:
> <http://www.apple.com/downloads/macosx/unix_open_source/dropscript.html>

Thanks, but unfortunately the download response was:

> 404 File Not Found
> I ate your Web page.
> Forgive me. It was juicy
> And tart on my tongue.

A link that does seem to work is:

http://web.mit.edu/tritan/www/software/

Will take a look..

Greg Ercolano

unread,
Nov 27, 2002, 4:54:06 AM11/27/02
to
Patrick Stadelmann wrote:
> In article <arig8n$1g3o$1...@madmax.keyway.net>,
> Greg Ercolano <er...@3dsite.com> wrote:
>
>>I have some shell scripts (perl/sh/csh) that I'd like
>>to be able have users simply click to run, in the background,
>>with /no/ terminal window.
>
>
> Create AppleScript applications, using the "do shell script" command.
>
> Patrick

Yes, I see. I've seen references to that before..

Question: Is there a way to make an AppleScript such
that it doesn't need to be compiled into a binary?
Also, is there a way to code the script such that a
hard coded path to the perl script isn't needed? This
way if the user relocates the file, one doesn't have
to modify the hard coded path to the perl script in
the AppleScript wrapper. (I've never used AppleScript).

What I have found that works is to make a simple C program
wrapper, which automatically determines the pathname based
on argv[0]. This is 'cute' in that the binary can be renamed
or relocated, and the program doesn't have to be recompiled.

I've found I can then put this binary in the
myprog.app/Contents/MacOS directory, call it 'myprog',
and have the perl script in the same directory with a .pl extension,
so that the wrapper automatically invokes it.

Curious if there are any benefits to a compiled AppleScript
over this C program..

// wrapper.c -- A simple C wrapper executable to invoke a perl (.pl) script
// er...@netcom.com 11/27/02
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
int t;
char *copy = malloc(strlen(argv[0])+10);
strcpy(copy, argv[0]); // "/path/to/foo.app/Contents/MacOS/foo"
strcat(copy, ".pl"); // "/path/to/foo.app/Contents/MacOS/foo.pl"
return(system(copy) >> 8); // return exit code
}

To use the above:

g++ wrapper.c -o wrapper
cp wrapper /path/to/myprog.app/Contents/MacOS/myprog
cp myprog.pl /path/to/myprog.app/Contents/MacOS/myprog.pl
open /path/to/myprog.app

This runs the perl script, causing the wrapper to invoke
the .pl script of the same name in the same directory.
The fact 'open' can open it means the Finder will too,
and will show an icon for the script, and everything.

The 'foo.app' directory structure is similar to that found in,
eg. the Terminal.app directory structure (the mac's unix terminal app)

I found I was able to make a copy of the Terminal.app directory,
tweak the Info.plist xml file to use my own program's name,
and make my own icon in the Resources directory, etc.

The problem here is a binary is needed, but at least the binary
is self aware, so that it doesn't need to be compiled with the
hard coded path of the perl script; it determines it automatically.
This lets the user move the app directory around anywhere they want,
without having to recompile the wrapper. Not sure if the AppleScript
can do that..?

Patrick Stadelmann

unread,
Nov 27, 2002, 7:11:59 AM11/27/02
to
In article <as24nq$5rk$1...@madmax.keyway.net>,
Greg Ercolano <er...@3dsite.com> wrote:

> Question: Is there a way to make an AppleScript such
> that it doesn't need to be compiled into a binary?

Yes, but I will have to be compiled before execution. This is done
automatically by the Script Editor or the osascript command.

> Also, is there a way to code the script such that a
> hard coded path to the perl script isn't needed? This
> way if the user relocates the file, one doesn't have
> to modify the hard coded path to the perl script in
> the AppleScript wrapper. (I've never used AppleScript).

If the script is not compiled, no. If you store the script
in compiled form and use the "alias" class to store the
file reference, than AppleScript will find it even if it
gets renamed or moved.

Tom Harrington

unread,
Nov 29, 2002, 12:50:04 PM11/29/02
to
In article <arig8n$1g3o$1...@madmax.keyway.net>,
Greg Ercolano <er...@3dsite.com> wrote:

> Is there not a simple way to have the Finder simply run the script,
> eg. from a network drive with a minimum of hassle, and *no* terminal window?

DropScript, <http://www.mit.edu/people/wsanchez/software/>.

--
Tom "Tom" Harrington
Macaroni, Automated System Maintenance for Mac OS X.
Version 1.2: New scheduling options for PowerBooks and iBooks!
See http://www.atomicbird.com/

0 new messages