This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
danielcbailey's profile picture

Program works standalone, but not when run from os.run()

Started by danielcbailey, 08 June 2016 - 03:22 PM
danielcbailey #1
Posted 08 June 2016 - 05:22 PM
I have recently have gotten into the world of computercraft and have been working on a personal operating system. When I try to execute a lobby program for paint from the gui program, all works until the lobby program executes shell.run("paint os/images/" .. keystrokes) . I tested the program from the terminal with os/programFiles/Paint/executable and all functions worked. I have noticed similar patterns with other programs that are coming from the gui. In the case of the Paint lobby program, when a button is hit to execute the above shell command, it returns either the length of the argument for paint or "executable:3: attempt to index ? (a nil value)". As I said before, it is not a simple case of forgetting to define a variable, it works when I don't execute it from the gui program via os.run.

Things to note:
1) The GUI program halts its while loop when launching a program.
2) Works when run from the terminal.

Code:
Paint Lobby: http://pastebin.com/vFqh75BF
Gui: http://pastebin.com/skpEE0ee

Any help is much appreciated.
TheRockettek #2
Posted 08 June 2016 - 05:56 PM
to do a shell.run it is shell.run(<program name>,<argument 1>,<argument 2> .etc)

do
shell.run("paint","os/images/" .. keystrokes)
danielcbailey #3
Posted 08 June 2016 - 06:36 PM
It still doesn't work even with the modified shell command. Any other ideas?
DannySMc #4
Posted 08 June 2016 - 07:15 PM
I have recently have gotten into the world of computercraft and have been working on a personal operating system. When I try to execute a lobby program for paint from the gui program, all works until the lobby program executes shell.run("paint os/images/" .. keystrokes) . I tested the program from the terminal with os/programFiles/Paint/executable and all functions worked. I have noticed similar patterns with other programs that are coming from the gui. In the case of the Paint lobby program, when a button is hit to execute the above shell command, it returns either the length of the argument for paint or "executable:3: attempt to index ? (a nil value)". As I said before, it is not a simple case of forgetting to define a variable, it works when I don't execute it from the gui program via os.run.

Things to note:
1) The GUI program halts its while loop when launching a program.
2) Works when run from the terminal.

Code:
Paint Lobby: http://pastebin.com/vFqh75BF
Gui: http://pastebin.com/skpEE0ee

Any help is much appreciated.

iirc this is because you need to give an environment to os.run (you don't have to, but to access some API's you do).
danielcbailey #5
Posted 08 June 2016 - 07:23 PM
DannySMc, what environment do you recommend?
Lyqyd #6
Posted 08 June 2016 - 07:48 PM
The usual way is to use {shell = shell, multishell = multishell} for the environment table.
danielcbailey #7
Posted 08 June 2016 - 08:08 PM
Ok, I modified the os.run for the paint lobby program and it still has an error. The line of code for it: os.run({shell = shell, multishell = multishell}, "os/programFiles/" .. listFileData[1] .. "/executable")
Edited on 08 June 2016 - 06:08 PM
danielcbailey #8
Posted 09 June 2016 - 01:57 AM
Unfortunately I am met by the same issue again in my file viewer when I try to use shell.run for various tasks. Solving the Paint lobby issue should pave the way to solve the file viewer ones. Any other thoughts? Perhaps other environment variables?
Edited on 09 June 2016 - 12:03 AM
danielcbailey #9
Posted 09 June 2016 - 02:31 AM
I was looking more into the shell API and it does need to be loaded into the program. I have tried any environment variables including the ones you guys suggested. How would I go about loading it with something like os.loadAPI()?
Bomb Bloke #10
Posted 09 June 2016 - 02:52 AM
I was looking more into the shell API and it does need to be loaded into the program. I have tried any environment variables including the ones you guys suggested. How would I go about loading it with something like os.loadAPI()?

You don't load the shell API "into programs" and os.loadAPI() won't handle it. But you don't need to and it doesn't need to; you simply need to ensure that the existing shell table exists in the current environment table.

That is to say, you either do:

os.run({shell = shell, multishell = multishell}, "os/gui")

… or you use the function built to handle it all for you:

shell.run("os/gui")

If you're still having trouble, edit the modified code into your paste and quote the error.
danielcbailey #11
Posted 09 June 2016 - 04:13 AM
It worked, thank you! It turned out my startup file was using os.run with {} set to the environment, so the environment of the gui was superficial as well.