Trouble running a shell script

Hi all,

I’m not exactly new to AppleScript, but I’m having trouble with a finicky shell script. I’ve searched the forums (and the whole internet for that matter) for advice, but I’m stumped and ready to give up. Here’s the problem part of my script:


				try
					do shell script "/usr/sbin/chown -R " & userGroup & " " & pathForShell user name "adminUser" password "adminPassword" with administrator privileges
				end try
				
				try
					do shell script "/bin/chmod -R " & myMode & " " & pathForShell user name "adminUser" password "adminPassword" with administrator privileges
				end try

Nothing fancy, right? Simple shell scripts to repair ownership and permissions on a folder. The 100 or so lines of AppleScript that appear before the do shell script command only serve to define the pathForShell variable from a file that’s dropped in a folder (this is a folder action script). userGroup and myMode are pulled from a data list. So the final shell scripts look something like this:


/usr/sbin/chown -R writer:editorial 'quoted/posix/path/to/restricted/directory'

/bin/chmod -R 775 'quoted/posix/path/to/restricted/directory' 

Simple, right? Everything I’ve read says I’m sending the right code to the terminal (or where ever it is that shell scripts go), but the chown script refuses to run! The chmod script works fine and both scripts work if I copy and paste my results into the terminal. Just that one script won’t work from AppleScript. It’s driving me crazy!

I pulled the sudo command from the shell script since Apple says it’s unneccesary (in the now-famous Technical Note TN2065) I’m wondering if there’s a way to include the password in the shell script since AppleScript doesn’t seem to be passing it to the system. Or even a way to avoid the shell script and set ownership with the Finder.

Beyond that I’m open to suggestions. Thanks for any help.

Model: Dual 1.8 GHz G5 Tower
AppleScript: 1.10.7
Browser: Firefox 3.0.7
Operating System: Mac OS X (10.4)

The try blocks will hide any error generated by do shell script. Such try blocks are nice to let a script continue in the face of non-fatal errors, but it is often still nice to know whether an error occurred and what the error message was. Try adding a display dialog call in an error handler of the try block. Then if an error is generated, you can still see its info.

try
	do shell script "/usr/sbin/chown -R " & userGroup & " " & pathForShell user name "adminUser" password "adminPassword" with administrator privileges
on error m number n
	display dialog m with title "Error " & n
end try

If there is an error message, it will include the ‘stderr’ output from the shell script. This output will likely indicate the source of the problem. If there is an error but the error text still does not enlighten you, then you might try including the values of userGroup and pathForShell in the dialog text so that you can double check them.

Since the subsequent chmod works correctly and it uses the same pathForShell variable (presumably also the same value), then the problem is probably with the value in userGroup (malformed or names a user/group that does not exist).

Thanks for the tip! The dialog gave me the error “A privilege violation occurred” (which makes no sense since I included the password.) Armed with that error code I was able to do a little more digging (i.e. googling) and learned that I fell victim to one of the classic blunders! The most famous is never get involved in a land war in Asia, but only slightly less well-known is this: you can’t promote a user to administrative privileges from within a tell “Finder” statement. In fact, it would appear you are automatically demoted! I guess I should have posted the whole script, tell “Finder” statement and all, but I assumed it was the shell script that was failing. So I moved the shell script to a subroutine and the AppleScript works without a hitch. I even added an error handler, inspired by your tip.

Thanks, again!

Marc