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

Starting a program in a new tab

Started by vrtMC, 15 June 2016 - 11:57 AM
vrtMC #1
Posted 15 June 2016 - 01:57 PM
I want some of my programs to start themselves in a new tab.

The programs that I want to run in a new tab are starting with the following code:


local appPath = shell.getRunningProgram()
local shellID = multishell.getCurrent()

if shellID == msID then
  shell.openTab(appPath)
  return
end

--msID is a global variable which has the main shell id stored in it (Which is, as far as I can tell, always 1, so I assume I could change that to ==1)
--the "return" is there to make sure the rest of the program is not running in the main shell.

When I run this in the lua editor it runs perfectly.

But when I run the program outside the lua editor I get the "No such program"-error

So, I think this doesnt work because of something Im not aware of. Therefor my questions are:
1. Why does this not work outside the lua editor?
2. How can I make this work?

Thanks for any help in advance.
Edited on 15 June 2016 - 02:56 PM
Creator #2
Posted 15 June 2016 - 05:39 PM
Is that the whole code? If not, please post it.
vrtMC #3
Posted 15 June 2016 - 05:47 PM
The rest of the code is the actual program, the code for this is


if disk.isPresent("right") and mon then
  local name = disk.getLabel("right")
 
  vrtapi.resetScrn(mon)
  mon.write("Disk loaded:")
  vrtapi.newLine(mon)
  mon.write(name)
elseif mon then
  mon.write("No disk loaded")
else
  print("No monitor available")
end

--vrtapi.resetScrn is a function which clears the screen and sets cursor to 1,1
--vrtapi.newLine is a function which sets the cursor to a new line
--mon is a global variable which holds the wrapped monitor

the vrtapi gets loaded in the startup file.
Dog #4
Posted 15 June 2016 - 06:05 PM
What is the program name? If you type 'ls' (without the quotes) into the computer that has the program does your program show up? What names are listed?
vrtMC #5
Posted 15 June 2016 - 06:11 PM
The program's name is chckdisk

Yes, it does, if you look at the code the programs saves its own path to a variable. And then tries to start that path in a new tab.
Bomb Bloke #6
Posted 16 June 2016 - 04:56 AM
But when I run the program outside the lua editor I get the "No such program"-error

I suggest getting it to print the path it's trying to execute before it actually tries to do so.
vrtMC #7
Posted 16 June 2016 - 11:14 AM
I did that and it is printing the correct path: /apps/chckdisk
valithor #8
Posted 16 June 2016 - 11:37 AM
It is because how shell works when you are passing it paths. If you pass the path "apps/chckdisk" while in the apps folder (is what it is actually printing that "/" before was not there when i tested it) it will look for chckdisk at /apps/apps/chckdisk.

try this:


shell.openTab("/"..appPath)

That will cause it to use a absolute path instead of a relative one.
Edited on 16 June 2016 - 09:42 AM
vrtMC #9
Posted 16 June 2016 - 04:02 PM
That worked like a charm! :)/> Thanks! In other words, the shell is working from the directory you started the program in.

I might have another question which has to do with putting the part of the code which is responsible for starting the program in a new shell in a function/API (since Im probably going to use it for more programs). But before Im going to ask the question Im going to do some research to make sure its not a silly question…
vrtMC #10
Posted 16 June 2016 - 04:48 PM
Ok, I did some research to see if I was certain what I did wasnt working because of a silly mistake.

Basicly the question is, can you refer to the calling program from within a api.function?


In other words, lookinmg at the code I posted before:

The

local appPath = shell.getRunningProgram()
was in the program itself, but now I moved that line to the api.function which will be responsible for starting the program in a new tab. Now when I start the program it gives the "attempt to index ? (a nil value)"-error.

I made sure that the common mistakes, like giving up the wrong fucntion name, wasnt the problem. Also I rebooted the computer to update API after I edited it.

So, can I get the shell.getRunningProgram within the function to look for the path of the program that called the function, or is the only way to pass that path to the function with arguments.
Bomb Bloke #11
Posted 17 June 2016 - 01:48 AM
You'll need to pass the path to the API function via arguments.

As to why, you first need to understand environment tables - by default, in Lua all global variables you declare go into a table known as _G.

When a ComputerCraft system starts, on the other hand, the bios.lua script that's automatically executed creates a new, empty table through which the values of _G can be accessed (via a metatable setup), and sets that as the environment table for the shell script. Likewise, every new instance of shell (including ones in new multishell tabs) gets its own unique environment table.

APIs, on the other hand, are loaded into _G directly, and so each instance of the shell running on a system can access the one copy. The exceptions are the "shell" and "multishell" APIs, which obviously need to be tied to a specific shell instance - how else can eg shell.dir() work? - and so those ones go into the specific environment table for the instance of the shell they're related to.

Which means that other APIs can't access functions in the shell/multishell APIs.
vrtMC #12
Posted 17 June 2016 - 09:24 PM
Ok, thanks for the help.

I kinda realize that ComputerCraft is not the ideal environment to learn LUA with ;)/>. You're just looking up what you (might) need for a program you want to make and skip the rest, which causes you to kinda miss the basics.