how to parse result from MsProcess::readAllStandardOutput() ?
Hello.
I'd like to get the last line of the result for an external process execution in MuseScore. The process is called via plugin API (MsProcess
), and its execution result I collect using readAllStandardOutput()
console.log("generating MIDI file with command: "+cmd);
proc.start(cmd);
var val = proc.waitForFinished(30000);
if (val) {
var res = proc.readAllStandardOutput();
console.log(res);
console.log("last line of process result is: "+res.lastIndexOf("\n")); //doesn't work, how can I parse res ?
return(res);
} else {
console.log("Generation of MIDI file failed, please check (blah blah)");
return;
}
While the console.log(res);
displays the multiple lines of execution log just fine, the next call with res.lastIndexOf("\n")
fails miserably stating:
182:-1: TypeError: Property 'lastIndexOf' of object {full content of 'res' displayed} is not a function
How can I parse the content of readAllStandardOutput() or cast it into a usable (multiline) String or do anything with it a bit more complex (regexp?) than log.
Thank you!
Comments
.
In reply to Can QML execute arbitrary… by [DELETED] 1831606
.
In reply to Yes, and you should indeed… by berteh
.
In reply to This is a horror and… by [DELETED] 1831606
.
In reply to It's much easier to leave… by [DELETED] 1831606
.
In reply to This is a horror and… by [DELETED] 1831606
.
In reply to What alternative would you… by berteh
.
In reply to Arbitrary command-line… by [DELETED] 1831606
.
In reply to Browser scripting is a good… by [DELETED] 1831606
.
In reply to It would take exactly one… by [DELETED] 1831606
.
In reply to Browser scripting is a good… by [DELETED] 1831606
.
In reply to As you seem well versed in… by berteh
.
Without being aware of what has already been said in this interesting conversation above ;-) ...
The return type is a QByteArray, which should be converted into an ArrayBuffer by the QML engine (https://doc.qt.io/qt-5/qtqml-cppintegration-data.html#qbytearray-to-jav…)
This does support the toString() prototype method (probably how the console.log processes it) so you can likely work from there. (See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Globa…). Another way is to turn it into a typed array (UInt8Array for example) and then parse those back into chars; this assumes of course that you also handle eventual encoding of the characters.
In reply to Without being aware of what… by jeetee
indeed, readAllStandardOutput().toString() does work, thanks !
What is the type of the object I get therefrom? QML QString, javascript string, or something else? (I'm not understanding exactly what's happening between the C++, the QML and the language)
In reply to indeed, … by berteh
JavaScript string.
Call and language order:
1. QML proc.readAllStandardOutput()
2. C++::QMLEngine turns this into MSProcess::readAllStandardOutput()
3. C++ function is executed, returns a QByteArray within C++
4. C++::QMLEngine turns this into an ArrayBuffer compatible object QML/JavaScript
5. QML/JavaScript ends up with the result of the call, being now an ArrayBuffer JavaScript
6. JavaScript toString() is executed
7. JavaScript resulting String object is created JavaScript
The boundaries between JavaScript and QML are quite fuzzy and exist mainly of additional JavaScript objects that have a C++ "code-behind" part (such as QtQuick) and the translation layer between JavaScript/QML object types and their C++ counter parts (the QMLEngine)
In reply to JavaScript string. Call and… by jeetee
Thanks @jeetee for these extensive explanations ! It does help a QML newbie like me ;)