Opening a terminal console and running commands from OD plugin

104 views
Skip to first unread message

Patricia Nedina

unread,
May 27, 2020, 6:40:31 PM5/27/20
to OpendTect Developers
Hi, I'm trying to build a OD plugin that collects some inputs from user and at the end, runs a Terminal Console, do a command and extract the returned code. 
The command will use a path determined by user. 
So, I'm looking for clues to, inside OD: 
* Collect a folder path
* Open a Terminal Console and run my command

Is that possible? If so, can anyone help me? 

Wayne Mogg

unread,
May 27, 2020, 7:00:31 PM5/27/20
to OpendTect Developers
Patricia,
All that is possible. Not sure where you are currently at in your use of OpendTect but to start with I suggest you:
  • Install the Developers Package
  • Follow the instructions in the documentation to setup a plugin development environment
  • Ensure you have installed the required build environment for your hardware/OS
  • Build and test the tutorial attributes/plugins included in the plugin development environment to make sure your development setup is working
  • Have a look though the source code of OpendTect, especially the plugins folder, and also my plugins (https://github.com/waynegm/OpendTect-Plugins) for inspiration where there are lots of examples of using dialog boxes in plugins to collect user input and then do something. The OpendTect libraries also include crossplatform functions/classes to run external processes (Basic/oscommand.h)
Come back to this list if you need help along the way.

Regards,
Wayne Mogg

JB West

unread,
May 27, 2020, 7:22:15 PM5/27/20
to devel...@opendtect.org
Patricia,
   I use this in a plugin to open a console window & execute a command (a .bat or shell script ).
  BufferString *mcmd = new BufferString();
#ifdef _WIN32
mcmd->add("cmd.exe /c start cmd.exe /k ");
#else
mcmd->add("/bin/sh ");
#endif

// cmd contains the script name & any parameters blank separated

mcmd->add(cmd->str());
OS::CommandLauncher cl = OS::MachineCommand(mcmd->str());
std::cout << mcmd->str() << std::endl;
OS::CommandExecPars pars; 
//  Background -- doesn't block OpendTect for long-running scripts
pars.launchtype_ = OS::RunInBG;
pars.isconsoleuiprog_ = true;
bool cstat = cl.execute(pars); 



--
You received this message because you are subscribed to the Google Groups "OpendTect Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to developers+...@opendtect.org.
To view this discussion on the web visit https://groups.google.com/a/opendtect.org/d/msgid/developers/406d3ec4-a967-48f3-b46f-4ac4b14dd4bf%40opendtect.org.

Patricia Nedina

unread,
May 27, 2020, 7:33:18 PM5/27/20
to OpendTect Developers
Hi Wayne, thanks for reply. 

I'm in use of OD. I 'm having a good time with plugin development environment and yours plugins. 

pluginLagritInterface_OD_InitialMenu.png


I will explore oscommand.h   
I forgot to tell that my SO is Ubuntu, but I think it is irrevelevant at this point.

Wayne Mogg

unread,
May 27, 2020, 7:40:21 PM5/27/20
to OpendTect Developers
Patricia,
Looks good. For file selection have a look at uiTools/uifileinput.h which you could use instead of the text box for entering a file name. It includes a text box and a button that will open a file selection dialog.

Regards,
Wayne Mogg

Patricia Nedina

unread,
May 28, 2020, 12:37:06 AM5/28/20
to OpendTect Developers
Hello Wayne, thanks for reply.
 
I'm in use of OD. I 'm having a good time with plugin development environment and yours plugins. I will explore oscommand.h  
My SO is Ubuntu. 

JB, your plugin is for Windows? If so, I will try that way at Windows and then go to Ubuntu. 

JB West

unread,
May 28, 2020, 1:46:51 AM5/28/20
to devel...@opendtect.org
Both windows and Linux. Note the #ifdef _WIN32

Jb

--
You received this message because you are subscribed to the Google Groups "OpendTect Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to developers+...@opendtect.org.

Patricia Nedina

unread,
Jun 16, 2020, 5:27:55 PM6/16/20
to OpendTect Developers
Hi JB, your tip was really great.

I tried this (Linux) and worked:

void openShell()
{
    string _command = "./createFile";
    const char* command = _command.c_str();
    OS::CommandLauncher cl = OS::MachineCommand(command);

    OS::CommandExecPars pars;
    //  Background -- doesn't block OpendTect for long-running scripts
    pars.launchtype_ = OS::RunInBG;
    pars.isconsoleuiprog_ = true;
    bool cstat = cl.execute(pars);
    cout << cstat << endl;
}

My challenge now is change the PATH. I want something like  to  run my script "createFile" from other directory, like Documents/Test

I tried to follow https://stackoverflow.com/questions/786376/how-do-i-run-a-program-with-a-different-working-directory-from-current-from-lin without sucess. I tried check oscommand.h but didn't find a clue. Do you know if it is possible?

Captura de tela de 2020-06-16 17-13-26.png

thx in advance

Em quinta-feira, 28 de maio de 2020 02:46:51 UTC-3, JB West escreveu:
Both windows and Linux. Note the #ifdef _WIN32

Jb

On Wed, May 27, 2020, 9:37 PM Patricia Nedina <patrici...@gmail.com> wrote:
Hello Wayne, thanks for reply.
 
I'm in use of OD. I 'm having a good time with plugin development environment and yours plugins. I will explore oscommand.h  
My SO is Ubuntu. 

JB, your plugin is for Windows? If so, I will try that way at Windows and then go to Ubuntu. 

--
You received this message because you are subscribed to the Google Groups "OpendTect Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to devel...@opendtect.org.

JB West

unread,
Jun 16, 2020, 6:33:54 PM6/16/20
to devel...@opendtect.org
You can just change the command to Documents/test/CreateFile

I use an environment variable set outside OpendTect to set the top level path, and the get it in the plugin & prepend the command with it.
jbw


To unsubscribe from this group and stop receiving emails from it, send an email to developers+...@opendtect.org.
To view this discussion on the web visit https://groups.google.com/a/opendtect.org/d/msgid/developers/b7c9a41a-88ad-4659-bbce-e546f33310bfo%40opendtect.org.

Patricia Nedina

unread,
Jun 22, 2020, 3:54:54 PM6/22/20
to OpendTect Developers
Hi,

I'm still with the PATH problem. My path is outside OpendTect folder. I tried './home/pnedina/Downloads/createFile' without success. But a folder 'test' inside Survey directory works if I use './test/createFile'; So my issue is how set the top level path outside OD flow. Do you mind sharing with me this piece of your code?

In the future I will collect this path from user and I will set it at the code. For now, any outside folder is being used for tests, like Downloads and Documents.

Thx in advance

Wayne Mogg

unread,
Jun 22, 2020, 6:51:26 PM6/22/20
to OpendTect Developers, patrici...@gmail.com
Patricia,
If you remove the '.' at the front of the path, ie '/home/pnedina/Downloads/createFile' it should work. The '.' makes the path start from the current folder which for OpendTect is the current survey folder.
Regards,
Wayne Mogg

Patricia Nedina

unread,
Jun 23, 2020, 9:20:03 PM6/23/20
to OpendTect Developers
Thanks very much Wayne, now "/home/pnedina/Downloads/createFile" works!

I will continue my plugin.

Best regards,

Patricia Nedina

unread,
Jun 24, 2020, 6:48:01 PM6/24/20
to OpendTect Developers
Wayne,

For my purposes I really will need change directory.

I will need chance for directory 1 to run a script from directory 2 with an argument (a file) from directory 3.

So I need something like "cd /completePath1/ ; /completePath2/script < /completePath3/filename.txt", but at plugin command 'cd' isn't reconized neither the third party ('< /completePath3/filename.txt'). I managed to do it using a bash script, so, I ask: Can I do it without bash script? Just using cpp with plugin OD?   

Thanks again,

JB West

unread,
Jun 25, 2020, 12:16:17 AM6/25/20
to devel...@opendtect.org
Why don't you just start the bash script from ODT and pass any relevant arguments?

To unsubscribe from this group and stop receiving emails from it, send an email to developers+...@opendtect.org.
To view this discussion on the web visit https://groups.google.com/a/opendtect.org/d/msgid/developers/9a94f18e-9ba1-4557-89f7-985522cb41deo%40opendtect.org.

Wayne Mogg

unread,
Jun 25, 2020, 12:41:57 AM6/25/20
to OpendTect Developers, jbwes...@gmail.com
Patricia,
 To invoke those commands straight from OD you need to prepend "bash -c" so the command starts up a bash shell. You also need to make sure the commands are properly quoted/escaped. (https://wiki-dev.bash-hackers.org/syntax/quoting)

Patricia Nedina

unread,
Jul 1, 2020, 12:53:20 AM7/1/20
to OpendTect Developers, jbwes...@gmail.com
JB and Wayne,

Thanks again for your help. I was forgetting to use CommandLineParser::addFilePath() to construct my command. I think now I'm ok with this task.
Reply all
Reply to author
Forward
0 new messages