shell script correct but not working

Hi,
Need help from a AppleScript guru with extended experience with shell script :smiley:

I’m trying to make git-gui an Application.
When called from Terminal within the correct directory (/Users/me/mygitrepo) it works FINE.
But if it’s called form /Users/me or elsewhere it doesn’t work.

also doesn’t work in Terminal.

So the only way is to ‘cd’ to the correct directory then call git-gui.
Form Terminal I tried:

and it works fine, so I thought putting the same stuff in AppleScript should work … but it doesn’t.

set myPath to text returned of (display dialog "Enter git path" default answer "/Users/me/" buttons {"OK"} default button 1)
display dialog "cd " & myPath buttons {"OK"} default button 1
do shell script "cd " & myPath & "; /usr/local/bin/git-gui"

it asks for the git repo you want to work on (you enter mygitrepo)
then confirm that variable is correct
then cd to it
and call git-gui

The latest doesn’t work. Same error code than if I run it without path.

Model: MacBook
Browser: Firefox 2.0.0.11
Operating System: Mac OS X (10.4)

Off hand, the only thing I can think of is that your path has some character in it that is special to the shell. You should always use quoted form of when passing arguments of dynamic content to the shell with do shell script. Also, since the git repository is always a folder, I might rewrite the selection part like this:

choose folder with prompt "Choose your git repository (press Command-Shift-G to enter a POSIX path)" default location (path to home folder) with invisibles without multiple selections allowed
set myPath to POSIX path of result
do shell script "cd " & quoted form of myPath & "; /usr/local/bin/git-gui"

That last line shows you how and where to use quoted form of if you want to try with your original display dialog prompting method.

Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari 3.0.4 (523.12)
Operating System: Mac OS X (10.4)

thanks chrys!! :wink:

your script is far better than mine! :smiley:
mine was my first AppleScript EVER. I made it with snippets from here and there on internet (where’s the manual??).

BTW, I gave yours a try with both click and POSIX paths and it’s not working.
git-gui isn’t verbose and has … NO manual too (Mac or Linux).
But still, if it works in Terminal, it should work in AppleScript, right??? :rolleyes:

I found a manual for git-gui via google, and it needs ‘current working directory’, if you how AppleScript can set it before firing shell script??

The environment for the shell in do shell script and Terminal windows are not quite the same. They are both probably bash but they can have different setting and environment variables. The environment for do shell script is pretty ‘bare bones’ for security reasons (just a basic PATH, etc.). Did you add anything to .bashrc, .bash_profile, .profile or anything like that to get git to work from Terminal windows? If so, that will need to be replicated for the commands that you run in do shell script.

I do not know of a way to set the cwd (current working directory) in AppleScript before do shell script starts its shell, but it would probably be useless anyway since do shell script always seems to set the cwd to the root directory.

As an example, if you needed to modify the PATH you could do something like this:

set gitBinDir to "/usr/local/bin/"
do shell script "cd " & quoted form of myPath & ";export PATH=\"$PATH:\"" & quoted form of gitBinDir & ";git-gui"

Are you getting any error messages when it fails or does it just silently seem to do nothing?

to reply to a previous question, git-gui on applescript warn with a message:
AppleScript Error
“The command exited with a non-zero status”
that’s it that’s all.
when git-gui is executed from a wrong directory in bash its a different message from wish (tcl tk).

choose folder with prompt "Choose your git repository (press Command-Shift-G to enter a POSIX path)" default location (path to home folder) with invisibles without multiple selections allowed
set myPath to POSIX path of result
do shell script "cd " & quoted form of myPath & "; ls -l"

WORKS!
it prints out the folder content
so cwd seems setable which is good news for other scripts.

git-gui command is a script which test if it’s a linux, a Mac OS X or cygwin on windows.
I guess that the command breaks somewhere.
I’m not good enough at script to add or tweak it.

If you feel you can check and tweak it for AppleScript to execute it let me know and I’ll post it.
I feel stuck

I found a git-gui.sh with Google, but I am not sure if it is the source from which your git-gui script is derived. It does a good bit more than just probing for the underlying OS. It seems to be the main driver/launcher for the whole git-gui program. Although the first line says #!/bin/sh, it is actually Tcl code. It just uses sh to bootstrap itself (usually this is done to avoid having to hard code the path to tcl/wish/whatever into the ‘shbang’ line; having sh start the interpreter means the interpreter can be anywhere in the PATH). Anyway, depending on the installation-time options supplied, it looks like this script is modified in various ways, so I can not be sure what yours actually looks like. Besides, I do not really know Tcl, so I would mostly have to guess at what the Tcl code is doing.

Since you are able to successfully run the program in a Terminal window, give this variation a try:

-- . setup myPath .
set gitGuiPath to "/usr/local/bin/git-gui"
do shell script "bash -lc  'cd '" & quoted form of quoted form of myPath & "'; '" & quoted form of quoted form of gitGuiPath

The quoting is a bit hairy, but it should be correct. There are two levels of quoting, one for the sh instance that is started by do shell script and one for the bash instance that is started by do shell script’s sh. Assuming that your login shell is bash (it might be tcsh if you migrated/upgraded from a pre-10.3 installation) and that there are no inappropriate assumptions in your login scripts, this will start a shell that should be nearly identical to one running in a fresh Terminal window. Then it tells that shell to change directories and run your git-gui.

If git-gui has no significant command-line output, you could tell Terminal to launch it like this:

-- . setup myPath .
set gitGuiPath to "/usr/local/bin/git-gui"
tell application "Terminal"
	do script "cd " & quoted form of myPath & "; exec " & quoted form of gitGuiPath
end tell

This will open a Terminal window while git-gui runs, but if you cannot get it to run any other way, it may be something you decide you can tolerate.

Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari 3.0.4 (523.12)
Operating System: Mac OS X (10.4)

MAN you DID it Chris!! :lol:

It WORKS!

choose folder with prompt "Choose your git repository (press Command-Shift-G to enter a POSIX path)" default location (path to home folder) with invisibles without multiple selections allowed
set myPath to POSIX path of result
set gitGuiPath to "/usr/local/bin/git-gui"
do shell script "bash -lc 'cd " & myPath & "; " & gitGuiPath & "'"

I just copy pasted your first script and it worked as is. :smiley:
I just cleaned quotes step by step to get the shortest (removing ‘…’ breaks it).
Script is running until I quit git-gui but it’s not a problem in itself.

So we can finally works with latest git-core on Mac WITHOUT any terminal commands.
I hope this script will be useful for others.

I turned it to app and added a nice icon.

Thanks again Chris, and I wish you and your gang a very happy new year!!!

I am glad that you were able to get it to work. If you care to isolate the problem, you should identify the differences in the environment variables between the sh from do shell script and your normal Terminal-based bash. Some environment variable that is different in your login environment (probably set in .bash_profile, .bash_login, .profile, .bashrc, etc.) is required by git-gui.

Hmm, the way it was quoted should have worked. I did test it, though I had to use something other than git-gui (in this case, getting the quoting correct is not dependent on which program is run). There are latent bugs with the quoting in the script in your most recent post. For the quoting situation in that most recent script, if either gitGuiPath or myPath have any double quotes, dollar signs, back quotes, or backslashes in them (all the special characters in a sh double quote context), the script will not function properly.

Quoting has a simple rule of thumb: Wrapping a string value with quotes (double, single, or other) is never the proper way to represent a string literal value (in any language, not just AppleScript+sh). At the very least one would have to escape any quote characters that would mark the end of the quoted data (otherwise that embedded quote would prematurely terminate the string). As an example, not doing proper quoting of string values in SQL statements results in SQL Injection vulnerabilities (comic).

Of course, your script will work OK as you only use paths do not contain any of the special characters, but the latent, security bug will still be there. The general idea is that just because something works does not mean that is is bug-free.