Invoking a shell script graphically?

TommyWillB

Registered
Is there a way I can invoke a shell script by double-clicking a desktop icon?

Certainly I can do a lot with AppleScript, but it is not clear to me if AppleScript can invoke a shell script.

On WinDoze you can do a certain amount of this by coding your command into a shortcut. I can't figure out if there is a way to do this via an Alais.

Is there a way to associate all .sh files so that when double-clicked they launch the terminal and run there? (Not really what I want, but may be okay.)


Even better I'd like this script to run as a particular user... not just the user of the person who clicks it.

Any ideas?
 
from Fred Sanchez's post to the OSX-dev list: (he wrote the app)

The demo I wrote is a small application called DropScript. You drop any executable program (for example, a shell script, a perl script, a C program, ...) onto the DropScript app icon (or launch DropScript and use the open panel), and DropScript will create a new application using your program. The new application, in turn, can be used in Finder to run your program on files. For example, you can write this shell script:

#!/bin/sh
gzip -9 -- "$@"

Save it as GZip.sh. Drop this on DropScript, and you get a new program (in the same location as the script) called DropGZip.sh. Drop any file onto DropGZip, and it will compress the file using GZip for you. You could also get similar results (sans the -9 option; and the '--' keeps it from complaining about files that begin with '-') by simply dropping the gzip executable itself onto DropScript. But shell scripts a smaller and far more flexible than binaries.
 
You can write a shell script - give it a .command extension and make sure it is executable (chmod +x ...)

You can then double click on it in the finder and it will do whatever you tell it to do.
 
Also, associating the application Terminal with the script 'document' in question works fine.

Cheers!
 
Okay, vihung, you win the tip-o-the-day prize for that .command thing. That's a life saver.

-Rob
 
Cool.


Nevertheless, don't put any spaces in the command's filename or path, otherwise the invocation from terminal will fail. One hidden 'feature'... =)


On the other hand, I've successfully invoked the command, which only does an 'open'... the script is executed, but somehow the opened app doesn't show up (it might be killed at the end of the script or something). Any pointers?


dani++
 
You sure wouldn't think the opened app would die right away, but you could try a nohup:

Code:
    nohup open MyApp &

(Interesting. The above formatting is what you get when you use the vB
Code:
 tags.)

-Rob
 
Definitely doesn't work, even using 'nohup'.

If I invoke the file from the terminal it works great, but if I double click on it the 'open' command doesn't work.


Thanks for the suggestion anyway.

I'll post here if I achieve some success.


Dani
 
There's a property to the left of the other properties if you look at the permissions on the command line. the sticky bit or whatever. Anyway, if you do a mn on chmod you'll find Set UID on execution. If you apply chmod u+s (or something like that) and the owner of the file is root, then the file should be executed as root.

I've done this on X Server, and it works, but be careful, and I don't know how much of this is still applicable on X. I'd assume all of it.
 
With my addition, this string officially belongs in the Unix Newbie forum. :) I'll try to keep it short. I've created a shell script app with DropScript containing a simple command <b><blockquote>ssh username@servername</blockquote></b>It didn't work. I tried adding <b><blockquote>#!/bin/sh</blockquote></b>to the beginning and it still didn't work.

Changing the extension to .command and making the app executable (chmod +x {appname}) made it so double-clicking the app launched Terminal, but nothing else happens.

Of the extensive list of things that I could be doing wrong, I would be most appreciative if someone would do two things:

1) Help me understand what I did wrong.

2) Help me get this script working.
 
I find that in scripting, you shouldn't count on paths resolving, so ssh should maybe be
/usr/local/sbin/ssh bob@turbopron.com
 
Well I did a <blockquote><b>which ssh</b></blockquote> just to make sure I was putting in the correct path. It didn't help.

Thanks for the input anyway!

Anyone else?
 
Well, you'd think it would work the way you had it, but I tried it this way:
Code:
#!/usr/bin/ssh rob@localhost
in a ssh.command file. It executed from the Finder, but the .command thing isn't expecting input, apparently. I got a "You have no controlling tty. Cannot read passphrase" error.

Is your goal to make some sort of shortcut to launching a ssh session easily?

-Rob
 
Back
Top