Class Reference process

Executes software and scripts outside the LayoutEditor. More...

Member

int execute(string command,stringList arguments)
string executeTcl(string command)
bool externalTool(string path,string name,stringList parameter,string callback,bool triggerOnOutput=false)
stringList getCommandlineParameter()
string getEnv(string parameter)
bool isLinux()
bool isMac()
bool isRunning(string toolName)
bool isWindows()
int setEnv(string parameter,string value)
bool openBrowser(string url)
string startPythonScript(string filename,stringList args=stringList())
string startTclScript(string filename)
void terminate(string toolName)
string toolData()
string toolResult()
string trigger()
bool startDetached(string command,stringList arguments)

Detailed Description

process is a static hub for starting programs and scripts outside the LayoutEditor. There is no object to construct: call process::execute, process::externalTool, or process::getEnv. execute runs a command, waits until it finishes, and returns the exit code. externalTool starts a tool and returns at once; a callback macro runs when the tool finishes, or on output if triggerOnOutput is true. isRunning and terminate use the same name as externalTool. startPythonScript and startTclScript run scripts; executeTcl runs one Tcl command. getEnv and setEnv read and write environment variables. isLinux, isMac, and isWindows report the build of the LayoutEditor. getCommandlineParameter returns the arguments the LayoutEditor was started with as a stringList.

Example:

stringList args;
args.append("--help");
int code = process::execute("ls", args);
string home = process::getEnv("HOME");
if (process::isLinux())
 process::openBrowser("https://layouteditor.org");

Member Function Documentation


static int process::execute(string command,stringList arguments)

Runs command with arguments and waits until it terminates. If arguments is a string it is split on spaces to a stringList.

Parameter:
command executable to run
arguments command-line arguments

Returns: the exit code of command.


static string process::executeTcl(string command)

Runs a Tcl command and returns its output.

Returns: the Tcl output as a string.

string s=process::executeTcl("puts $application\n");

static bool process::externalTool(string path,string name,stringList parameter,string callback,bool triggerOnOutput=false)

Searches for and starts tool name with parameter. path is a hint where to find the tool. The callback macro is called as soon as the tool finishes. This method returns immediately and does not wait until the tool terminates. true is returned if the tool was started successfully.

If the optional triggerOnOutput is true, the callback macro is also called regularly when the tool writes output.

Parameter:
path directory hint used to find the tool
name tool name, also used later with isRunning and terminate
parameter command-line arguments
callback macro file called when the tool finishes (or on output)
triggerOnOutput if true, call the callback on output as well

Returns: true if the tool was started, otherwise false.

#!/usr/bin/layout
#name=start bash script
#help=start bash script with callback macro

int main(){
 file f;
 string path=f.currentPath();
 stringList sl;
 sl.append(path+"/script");
 sl.append(layout->filename);
 if (process::externalTool("/bin","bash",sl,path+"/callback.layout")) {
 layout->showStatus("bash script started");
 } else {
 layout->showStatus("failed to start bash script");
 }
}

static stringList process::getCommandlineParameter()

Returns: the command-line parameters the LayoutEditor was started with.


static string process::getEnv(string parameter)

Returns: the environment variable parameter.

 layout->showMessage("your home path",process::getEnv("HOME"));

static bool process::isLinux()

Returns: true if this LayoutEditor was built for Linux.


static bool process::isMac()

Returns: true if this LayoutEditor was built for Mac OS X.


static bool process::isRunning(string toolName)

Returns: true if the external process started earlier with externalTool is still running. toolName must be identical with the name used when starting the process.


static bool process::isWindows()

Returns: true if this LayoutEditor was built for Microsoft Windows.


static int process::setEnv(string parameter,string value)

Sets the environment variable parameter to value.

Returns: the result of the underlying set operation.

Parameter:
parameter parameter
value value

static bool process::openBrowser(string url)

Opens a browser with url.

Returns: true if a browser was started, otherwise false.

#!/usr/bin/layout
#name=open browser
#help=open browser with the LayoutEditor docmentation

int main(){
 if (!process::openBrowser("https://layouteditor.org")){
 layout->showStatus("browser not found");
 }
 return 0;
}

static string process::startPythonScript(string filename,stringList args=stringList())

Starts a Python script filename with argument list args.

Parameter:
filename path of the Python script
args arguments passed to the script

Returns: the script output as a string.


static string process::startTclScript(string filename)

Starts a Tcl script filename.

Returns: the script output as a string.


static void process::terminate(string toolName)

Terminates the external process started earlier with externalTool. toolName must be identical with the name used when starting the process.


static string process::toolData()

Returns: the new command-line output since the last callback, if this macro is a callback of externalTool. In all other cases an empty string is returned.


static string process::toolResult()

Returns: the command-line output if this macro is a callback of externalTool. In all other cases an empty string is returned.

#!/usr/bin/layout
#name=callback macro
#help=callback macro to show output in the TextEditor

int main(){
 string s=process::toolResult();
 if (s.size()>1){
 textEdit *te=project::getCentralTextEditor();
 if (te->drawing->text()!="") {
 te->newFile();
 te->setFile(te->countFiles()-1);
 }
 te->setText(s);
 te->drawing->title="Command Line Output";
 te->drawing->setModifySaved();
 }
 return 0;
}

static string process::trigger()

Returns: the event that started this macro, if this macro is a callback of externalTool. Possible values are output and completed.

Obsolete Members


static bool process::startDetached(string command,stringList arguments)

Runs command with arguments. Returns true if the program launch was successful. If arguments is a string it is split on spaces to a stringList. There is no further control of the started process. Use externalTool if the process needs to be controlled.

Parameter:
command executable to run
arguments command-line arguments

Returns: true if the program was launched, otherwise false.