Existing users, log in.  New users, create a free account.  Lost password?


MacFixIt Logo
 
Contact Us | About MacFixIt | Who's Online  

Topic Options
#398520 - 06/08/07 07:56 AM Launching a shell script with .command
Anonymous
Unregistered


It used to be the case that if you named a unix shell script with the extension ".command", it would open a terminal window and run when I double clicked on it. I used this trick successfully to write a couple of commands which copy active files to/from my iPod so I can back them up and transport them between work and home. I then dragged aliases of the ".command" files to my desktop so I could run them every day.

The problem is that this has stopped working reliably. Some scripts work and some do not, and I can't seem to figure out why. The scripts in question run correctly if invoked from the command line on the terminal, but not when double clicked. In this case, the terminal application comes to the front, but the new script does not start to run. I can run the command from the terminal prompt, but I can't get it to start by double clicking.

This problem started about the time of the security patch to fix the problem with safari running shell scripts without asking first. I'm having it both on my work computer (running 10.4.9) and my home computer (running 10.3.9). The problem is that I no longer seem to have a method of blessing the scripts I run so that the system knows they are okay to run. And if they are blessed, then editing them (say to add a new project directory) means they no longer run via double click.


Top
#398521 - 06/08/07 08:33 AM Re: Launching a shell script with .command
Hal Itosis Offline
MacWizard

Registered: 08/23/99
Posts: 7032
Loc: 10.5.7 (build 9J61)
Minus a specific question, what can I say but "thanks for the update."



You may or may not know about alternatives like:
    iHook 1.1
    Pashua 0.9.4
    Platypus 3.4
    CocoaDialog 2.1.1
We can also wrap shell scripts in an AppleScript via:
tell application "Terminal"
do script "
blah blah blah"
end tell

(practical for short scripts and one-liners, like the /path/to/your/executable)

Or alternatively, call Unix commands within an AppleScript via do shell script.

There's also Script Menu which (beyond AppleScripts) can also do shell scripts.


As far as commenting on your specific issue, someone might like to see
a ls listing of the ones that don't work... maybe even look at their content.

I just tested a really old '.command' I have stashed and it worked.
(I don't use them much, as I favor the methods mentioned above).

There is also the type ".term" we can create in Terminal which can run... but
they only seem to work via double-clicks when Terminal is *not* already up.

-HI-


Edited by Hal Itosis (06/08/07 09:05 AM)
_________________________

Problems? # Have you <run fsck>? and/or <safe boot>? and/or <reset perms>?

Top
#398522 - 06/08/07 10:11 AM Re: Launching a shell script with .command [Re: Hal Itosis]
MacManiac Moderator Offline
Moderator

Registered: 09/18/01
Posts: 6017
Loc: San Diego, CA (that's my story...
This level of discussion almost makes me wish for a "translate to English" button.

Almond, I can only tell you that you are talking to the right guy for this one.
_________________________
  • MacFixit Forums Moderator

Top
#398523 - 06/08/07 11:33 AM Re: Launching a shell script with .command [Re: MacManiac]
Hal Itosis Offline
MacWizard

Registered: 08/23/99
Posts: 7032
Loc: 10.5.7 (build 9J61)
Depending on what a script does, it could be that neither Terminal nor
any of those special apps I mentioned are even necessary. Like... if there
are no 'echo' statements printing crucial info for the user to read, then
no "screen" output is needed. In such cases, those shell scripts can be
put in ~/Library/Scripts [or /Library/Scripts] somewhere (i.e., any sub-
folder thereof), and run via Script Menu. <--That link is a little outdated.
Tiger users install via: /Applications/AppleScript/AppleScript Utility.app

An extension to that would be to get the free FastScripts Lite (or its
$15 big brother) and assign *keyboard shortcuts* to call/run the scripts!!!
The 'Lite' version is limited to ten shortcuts (just enough to get us hooked).

If there's no text output for us to read, skip Terminal and just run "blind".
[EDIT: script messages in general wind up in console.log].

--

Here's a "poor man's" way to get a simple message (like "script done" or
something) from a background script to the GUI:
code:

# include this function in the background shell script:
function GUImessage ()
{
/usr/bin/osascript <<-HEREDOC
set appName to (info for (path to frontmost application))'s short name
tell application appName
display dialog "$1" giving up after 5 -- [optional 5 second timeout]
end tell
HEREDOC
}

# Put lines like this in the background shell script:
GUImessage "a GUI dialog from a background shell script"


And there's also the "say" command (paste in Terminal to test):
/usr/bin/say backup done $(/bin/date "+%A %H %M")


Edited by Hal Itosis (06/08/07 02:35 PM)
_________________________

Problems? # Have you <run fsck>? and/or <safe boot>? and/or <reset perms>?

Top
#398524 - 06/08/07 12:19 PM Re: Launching a shell script with .command
Andreas.. Offline
MacAuthor

Registered: 12/29/00
Posts: 1756
Loc: UnKnown!
With those that don't work have you tried using full paths even where you think they shouldn't be needed? That is certainly often crucial for AppleScript shell scripts.
_________________________
Andreas

G5 2.1GHz  •  Poking around in OS 10.5.4  •  Working in OS 10.4.10

Top
#398525 - 06/08/07 06:31 PM Clarifications and details.
Anonymous
Unregistered


My question is how do get a shell script to run by double-clicking its icon? The help file for terminal claims it is possible, but I can't get it to work.

A little more testing is revealing that it works under 10.3 but not under 10.4. So it must be something which was "fixed" with the new operating system release. This has "broken" a couple of other commands as well.

In answer to one query, I don't think that there are any path dependencies. The scripts in question look like:

code:

#!/bin/bash
if [ -e /Volumes/rgaPod/Projects ]
then
ipp=/Volumes/rgaPod/Projects
else
echo "iPod not attached."
exit 1
fi

rsync -av --exclude "*~" ${ipp}/graphTutor/ $HOME/Projects/graphTutor/


(with a bunch of other similar rsync calls). The rsync command dumps a list of copied files to the terminal so it is occasionally useful to scan the output to make sure a critical file was copied. (For the most part, these are unix style projects, so I don't need the rsyncX which provides support for resource forks and other similar oddities.)

I don't see any path dependencies, unless the location of bash has moved or there is a hidden problem lurking in my .login or .bashrc file.

I briefly considered writing a wrapper apple script to call my shell script, but it seemed unnecessarily recursive. I've got the shell script doing more or less what I want and I don't want to heavily invest in learning another system when I can just type at the terminal to work around the problem.

Thanks for the suggestions, though.

Top
#398526 - 06/09/07 08:29 AM Re: Clarifications and details.
Hal Itosis Offline
MacWizard

Registered: 08/23/99
Posts: 7032
Loc: 10.5.7 (build 9J61)
In reply to:

My question is how do get a shell script to run by double-clicking its icon? The help file for terminal claims it is possible, but I can't get it to work. A little more testing is revealing that it works under 10.3 but not under 10.4. So it must be something which was "fixed" with the new operating system release. This has "broken" a couple of other commands as well.


Assuming something was fixed and that double-clicking was indeed broken,
what is so bad about a menu-select and/or a keyboard-shortcut solution?
Both can be achieved without AppleScript "wrapping" (or any other kind).

Double-clicking requires navigating to some location to get a "visual" on
the icon, whereas the menu is global as is the keyboard method. (Though
I guess one could always clutter their Dock to obviate window searching).



In reply to:

In answer to one query, I don't think that there are any path dependencies. The scripts in question look like:


I believe Andreas was referring to AppleScript techniques. In the example you
gave, it looks like /usr/bin/rsync is what would be recommended. (The other
commands there are all builtin to the shell). But since you're not going through
AppleScript there, the $PATH lookup is supposed to take care of that. You
could always try using the full path to rsync, to see if it matters. Are the
ownerships and perms *identical* on working and non-working .commands?
Are they located in the same folder? (what happens if they are moved?)



In reply to:

The rsync command dumps a list of copied files to the terminal so it is occasionally useful to scan the output to make sure a critical file was copied.


Most would "| tee |" such info to a log file, for leisurely (and accurate) total recall.



In reply to:

I've got the shell script doing more or less what I want and I don't want to heavily invest in learning another system when I can just type at the terminal to work around the problem.


1st: Having an AppleScript (application) wrapper like
    tell application "Terminal" to do script "/path/to/your/executable"
doesn't alter anything you're doing now.
It just means you double-click a different icon.

2nd: I don't know if your response considered my latest edit, but neither Script
Menu
nor FastScripts Lite involve learning another system. They do handle
Bash as well as AppleScript, Perl, Ruby, etc. The idea is to have all scripts
accessible in a global menu which can be customized (categorized, etc.),
in a very convenient manner. FastScripts adds the ability to quickly edit
or launch (via keyboard) the buried items. Double-clicking .command files
may or may not be broken (works here on the two items I ever created),
but -- if so -- it's certainly no great loss... vis-à-vis the fine alternatives.

I go to this extent of explanation -- not for argument's sake, but -- for the
benefit of *all* readers. (Though, you may wish to reconsider as well).


Edited by Hal Itosis (06/09/07 09:09 AM)
_________________________

Problems? # Have you <run fsck>? and/or <safe boot>? and/or <reset perms>?

Top
#398527 - 07/03/07 06:10 PM Re: Launching a shell script with .command
Anonymous
Unregistered


As a postscript, after I upgraded to 10.4.10, the .command files mysteriously started working again. Somehow whatever got broken was mysteriously fixed. Anybody who has a similar problem, I'd suggest trying the combo updater.

Thanks again for all the suggestions. I think that there are a lot of interesting ideas in this thread.

Top


Moderator:  dianne, dkmarsh, joemikeb, MacManiac 

VersionTracker: Software Updates and Downloads | iPhone Atlas: iPhone Help, News, Tutorials, and Tips