do shell script ruby

I am trying to run a ruby script from within my applescript but it does not seem to working properly. The ruby script is very simple in that it just writes some text to a file.

I have installed ruby using rbenv which I think is causing the problem. If I run the ruby script from terminal then it works but as soon as I run it from within the applescript I get the following error messages:

If I remove the gems from the ruby script, the do shell script runs the ruby script without any problems. I have tried the following commands when using the ruby script with gems:

This runs without any errors but does not write the file to any location as far as I can tell. I have also tried:

This also runs without any errors but again the file is not saved anywhere.

Does anyone have any ideas as to how to get this working.

Hello!

Any environment variables that are setup by ruby, won’t work directly, do shell script doesn’t use the .bashrc or whatever login file you use.

The other thing, which I think is a copy paste error is that ther is no / in front of bin in your last line. But check the environment first.

And / is the homefolder for the do shell script command (I think) so look after any files there.

There is also a TechNote about do shell script at ADC. (Apple Developer Connection)

McUsr, thank you for your reply.

I realise that environment variables are not set up when using do shell script but I thought that by using bash -lc command, it would invoke the environment variables along with the bash shell in a similar way to running the ruby script from the terminal. Is that not the case?

I am not sure, I think -lc should work, but I’d rather use set from both the terminal, and then from a do shell script redirecting the output to two files, and then compare the two files within TextWrangler or similar to be sure.

I really can’t think of anything else than a different environment that should hinder you from running ruby from a do shell script.

Please try the trick above, save environment from both places, and compare with TextWrangler,BBEdit or FileMerge.

Thank you for your help.

Please could you explain what you mean by using “set” - I am not familiar with that.

Thank you.

Hello.

set prints out the current environment, just try to enter set and hit enter in a terminal window.

What I wanted you to do was something like entering this in Terminal:

set >~/Desktop/termsettings.txt

and the execute this snippet from AppleScript:


do shell script "/bin/bash -lc \"set >~/Desktop/doshellsettings.txt\""

And then compare the two settings files with a diff utility.

Edit

It seems to me that I then get the same environment, but I can’t be totally sure as I have set some of it at a higher level. But I see now that you didn’t use doubleticks when executing the /bin/bash -lc for the command string, you must embellish the command string with double ticks like this, I think.


do shell script "/bin/bash -lc \"ruby whatever.rb\""

Thank you very much for your help.

using /bin/bash -lc and embellishing the command with double ticks worked.

Hello!

I am sorry for not seeing that at first, but I don’t use it that way, and as a matter of fact I thought they were not necessary. It apparently turned out to be so.

Happy ruby/AppleScripting! :slight_smile: