cmd
Syntax
cmd(app, args)
Returns
returns (string) the complete command line output
Parameters
- app (string)
- name of an whitelisted app or path to executable file
- args (array)
- all arguments to use in an array
Usage
The cmd
command can be used to call external command line apps. This can be any app on your computer that can be called from the command line.
Through the cmd_callback function you can keep track of the progress. Or interact with the application and keep the recipe responsive to the user. The cmd_callback function is called about once a second during a cmd() call. The arguments of the function contain the cmd app name and the console output of the program as a string. This output can be used to determine user feedback.
the return value of the callback is an optional object with 2 optional values. "terminate" a boolean which can be set to non-zero to hard kill the running command line application. And "input" a string which can be used to send keystrokes to the command line application. To graciously quit the app for instance.
A simple cmd
call wil look like this:
function main()
{
cmd("program_name", ['c:/somefile.mp4']);
}
function cmd_callback(cmdname, cmd_output)
{
if (cmd_output.length > 0)
echo(cmdname + " " + cmdoutput);
return {
"terminate": isCanceled(), // use this option to end the process if there is no better alternative
"input": "" // send console input
};
}
As an extra security step apps need to be added to the "Allowed cmd calls" list in the settings. You can check if the command is added with the getAllowedApps
function. If it's not added it can be added by the script with the function setAllowedApps
.
// CHECK IF A SPECIFIC APP IS ADDED TO THE ALLOWED CMD APPS
if( getAllowedApps("ls") == '')
{
// IF NOT YOU CAN ADD IT LIKE THIS
setAllowedApps("ls", '/bin/ls');
}
// CALL THE COMMAND (THE . IS AN ARGUMENT,
// IN THIS EXAMPLE THE CURRENT FOLDER THAT SHOULD BE SHOWN)
var rtn = cmd("ls", S['.']);
// SHOW THE RETURNED VALUE FROM THE COMMAND LINE APP
echo(rtn);
The call will return the entire console output. For example when calling ffprobe you might expect something like this:
var rtn = cmd("ffprobe", [
'-v','error',
'-select_streams','a:0',
'-show_entries','
stream=codec_name,
bit_rate,channels,
sample_rate,
bits_per_sample : format=duration : format_tags : stream_tags',
'-of','default=noprint_wrappers=1',
'-i',file
]);
/* will return something like this:
codec_name=pcm_s24le
sample_rate=48000
channels=2
bits_per_sample=24
bit_rate=2304000
duration=15.000000
TAG:encoder=Lavf58.19.100
*/
This output needs to be processed for example like this:
// CHECK IF FFPROBE RETURNED SOMETHING
if(ffprobe == ""){
echo("FFProbe returned empty");
return false;
}
// EXPLODE FFPROBE VALUES TO ARRAY
ffprobe = ffprobe.split("\n");
// IF ARRAY IS EMPTY, SOMETHING WENT WRONG
if(ffprobe.length == 0){
echo("error probing file");
return false;
}
// LOOP ARRAY ENTRIES AND LOOK FOR VALUES
for (var j = 0; j < ffprobe.length; j++) {
var a = ffprobe[j].split("=");
if(a[0] == "bits_per_sample") { rtn.bitrate = a[1]; }
else if (a[0] == "sample_rate") { rtn.samplerate = a[1]; }
else if (a[0] == "duration") { rtn.duration = parseFloat(a[1]); }
else if (a[0] == "channels") { rtn.channels = a[1]; }
}